วันศุกร์ที่ 8 กันยายน พ.ศ. 2560

Robot car Part 3/3 avoid obstacle . Finish







HC-SR04 Ultrasonic module








Drive unit using L298 2 motor driver 



Connection  
L298 to Arduino Nano
ENA=2;
IN1=3;
IN2=4;
ENB=7;
IN3=5;
IN4=6;
Add bluetooth for mobile App control


Bluetooth >> Arduino Nano

Rx >> 11
Tx >> 10
Vcc >> 5 Vcc
GND >> GND


Code 


#include <Servo.h> 

#include<SoftwareSerial.h>

Servo myservo;
const int pingPin = 13;
int inPin = 12;
SoftwareSerial mySerial(10, 11); // RX, TX
String state;
int ENA=2;
int IN1=3;
int IN2=4;
int ENB=7;
int IN3=5;
int IN4=6;
void setup() {

  // ultrasonic

  myservo.attach(9);
 // Serial.begin(9600);
  // put your setup code here, to run once:
       mySerial.begin(9600);
       Serial.begin(9600); //start serial communication
       pinMode(ENA, OUTPUT);//Pin2
       pinMode(IN1, OUTPUT);//Pin3
       pinMode(IN2, OUTPUT);//Pin4
       pinMode(IN3, OUTPUT);//Pin5
       pinMode(IN4, OUTPUT);//Pin6
       pinMode(ENB, OUTPUT);//Pin7

//digitalWrite(ENA,LOW);
//digitalWrite(IN1,LOW);
//digitalWrite(IN2,LOW);
//digitalWrite(IN3,LOW);
//digitalWrite(IN4,LOW);
//digitalWrite(ENB,LOW);
   
}
void loop() {
  // put your main code here, to run repeatedly:
 Ultrasonic();

  while (mySerial.available()){ //Check if there is an available byte to read
     delay(10); //Delay added to make thing stable 
    char c = mySerial.read(); //Conduct a serial read
   
    state += c; //Shorthand for voice = voice + c
}

if (state.length() > 0){
  //Serial.println(state);
  if(state == "forward")
  {


   Forward();
  }
 else if(state == "back")
 {
    Backward();
 }
 if(state == "left")
  {
    Turn_left();
  }
 else if(state == "right")
 {
    Turn_right();
 }
  else if(state == "stop")
 {
    Stop();
 }
  state =""; //Reset the variable after initiating
  }
}
void Forward()
{
 {digitalWrite(ENA,HIGH);
 digitalWrite(IN1,LOW);
 digitalWrite(IN2,HIGH);
 digitalWrite(ENB,HIGH);
 digitalWrite(IN3,LOW);
 digitalWrite(IN4,HIGH);}
}
void Backward()
{
 digitalWrite(ENA,HIGH);
 digitalWrite(IN1,HIGH);
 digitalWrite(IN2,LOW);
 digitalWrite(ENB,HIGH);
 digitalWrite(IN3,HIGH);
 digitalWrite(IN4,LOW);
}

void Turn_left()
{
 digitalWrite(ENA,HIGH);
 digitalWrite(IN1,HIGH);
 digitalWrite(IN2,LOW);
 digitalWrite(ENB,HIGH);
 digitalWrite(IN3,LOW);
 digitalWrite(IN4,HIGH);
}
 void Turn_right()
{
 digitalWrite(ENA,HIGH);
 digitalWrite(IN1,LOW);
 digitalWrite(IN2,HIGH);
 digitalWrite(ENB,HIGH);
 digitalWrite(IN3,HIGH);
 digitalWrite(IN4,LOW);
}
void Stop()
{
 digitalWrite(ENA,HIGH);
 digitalWrite(IN1,LOW);
 digitalWrite(IN2,LOW);
 digitalWrite(ENB,HIGH);
 digitalWrite(IN3,LOW);
 digitalWrite(IN4,LOW);
}
void Ultrasonic()
{
long duration, cm;
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(inPin, INPUT);
duration = pulseIn(inPin, HIGH);
cm = microsecondsToCentimeters(duration);
 if (cm < 15) {  
    //  servo
Stop();
delay(1000);
Turn_right();
delay(250);
Stop();
delay(1000);
        myservo.write(0); 
        delay(1000);      
        myservo.write(90); 
        delay(1000);      
        myservo.write(270); 
        delay(1000);    
  // end servo
delay(2000);
  Forward();
}
  else {
    myservo.write(90);
  }
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
  

ไม่มีความคิดเห็น:

แสดงความคิดเห็น