(please help)IR controller for robot switch case problem

I am building a sumo robot that can be controlled by the IR or AUTO every thing worked fine(sensors,connection) but the only problem i am facing is in the code it self,i crated a array of state to jump to them when an action happen.

when i press the button it move to that case but when i press another button to go to another case it dose not response it stay in the same case.

i think the problem is that this line repeat in all of the case statement.
pls help me i have been trying to fix it for a week.

if (irrecv.decode(&results)){
           if (results.value == BUTTON_front)
                   currentState = MovingForward;
           
           if (results.value == BUTTON_back){
                   currentState = Backward;
                              
           if (results.value == BUTTON_right)
                   currentState = Turnright;
          
           if (results.value == BUTTON_left)
                   currentState = Turnleft;

          
                      irrecv.resume(); 


        }

I attached the file if u want to look at it

Update1: now i tested the code without powering the motor and it work fine but when i plug the motor the problems start......when i press the button it change the state but it dose not exit it(the IR stop lighting up) !!!!!?????

Update2 :i did change the motor output in every state to(0,0,0,0) which is OFF and the IR worked fine so the problem appear when one of the motor go high so i guess it is power problem,but i don't know why..... the motor connected to the external battery(L293D) and the sensors connected to the vcc.

any suggestion??

the_best.ino (4.1 KB)

#include <IRremote.h>
#include <boarddefs.h>
#include <NewPing.h>
enum {Idle,MovingForward,Turnleft,Turnright,Turn,AM_Backward,Backward,rest,AM_MovingForward};
int currentState = Idle; 
int RECV_PIN = A4;
int VOID = LOW;
IRrecv irrecv(RECV_PIN);
decode_results results;
#define BUTTON_front 0xEFA25D
#define BUTTON_back 0xEFB24D
#define BUTTON_left 0xEF50AF
#define BUTTON_right 0xEF12ED
#define BUTTON_speedup 0xEFE817
#define BUTTON_speeddown 0xEF6897
#define ControlMotorLeft1 A0
#define ControlMotorLeft2 A1
#define ControlMotorRight1 A2
#define ControlMotorRight2 A3
#define TheLED 7
#define IRS 6
#define PushButtonON 5
#define PushButtonOFF 4
#define PushButtonAUTO 3
#define Controller A4
#define TRIGGER_PIN A5
#define ECHO_PIN     A5
#define MAX_DISTANCE 200
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);



void setup() {

Serial.begin(9600);  
//Control Motor Left
pinMode(ControlMotorLeft1, OUTPUT);
pinMode(ControlMotorLeft2, OUTPUT);
//Control Motor Right
pinMode(ControlMotorRight1, OUTPUT);
pinMode(ControlMotorRight2, OUTPUT);
//LED
pinMode(TheLED,OUTPUT);
//R1 sensor
pinMode(IRS,INPUT);
//button(ON)
pinMode(PushButtonON,INPUT_PULLUP);
//button(OFF)
pinMode(PushButtonOFF,INPUT_PULLUP);
//button(auto)
pinMode(PushButtonAUTO,INPUT_PULLUP);
//controller
irrecv.enableIRIn();

}
void AllOff(){
digitalWrite(ControlMotorLeft1,LOW);
digitalWrite(ControlMotorLeft2,LOW);
digitalWrite(ControlMotorRight1,LOW);
digitalWrite(ControlMotorRight2,LOW);
digitalWrite(TheLED,LOW);
}

