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);
}