#autonome

Dernière modification : 2017/02/01 16:42

work in progress automatisation

commenté

(basé sur le code https://blog.the-jedi.co.uk/2015/11/26/nodemcu-motor-shield-review/ )



//AUTOBOT 01022017
//un véhicule autonome basé sur NodeMCU + shield  avec sonar et 4 moteurs
int DistLeft;
int DistMid;
int DistRight;
//on  insère la librairie newping
#include <NewPing.h>

//on définit les pins ECHO & TRIGGER du sonar
NewPing sonar(D6, D7, 100);

// on définit des action en attribuant des valeurs aux moteurs sur les PINS suivants
//PINS 4 & 5 en analogWrite de 0 à 1023
//PINS 0 & 2 en digitalWrite  à 0 ou 1
void stop(void)
{analogWrite(5, 0);analogWrite(4, 0);}
void forward(void)
{analogWrite(5, 1023);analogWrite(4, 1023);digitalWrite(0, HIGH);digitalWrite(2, LOW);}
void backward(void)
{analogWrite(5, 1023);analogWrite(4, 1023);digitalWrite(0, LOW);digitalWrite(2, HIGH);}
void left(void)
{analogWrite(5, 1023);analogWrite(4, 1023);digitalWrite(0, LOW);digitalWrite(2, LOW);}
void right(void)
{analogWrite(5, 1023);analogWrite(4, 1023);digitalWrite(0, HIGH);digitalWrite(2, HIGH);}

//on définit les pins 0/2/4/5 pour les moteurs
void setup()
{pinMode(5, OUTPUT);pinMode(4, OUTPUT);pinMode(0, OUTPUT);pinMode(2, OUTPUT);}

//on boucle sur les actions à faire
void loop()
{
if (sonar.ping_cm() > 19) {
  forward();
}

else if(sonar.ping_cm() <= 19) {
    stop();
    DistMid = sonar.ping_cm();
    backward();
    delay(300);
    left();
    delay(800);
    DistLeft = sonar.ping_cm();
    right();
    delay(1200);
    DistRight = sonar.ping_cm();
    if (DistRight > DistLeft){
        forward();
    }
    else if (DistRight < DistLeft) {    
      left();
      delay(1200);
      forward();
    }
    else if ((DistRight + DistLeft) < DistMin){
        backward();
    }
  }
}




redux



#include <NewPing.h>
NewPing sonar(D6, D7, 100);

void stop(void)
{analogWrite(5, 0);analogWrite(4, 0);}

void forward(void)
{analogWrite(5, 1023);analogWrite(4, 1023);digitalWrite(0, HIGH);digitalWrite(2, HIGH);}

void backward(void)
{analogWrite(5, 1023);analogWrite(4, 1023);digitalWrite(0, LOW);digitalWrite(2, LOW);}

void left(void)
{analogWrite(5, 1023);analogWrite(4, 1023);digitalWrite(0, LOW);digitalWrite(2, HIGH);}

void right(void)
{analogWrite(5, 1023);analogWrite(4, 1023);digitalWrite(0, HIGH);digitalWrite(2, LOW);}

void setup()
{pinMode(5, OUTPUT);pinMode(4, OUTPUT);pinMode(0, OUTPUT);pinMode(2, OUTPUT);}

void loop()
{
if (sonar.ping_cm() > 20) {
    forward();
    delay(600);
  }
  else {
    right();
    delay(2000);
  }
}


===========================
autobot.ino


const short int BUILTIN_LED2 = 16;

#include <NewPing.h>
NewPing sonar(D6, D7, 100);

void stop(void)
{
  analogWrite(5, 0);
  analogWrite(4, 0);
}

void forward(void)
{
  analogWrite(5, 1024);
  analogWrite(4, 1024);
  digitalWrite(0, HIGH);
  digitalWrite(2, HIGH);
}

void backward(void)
{
  analogWrite(5, 1023);
  analogWrite(4, 1023);
  digitalWrite(0, LOW);
  digitalWrite(2, LOW);
}

void left(void)
{
  analogWrite(5, 1023);
  analogWrite(4, 1023);
  digitalWrite(0, LOW);
  digitalWrite(2, HIGH);
}

void right(void)
{
  analogWrite(5, 1023);
  analogWrite(4, 1023);
  digitalWrite(0, HIGH);
  digitalWrite(2, LOW);
}

void setup()
{
  Serial.begin(115200);
  pinMode(BUILTIN_LED2, OUTPUT);
  digitalWrite(BUILTIN_LED2, LOW);

  pinMode(5, OUTPUT); // 1,2EN aka D1 pwm left
  pinMode(4, OUTPUT); // 3,4EN aka D2 pwm right
  pinMode(0, OUTPUT); // 1A,2A aka D3
  pinMode(2, OUTPUT); // 3A,4A aka D4
}

void loop()
{
  delay(50);
  Serial.print("Ping: ");
  Serial.print(sonar.ping_cm());
  Serial.println("cm");

  if (sonar.ping_cm() > 20) {
    forward();
    delay(300);
  }
  else {
    stop();
    backward();
    delay(600);
  }
}