void loop() {
  
    switch(currentState)
  {
  case Idle:{
        Serial.println("Idle");
      AllOff();
     if(!digitalRead(PushButtonON))
          currentState = rest;
    
     if(!digitalRead(PushButtonAUTO))
          currentState = AM_MovingForward;
    break;
  }
  case rest:{
      Serial.println("Rest");
      digitalWrite(ControlMotorLeft1,LOW);
      digitalWrite(ControlMotorLeft2,LOW);
      digitalWrite(ControlMotorRight1,LOW);
      digitalWrite(ControlMotorRight2,LOW);
      digitalWrite(TheLED,HIGH);   
     if(!digitalRead(PushButtonOFF))
          currentState = Idle;
     if(!digitalRead(PushButtonAUTO))
          currentState = AM_MovingForward;          
     if (irrecv.decode(&results)){
           if (results.value == BUTTON_front){
                   currentState = MovingForward;
                               irrecv.resume(); 

           }
           if (results.value == BUTTON_back){
                   currentState = Backward;
                               irrecv.resume(); 

           }
           if (results.value == BUTTON_right){
                   currentState = Turnright;
                               irrecv.resume(); 
          }
           if (results.value == BUTTON_left){
                   currentState = Turnleft;
                   irrecv.resume(); 

          }
                      irrecv.resume(); 

        }

    break;}
    case MovingForward:{
      Serial.println("Moving Forward");
      analogWrite(ControlMotorLeft1,150);
      analogWrite(ControlMotorLeft2,0);
      analogWrite(ControlMotorRight1,150);
      analogWrite(ControlMotorRight2,0);
      digitalWrite(TheLED,HIGH);   
      VOID = digitalRead(IRS);
     if(!digitalRead(PushButtonOFF))
          currentState = Idle;
     if(!digitalRead(PushButtonAUTO))
          currentState = AM_MovingForward;          
     if (irrecv.decode(&results)){
           if (results.value == BUTTON_back){
                   currentState = Backward;
                               irrecv.resume(); 

           }
           if (results.value == BUTTON_right){
                   currentState = Turnright;
                               irrecv.resume(); 

          }
           if (results.value == BUTTON_left){
                   currentState = Turnleft;
                               irrecv.resume(); 

            }
            if (results.value == BUTTON_speedup){
                  analogWrite(ControlMotorLeft1,250);
                  analogWrite(ControlMotorRight1,250);

          }
                      irrecv.resume(); 

            if (results.value == BUTTON_speeddown){
                  digitalWrite(ControlMotorLeft1,100);
                  analogWrite(ControlMotorRight1,100);      
                  }
                  
     }    
      if (VOID == HIGH){
          currentState =Backward ;
     }


    break;}
    case Backward:{
      Serial.println("Backward");
      digitalWrite(ControlMotorLeft1,LOW);
      digitalWrite(ControlMotorLeft2,HIGH);
      digitalWrite(ControlMotorRight1,LOW);
      digitalWrite(ControlMotorRight2,HIGH);
      digitalWrite(TheLED,HIGH);
      VOID = digitalRead(IRS);         
     if(!digitalRead(PushButtonOFF))
          currentState = Idle;
     if(!digitalRead(PushButtonAUTO))
          currentState = AM_MovingForward;          
      if (irrecv.decode(&results)){
          if (results.value == BUTTON_front){
                  currentState = MovingForward;
                              irrecv.resume(); 

           }
           if (results.value == BUTTON_right){
                   currentState = Turnright;
                               irrecv.resume(); 

          }
           if (results.value == BUTTON_left){
                   currentState = Turnleft;
                               irrecv.resume(); 

            }
            irrecv.resume(); 
      }          
      if (VOID == HIGH){
          currentState =MovingForward ;
     }

    break;}
    case Turnleft:{
      Serial.println("Turn left");
      digitalWrite(ControlMotorLeft1,LOW);
      digitalWrite(ControlMotorLeft2,HIGH);
      digitalWrite(ControlMotorRight1,HIGH);
      digitalWrite(ControlMotorRight2,LOW);
      digitalWrite(TheLED,HIGH);   
     if(!digitalRead(PushButtonOFF))
          currentState = Idle;
     if(!digitalRead(PushButtonAUTO))
          currentState = AM_MovingForward;          
     if (irrecv.decode(&results)){
          if (results.value == BUTTON_front){
                  currentState = MovingForward;
           }
           if (results.value == BUTTON_back){
                   currentState = Backward;
           }
           if (results.value == BUTTON_right){
                   currentState = Turnright;
          }
          
           irrecv.resume(); 
  }          
    break;}
    case Turnright:{
      Serial.println("Rest");
      digitalWrite(ControlMotorLeft1,HIGH);
      digitalWrite(ControlMotorLeft2,LOW);
      digitalWrite(ControlMotorRight1,LOW);
      digitalWrite(ControlMotorRight2,HIGH);
      digitalWrite(TheLED,HIGH);   
     if(!digitalRead(PushButtonOFF))
          currentState = Idle;
     if(!digitalRead(PushButtonAUTO))
          currentState = AM_MovingForward;          
     if (irrecv.decode(&results)){
          if (results.value == BUTTON_front){
                  currentState = MovingForward;
           }
           if (results.value == BUTTON_back){
                   currentState = Backward;
           }
           if (results.value == BUTTON_left){
                   currentState = Turnleft;
            }
            irrecv.resume(); 
  }          

    break;}
  case AM_MovingForward:{
       Serial.println("Auto Mode");
      digitalWrite(ControlMotorLeft1,HIGH);
      digitalWrite(ControlMotorLeft2,LOW);
      digitalWrite(ControlMotorRight1,HIGH);
      digitalWrite(ControlMotorRight2,LOW);
      VOID = digitalRead(IRS);         
      digitalWrite(TheLED,HIGH);
       
     if(!digitalRead(PushButtonOFF))
          currentState = Idle;

     if (VOID == HIGH){
            currentState =AM_Backward ;}
       
       if(sonar.ping_cm()<=8)
            currentState =AM_Backward ;
            
    break;}
   case AM_Backward:{
        Serial.println("Backward");
      digitalWrite(ControlMotorLeft1,LOW);
      digitalWrite(ControlMotorLeft2,HIGH);
      digitalWrite(ControlMotorRight1,LOW);
      digitalWrite(ControlMotorRight2,HIGH);
       digitalWrite(TheLED,HIGH);
       delay(1000);
       currentState = Turn; 
     if(!digitalRead(PushButtonOFF))
          currentState = Idle;

            
    break;}
  case Turn:{
      Serial.println("Turn");
      digitalWrite(ControlMotorLeft1,HIGH);
      digitalWrite(ControlMotorLeft2,LOW);
      digitalWrite(ControlMotorRight1,LOW);
      digitalWrite(ControlMotorRight2,HIGH);
       digitalWrite(TheLED,HIGH);
       delay(1000);
       currentState = AM_MovingForward; 
     if(!digitalRead(PushButtonOFF))
          currentState = Idle;
            
    break;}
}
  
  
}

