Need help on correcting the program..

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 :confused:
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)

Hi,

Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html
then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom... :slight_smile:

then come back to your posts and edit it (lower right of your post screen) add the code tags and make us happy.

Welcome to the forum.

Please help us to help you by formatting your code in the IDE (Tools/Auto Format) then post it here in code tags (</> icon above/left of the forum editor)

Done the corrections..

Thanks. See how much easier it is to scroll and copy the code now that it is in code tags.

The problem is that because the distance stays below 20 cm the motors will remain stopped no matter what commands you give it. A suggestion - create a new boolean global variable named moving and set it initially to true then change your range check to

  if (distance < 20 && moving)  //distance is only important if moving
  {
    digitalWrite(InA, LOW);
    digitalWrite(InB, LOW);
    digitalWrite(InC, LOW);
    digitalWrite(InD, LOW);
    moving = false;
  }

Now the automatic stop will only happen when the moving boolean is true.
Next problem, when/how would you like to reactivate the automatic stop ?
Maybe

else
{
  moving = true;
}

Thank you so much sir :slight_smile:

Almost working , after moving beyond 20 cm , its slowly starts moving forward and if i send back or other commands it does come back slowly bit by bit like a stepper motor it rotates few microseconds and then waits for a second or two then rotate for few microseconds same with reverse , then after coming out of 20 cm zone it reverses fast!

Modified as u suggested.. tell me how to correct above problem , i guess its due to improper delay set by me.. and what would be ideal one...

// 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.


boolean moving= true;

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 && moving)  //distance is only important if moving
  {
    digitalWrite(InA, LOW);
    digitalWrite(InB, LOW);
    digitalWrite(InC, LOW);
    digitalWrite(InD, LOW);
    moving = false;
    delay(30);
  }

  else
  {
    moving = true;
  }


}

Regards

Give this a try

  if (distance < 20 && moving)  //distance is only important if moving
  {
    digitalWrite(InA, LOW);
    digitalWrite(InB, LOW);
    digitalWrite(InC, LOW);
    digitalWrite(InD, LOW);
    moving = false;
    delay(30);
  }
  else if (distance > 25 && !moving)
  {
    moving = true;
  }

I am not sure that it will solve the problem but it should turn on the moving test only when the distance is greater than 25, or any other number you choose.