Need help developing Logic For arduino bot

Hello, I just completed a remote controlled bot using arduino uno, TSOP, ir reciever.

The logic for the code is very simple. When forward button is pressed on the remote, the bot moves forward for 5 secs then stops, and the code then searches for new input. Similar approach is used for other directional movements.

Now, I want (when forward button is pressed) the bot to keep moving forward until any other directional buttons are pressed.

Sounds simple, but I have not been able to successfully bound the logic, please help.

ps:- (In the given code, removing the stop1 function from each condition statement makes the statement continuous but the state does not change, ie. if forward is pressed it will keep moving forward, and never stop even if other inputs are given.)

/*
 * IRremote: IRrecvDemo - demonstrates receiving IR codes with IRrecv
 * An IR detector/demodulator must be connected to the input RECV_PIN.
 * Version 0.1 July, 2009
 * Copyright 2009 Ken Shirriff
 * http://arcfn.com
 */

#include <IRremote.h>

int motor1Pin1 = 8;    
int motor1Pin2 = 9;    
int enablePin1 = 10; 
int motor2Pin1 = 5;    
int motor2Pin2 = 6;
int enablePin2 = 7;  




int RECV_PIN = 12;


IRrecv irrecv(RECV_PIN);

decode_results results;





void left()
{
  digitalWrite(motor1Pin1,HIGH);
  digitalWrite(motor1Pin2,LOW);
  digitalWrite(motor2Pin1,LOW);
  digitalWrite(motor2Pin2,HIGH);
}
void back()
{

  
   digitalWrite(motor1Pin1,HIGH);

  digitalWrite(motor1Pin2,LOW);
    digitalWrite(motor2Pin1,HIGH);
     digitalWrite(motor2Pin2,LOW);
  
}

void forward()
{
  digitalWrite(motor1Pin1,LOW);
  digitalWrite(motor1Pin2,HIGH);
  digitalWrite(motor2Pin1,LOW);
  digitalWrite(motor2Pin2,HIGH);
}

void right()
{
  digitalWrite(motor1Pin1,LOW);
  digitalWrite(motor1Pin2,HIGH);
  digitalWrite(motor2Pin1,HIGH);
  digitalWrite(motor2Pin2,LOW);
}

void stop1()
{
  digitalWrite(motor1Pin1,LOW);
  digitalWrite(motor1Pin2,LOW);
  digitalWrite(motor2Pin1,LOW);
  digitalWrite(motor2Pin2,LOW);
}



void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver

   pinMode(motor1Pin1, OUTPUT);
  pinMode(motor1Pin2, OUTPUT);
   pinMode(motor2Pin1, OUTPUT);
  pinMode(motor2Pin2, OUTPUT);
 

  
  pinMode(enablePin1, OUTPUT);
   pinMode(enablePin2, OUTPUT);
  digitalWrite(enablePin1, HIGH);
  digitalWrite(enablePin2, HIGH);
}

void loop() {
  if (irrecv.decode(&results)) {
    if(results.value==12583000){
    Serial.println("forward()");
    forward();
    delay(1000);
    stop1();
    delay(50);
    }
    
    else if(results.value==12583003){
    Serial.println("right()");
    right();
    delay(500);
    stop1();
    }
    
     else if(results.value==12583001){
    Serial.println("back()");
    back();
    delay(500);
    stop1();
    }
    
     else if(results.value==12583002){
    Serial.println("left()");
    left();
    delay(500);
    stop1();
    }
    irrecv.resume();
    
    
    
    
    // Receive the next value
  }
  delay(50);
}

loop gets called continuously, so you need to store the FORWARD state, and use an “if” to check it each time through the loop.

I cannot be sure this will work with your hardware, but how about setting a boolean variable to true when you should be going forward and false when you are not?

Each time through the loop go forward as long as the variable is set and call stop1() when the bot is going forward and a different button was clicked.

boolean goingFoward = false;

void loop() {
  if (irrecv.decode(&results)) {

    // if going forward and need to stop
    if( (results.value!=12583000) && (goingForward))
    {
      stop1();
      delay(50);

    } // if

    if(results.value==12583000){

      goingForward = true;

    }

    else if(results.value==12583003){
      Serial.println("right()");
      goingForward = false;
      right();
      delay(500);
      stop1();
    }

    else if(results.value==12583001){
      Serial.println("back()");
      goingForward = false;
      back();
      delay(500);
      stop1();
    }

    else if(results.value==12583002){
      Serial.println("left()");
      goingForward = false;
      left();
      delay(500);
      stop1();
    }

    irrecv.resume();

    // Receive the next value
  }

  // if bot is going forward
  if( goingForward )
  {
    Serial.println("forward()");
    forward();
    delay(1000);
  }

  delay(50);
}

I would remove Delay(1000); and stop1() from the forward case. I would remove delay(50); from two places. When the bot is moving forward are you getting the serial output from the other cases?

johnwasser: I would remove Delay(1000); and stop1() from the forward case. I would remove delay(50); from two places. When the bot is moving forward are you getting the serial output from the other cases?

I did that, in that case the the if statement continues indefinitely.