but when i give the IR a value it move to it and dose not exit

Please exaplin what this means? What is it supposed to exit from and does not do?


[/quote]

marco_c:
Please exaplin what this means? What is it supposed to exit from and does not do?

sorry if i was not clear.

what i mean is when i press the button to move forward it move but when i press another button lets say to go back it dose not response it keep moving forward.

Factoring out the common code in your state machine makes the code more readable and easier to modify:

// Note: analogWrite(p, 0) == digitalWrite(p, LOW) and analogWrite(p, 255) == digitalWrite(p, HIGH)
void ControlMotors(byte L1, byte L2, byte R1, byte R2) {
  analogWrite(ControlMotorLeft1, L1);
  analogWrite(ControlMotorLeft2, L2);
  analogWrite(ControlMotorRight1, R1);
  analogWrite(ControlMotorRight2, R2);
}

void IRInputCheck() {
  if (irrecv.decode(&results)) {
    switch (results.value) {
      case BUTTON_front:
        currentState = MovingForward;
        ControlMotors(150, 0, 150, 0);
        break;

      case BUTTON_back:
        currentState = Backward;
        break;

      case BUTTON_right:
        currentState = Turnright;
        break;

      case BUTTON_left:
        currentState = Turnleft;
        break;

      case BUTTON_speedup:
        if (currentState == MovingForward) {
          analogWrite(ControlMotorLeft1, 250);
          analogWrite(ControlMotorRight1, 250);
        }
        break;

      case BUTTON_speeddown:
        if (currentState == MovingForward) {
          digitalWrite(ControlMotorLeft1, 100);
          analogWrite(ControlMotorRight1, 100);
        }
        break;
    } // End: switch
    irrecv.resume();
  }  // End: if decode

}

void loop() {


  if (currentState == Idle)
    digitalWrite(TheLED, LOW);
  else {
    digitalWrite(TheLED, HIGH);
    IRInputCheck();  // Only allow IR control if not Idle
  }

  // Pushbuttons override Idle, manual and automatic behaviors
  if (!digitalRead(PushButtonOFF))
    currentState = Idle;

  if (!digitalRead(PushButtonAUTO))
    currentState = AM_MovingForward;

  switch (currentState) {
    case Idle:
      Serial.println("Idle");
      ControlMotors(0, 0, 0, 0);
      if (!digitalRead(PushButtonON))
        currentState = rest;
      break;

    case rest:
      Serial.println("Rest");
      ControlMotors(0, 0, 0, 0);
      break;

    case MovingForward:
      Serial.println("Moving Forward");
      ControlMotors(150, 0, 150, 0);
      if (digitalRead(IRS) == HIGH)
        currentState = Backward ;
      break;

    case Backward:
      Serial.println("Backward");
      ControlMotors(0, 255, 0, 255);
      if (digitalRead(IRS) == HIGH)
        currentState = MovingForward ;
      break;

    case Turnleft:
      Serial.println("Turn left");
      ControlMotors(0, 255, 255, 0);
      break;

    case Turnright:
      Serial.println("Rest");
      ControlMotors(255, 0, 0, 255);
      break;

    case AM_MovingForward:
      Serial.println("Auto Mode");
      ControlMotors(255, 0, 255, 0);
      digitalWrite(ControlMotorLeft1, HIGH);
      if (digitalRead(IRS) == HIGH)
        currentState = AM_Backward ;

      //        if (sonar.ping_cm() <= 8)
      //          currentState = AM_Backward ;

      break;

    case AM_Backward:
      Serial.println("Backward");
      ControlMotors(0, 255, 0, 255);
      delay(1000);
      currentState = Turn;
      break;

    case Turn:
      Serial.println("Turn");
      ControlMotors(255, 0, 0, 255);
      delay(1000);
      currentState = AM_MovingForward;
      break;
  }
}

OMG.....i can,t say thank enough the code looks clean and it is working fine

johnwasser:
Factoring out the common code in your state machine makes the code more readable and easier to modify:

now i tested the code with out powering the motor and it work fine but when i plug the motor when i press the button it change the state but it dose not exit it(the IR stop lighting up) maybe it is a power problem?

BTW i am using l293d to connect the motors

update:i did change the motor output in every state to(0,0,0,0) which is OFF and the IR worked fine so the problem appear when one of the motor go high so i guess it is power problem,but i don't know why the motor work connected to the external battery and the sensors connected to the vcc.

any suggestion??

Please stop starting new threads on the same topic in different parts of the forum.

Duplicates deleted.