I am writing code for a boat to autonomously complete a mission. I'm using a remote to start the mission. I would like to be able to stop the 'if' statement if the boat isn't running correctly by pressing a different button on the remote so I don't have to wait for the entire statement to be completed. I don't know which command to use or where to even begin for this.
Thank you!!
#include <IRremote.h>
#include <Servo.h>
Servo myservo;
#define ONE 0xFF30CF
#define ZERO 0xFF6897
#define LED 3 //LED Pin 3
#define E1 4 //Enable A Pin 4 (RIGHT MOTOR)
#define I1 5 //In 1 Pin 5
#define I2 6 //In 2 Pin 6
#define E2 7 //Enable B Pin 7 (LEFT MOTOR)
#define I3 8 //In 3 Pin 8
#define I4 9 //In 4 Pin 9
#define SERVO 10 //Servo Pin 10
//#define NEUTRAL 270 //Neutral Servo Position at 270 degrees
int RECV_PIN = 2; //IR Receiver Pin 2
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
irrecv.enableIRIn(); // start the receiver
myservo.attach(SERVO);
pinMode(LED, OUTPUT);
pinMode(E1, OUTPUT);
pinMode(I1, OUTPUT);
pinMode(I2, OUTPUT);
pinMode(E2, OUTPUT);
pinMode(I3, OUTPUT);
pinMode(I4, OUTPUT);
digitalWrite(LED, LOW);
analogWrite(E1, 0);
analogWrite(E2, 0);
myservo.write(0);
}
void loop() {
// put your main code here, to run repeatedly:
if(irrecv.decode(&results)) {
if (results.value == ONE) {
digitalWrite(I1, LOW); // Forward: I1 LOW, I2 HIGH, I3 LOW, I4 HIGH
digitalWrite(I2, HIGH); // Reverse: I1 HIGH, I2 LOW, I3 HIGH, I4 LOW
digitalWrite(I3, LOW);
digitalWrite(I4, HIGH);
digitalWrite(LED, HIGH); // Begin testing all functions
delay(1000);
digitalWrite(LED, LOW);
delay(500);
analogWrite(E1, 200);
delay(500);
analogWrite(E1, 0);
delay(500);
analogWrite(E2, 200);
delay(500);
analogWrite(E2, 0);
delay(500);
myservo.write(90);
delay(500);
myservo.write(0); // End testing all functions
delay(2000);
digitalWrite(LED, HIGH);
analogWrite(E1, 255);
analogWrite(E2, 255);
delay(1500);
analogWrite(E1, 0);
analogWrite(E2, 0);
delay(500);
digitalWrite(I1, HIGH);
digitalWrite(I2, LOW);
digitalWrite(I3, HIGH);
digitalWrite(I4, LOW);
analogWrite(E1, 200);
analogWrite(E2, 200);
delay(1500);
analogWrite(E1, 0);
analogWrite(E2, 0);
myservo.write(140);
digitalWrite(LED, LOW);
}
else if (results.value == ZERO) {
myservo.write(0);
digitalWrite(LED, LOW);
analogWrite(E1, 0);
analogWrite(E2, 0);
}
irrecv.resume();
}
delay(200);
}