Hello all... I'm a newbie , trying to figure out what i made wrong in the following code.
this is project based on Arduino , Bluetooth , MIT APP inventor , H - Bridge and SR 04 ping sensor , i use q,w,e,r,t to control the robots movement such as fwd , rev , left , right and stop . And it works like a charm , next i inserted whenever the distance between Obstacle and robot is < 20 cm , it must stop , it stops though , but after stopping it wont come out of loop , and it won't take any of my other commands like moving back etc , I know i made mistake in loop statement ![]()
and if i change these Low write signals to move left or write , whenever arduino is powered on , it keeps moving left , without even communicating with bluetooth!
Is there any way to exit this loop after some stipulated time is elapsed , like let all motors stop for 5 seconds when the distance is less than 20 cm and then wait for user inputs..
Full Code
// Definitions
//Left motor
#define InA 5 // declare PIn 5 as InA(just a name)
#define InB 6
//Right motor
#define InC 7
#define InD 8
#define LED 13
#include <NewPing.h>
#define TRIG_PIN 3 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 2 // Arduino pin tied to echo pin on the ultrasonic sensor.
char state; // Variable to hold serial data
NewPing sonar(TRIG_PIN, ECHO_PIN );
// ******************************************************
// SETUP
//********************************************************
void setup(){
for(int i=2;i<14;i++) {
pinMode(i,OUTPUT);
}
Serial.begin(9600);
pinMode(ECHO_PIN,INPUT);
pinMode(TRIG_PIN,OUTPUT);
digitalWrite(LED,HIGH);
delay(300);
digitalWrite(LED,LOW);
}
void loop(){
digitalWrite(TRIG_PIN,HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN,LOW);
float duration= pulseIn(ECHO_PIN, HIGH);
float distance= (duration/2) / 29.1;
Serial.print(distance);
Serial.println("cm");
delay(20);
while(Serial.available() > 0)
{
state =(char) Serial.read();
digitalWrite(LED,HIGH);
}
if (state== 'q'){ //frd mode
digitalWrite(InA,HIGH);
digitalWrite(InB,LOW);
digitalWrite(InC,HIGH);
digitalWrite(InD,LOW);
digitalWrite(LED,HIGH); // delay(300);
digitalWrite(LED,LOW);
}
if (state== 'w'){ //back mode
digitalWrite(InA,LOW);
digitalWrite(InB,HIGH);
digitalWrite(InC,LOW);
digitalWrite(InD,HIGH);
digitalWrite(LED,HIGH); //delay(300);
digitalWrite(LED,LOW);
}
if (state== 'r'){ //right mode
digitalWrite(InA,LOW);
digitalWrite(InB,HIGH);
digitalWrite(InC,HIGH);
digitalWrite(InD,LOW);
digitalWrite(LED,HIGH); // delay(300);
digitalWrite(LED,LOW);
}
if (state== 'e'){ //LEFT mode
digitalWrite(InA,HIGH);
digitalWrite(InB,LOW);
digitalWrite(InC,LOW);
digitalWrite(InD,HIGH);
digitalWrite(LED,HIGH);//delay(300);
digitalWrite(LED,LOW);
}
if (state== 't'){ //stop mode
digitalWrite(InA,LOW);
digitalWrite(InB,LOW);
digitalWrite(InC,LOW);
digitalWrite(InD,LOW);
digitalWrite(LED,HIGH);//delay(300);
digitalWrite(LED,LOW);
}
if (distance <20)
{
digitalWrite(InA,LOW);
digitalWrite(InB,LOW);
digitalWrite(InC,LOW);
digitalWrite(InD,LOW);
}
}
Regards
final_code.ino (3.24 KB)