project help

hey, i am working on a project to make a simple 2 wheel chasis smart car just it needed to be operated via bluetooth…i got everything working just i want that my car should move forward, backward, right
and left using buttons on my mobile interface which it does when i click forward the car moves forward and it moves unless new command is given to it but i want this “If none of these buttons is pressed car should stop” plz help.here is the code.

# include <SoftwareSerial.h>

SoftwareSerial BT(0, 1); //TX, RX respetively
String readdata;
int motorLpin1=2;
int motorLpin2=3;
int motorRpin1=4;
int motorRpin2=5;
int motorLpwm=10;
int motorRpwm=11;

int motorSpeed=125;
int turn=50;

void setup() {
  Serial.begin(9600);
  Serial.flush();
  pinMode(8, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(motorLpin1,OUTPUT);
  pinMode(motorLpin2,OUTPUT);
  pinMode(motorRpin1,OUTPUT);
  pinMode(motorRpin2,OUTPUT);
  pinMode(motorLpwm,OUTPUT);
  pinMode(motorRpwm,OUTPUT);
}

void loop() {
  String input="";
  while(Serial.available()){
    input+=(char)Serial.read();
    delay(5);
  }
 
  if(input=="forward"){
    fwd();
    Serial.println("fwd");
  }
  else if(input=="stop"){
    stp();
    Serial.println("stp");
  }
  else if(input=="led"){
    led();
    Serial.println("led");
  }
  else if(input=="ledoff"){
    ledoff();
    Serial.println("led");
  }
  else if(input=="backward"){
    rev();
    Serial.println("backwards");
  }
  else if(input.indexOf("left")>-1){
    lft();
    Serial.println("left");
  }
  else if(input.indexOf("right")>-1){
    rght();
    Serial.println("right");
  }

  else if(input!=""){
    motorSpeed=input.toInt();
  }
}
void led(){
  digitalWrite(8, HIGH);                 
  digitalWrite(12, HIGH);   
  }
void ledoff(){
  digitalWrite(8, LOW);                 
  digitalWrite(12, LOW);   
  }
void fwd(){
 
  analogWrite(motorLpwm,motorSpeed);
  analogWrite(motorRpwm,motorSpeed);
  digitalWrite(motorLpin1,1);
  digitalWrite(motorLpin2,0);
  digitalWrite(motorRpin1,1);
  digitalWrite(motorRpin2,0);
  Serial.println("agyaa");
}

void rev(){
  analogWrite(motorLpwm,motorSpeed);
  analogWrite(motorRpwm,motorSpeed);
  digitalWrite(motorLpin1,0);
  digitalWrite(motorLpin2,1);
  digitalWrite(motorRpin1,0);
  digitalWrite(motorRpin2,1);
}

void lft(){
  analogWrite(motorLpwm,motorSpeed-turn);
  analogWrite(motorRpwm,motorSpeed+turn);
  digitalWrite(motorLpin1,0);
  digitalWrite(motorLpin2,1);
  digitalWrite(motorRpin1,1);
  digitalWrite(motorRpin2,0);
}

void rght(){
  analogWrite(motorLpwm,motorSpeed+turn);
  analogWrite(motorRpwm,motorSpeed-turn);
  digitalWrite(motorLpin1,1);
  digitalWrite(motorLpin2,0);
  digitalWrite(motorRpin1,0);
  digitalWrite(motorRpin2,1);
}

void stp(){
  analogWrite(motorLpwm,0);
  analogWrite(motorRpwm,0);
  digitalWrite(motorLpin1,1);
  digitalWrite(motorLpin2,1);
  digitalWrite(motorRpin1,1);
  digitalWrite(motorRpin2,1);
}

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

I believe you need to add a short timer into your loop. With this every time you go through the loop you check whether the time is up and if so you stop the motor.

Every time you receive a command to move you reset the time which gets compared. So as long as commands get send the motor moves. If you stop sending the motors stops say after 1s.

You need to use a timer because the loop gets called very often while you do not send anything. Without the timer the motor will stop immediately after it received a command, and this might even be too short for you to see the motor moving at all.

Check out the File → Examples → 02.Digital → BlinkWithoutDelay.

Once this works in principal, you need to adjust the timer. If the interval is short you need to send more commands per second to keep the motor spinning. This is limited by the command length and the baudrate. If the interval is longer you send less commands per second, but the motor will be less accurate to control.

Tip: Make sure to mark your source code with the </> icon in the tool bar. This will place code tag around the source and then display the code in a nice source code box. :slight_smile: Try to modify your post. :slight_smile:

umniyah:
but i want this "If none of these buttons is pressed car should stop" plz help.here is the code.

A good way to implement that is for the transmitter to send a message at regular intervals (maybe 5 times per second) even if the data has not changed.

Then the receiver will know if it has not received a message for (say) 1 second that communication has failed.

...R

@umniyah, do not cross-post. Other thread removed.