IR controlled arduino uno car

Need some help with the code for my ir remote controlled arduino car. I have to press one button to make the car drive in a box pattern 5 feet by 5 feet. The amount of times the car drives in the box pattern depends on the amount of times the one button was pressed at the start of the mission.

Here is my code so far:


enum MISSION_PHASE {
    CONFIG, WAIT, FIRST, SECOND, THIRD, FOURTH, STOP
};

#include <Servo.h>
#include <IRremote.h>

#define RED_LED 13
#define GREEN_LED 12
#define IR   2
#define SERVO 11
#define TRUE 1
#define FALSE 0
#define LEFT_MOTOR_EN 5          // must be a PWM compatible pin
#define RIGHT_MOTOR_EN 6         // must be a PWM compatible pin
#define LEFT_MOTOR_I1 7
#define LEFT_MOTOR_I2 8
#define RIGHT_MOTOR_I1 9
#define RIGHT_MOTOR_I2 10

#define LEG_THRESHOLD 5000    // number of milli-seconds for each leg in this example

Servo myservo;
IRrecv irrecv(IR);
decode_results results;

int token;    // keep track of which part of the mission the vehicle is
int cmdLeftMotor, cmdRightMotor, cmdServo;  // in terms of duty cycles
long timeElapse, timeOrigin;                // in terms of time ticks

int IRPresence;     //  1: yes, 2: no
int boxDir;         // box direction 0: clockwise; 1: anti-clockwise
int numLoops;       // number of boxes in the mission

////////////////////// Function Prototyping //////////////////////

void SetUpLEDs();
void TurnOnRedLED();
void TurnOffRedLED();
void TurnOnGreenLED();
void TurnOffGreenLED();

void SetUpServo();
void SetUpDCMotors();
void StopDCMotors();

void SetUpIR();

void SetTimeOrigin();
void GetTimeElapse();

void CheckIRPresence();

void UpdateMotorCommands();  

//////////////////////  Function Definitions //////////////////////

void SetUpLEDs() {
  pinMode(GREEN_LED, OUTPUT);
  pinMode(RED_LED, OUTPUT);
}

void TurnOnGreenLED(){
  digitalWrite(GREEN_LED, HIGH);
}

void TurnOffGreenLED(){
  digitalWrite(GREEN_LED, LOW);
}

void TurnOnRedLED(){
  digitalWrite(RED_LED, HIGH);
}

void TurnOffRedLED(){
  digitalWrite(RED_LED, LOW);
}

void SetUpServo() {
    myservo.attach(SERVO);
}

void SetUpDCMotors() {
  analogWrite(LEFT_MOTOR_EN, 0);
  analogWrite(RIGHT_MOTOR_EN, 0);
}

void SetUpIR() {
    irrecv.enableIRIn(); // start the receiver
}

void StopDCMotors() {
  analogWrite(LEFT_MOTOR_EN, 0);
  analogWrite(RIGHT_MOTOR_EN, 0);
}
void CheckIRPresence() {}
void UpdateMotorCommands() {}

void SetTimeOrigin() {
  timeOrigin = millis();
  timeElapse = timeOrigin;
}

void GetTimeElapse() {
  timeElapse = millis() - timeElapse;
  
}

void setup() {
  // put your setup code here, to run once:
  SetUpLEDs();
  SetUpServo();
  SetUpDCMotors();
  SetUpIR();  
  TurnOffRedLED();
  TurnOffGreenLED();
  SetTimeOrigin();

  Serial.begin(9600);

  token = CONFIG; // the starting part is to configure all sensors/actuators
}

void loop() {
  // put your main code here, to run repeatedly:
  switch (token) {
           
      case CONFIG:

          SetUpLEDs();
          SetUpServo();
          SetUpDCMotors();
          SetUpIR();
          TurnOffGreenLED();
          TurnOffRedLED();

          token = WAIT;
          break;
        
     case WAIT:
      // do what is needed
      //test if the condition is met before breaking out of this case

          CheckIRPresence();
          if (IRPresence == TRUE) {
            token = FIRST;
            TurnOnGreenLED();
            SetTimeOrigin();
          }
          break;

      case FIRST:
       // do what is needed
      //test if the condition is met before breaking out of this case

        UpdateMotorCommands();
        GetTimeElapse();
        if (timeElapse > LEG_THRESHOLD) {
          token = SECOND;
        }
        break;
        
      case SECOND:
       // do what is needed
      //test if the condition is met before breaking out of this case

        UpdateMotorCommands();
        GetTimeElapse();
        if (timeElapse > LEG_THRESHOLD) {
          token = THIRD;
        }
        break;
        
      case THIRD:
       // do what is needed
      //test if the condition is met before breaking out of this case

        UpdateMotorCommands();
        GetTimeElapse();
        if (timeElapse > LEG_THRESHOLD) {
          token = FOURTH;
        }
        break;
        
      case FOURTH:
       // do what is needed
      //test if the condition is met before breaking out of this case

        UpdateMotorCommands();
        GetTimeElapse();
        if (timeElapse > LEG_THRESHOLD) 
          token = STOP;
          
        break;
        
      case STOP:
       // do what is needed
      //test if the condition is met before breaking out of this case
          StopDCMotors();
          TurnOffGreenLED();
          TurnOnRedLED();
          break;
  }
  delay(1000);
}

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Please go back and format the code and put the code in your original post into code tags so that it is easier to copy to the IDE or text editor for examination.

What version of the IRremote library are you using?

That is code for the older (<3.0> version of the IRremote library. Code written for the old version may not work properly with the new library version. See the IRremote GitHub page for more information.

I know this library will work. I just need help on the button part

Fix the code and I will look at it.

I believe i fixed it

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.