Good day all, this code “should” call 4 functions forward, left right etc… My problem is that whichever function I call first is the only function that runs. I have tried putting return at the end of each function to no avail. it seems too simple to go wrong, but there you have it.
Any insights greatly appreciated. This is running on an Arduino uno and DFRrobotshop rovershield v1
#include <IRremote.h> // Include the IRremote library
const int IR_RECEIVER_PIN = 9; // Define the pin connected to the IR receiver
IRrecv irrecv(IR_RECEIVER_PIN); // Create an IRrecv object
decode_results results; // Create a decode_results variable to store IR data
// Define motor control pins (adjust based on your Rover Shield connections)
const int E1 = 6; // Motor 1 speed control
const int E2 = 5; // Motor 2 speed control
const int M1 = 8; // Motor 1 direction control
const int M2 = 7; // Motor 2 direction control
void setup() {
Serial.begin(9600); // Initialize serial communication
irrecv.enableIRIn(); // Enable IR receiver
// Set motor control pins as outputs
pinMode(E1, OUTPUT);
pinMode(E2, OUTPUT);
pinMode(M1, OUTPUT);
pinMode(M2, OUTPUT);
}
void loop() {
if (irrecv.decode(&results)) { // Check if IR signal received
switch (results.value) { // Perform actions based on IR code
case 0xE0E006F9: // IR code for "Forward"
moveForward();
break;
case 0xE0E08679: // IR code for "Backward"
moveBackward();
break;
case 0xE0E0A659: // IR code for "Left"
turnLeft();
break;
case 0xE0E046B9: // IR code for "Right"
turnRight();
break;
// Add more cases for other IR commands as needed
}
irrecv.resume(); // Prepare to receive the next IR signal
}
}
// Motor control functions
void moveForward() {
digitalWrite(M1, HIGH); // Set motor 1 direction
digitalWrite(M2, HIGH); // Set motor 2 direction
analogWrite(E1, 255); // Set motor 1 speed
analogWrite(E2, 255); // Set motor 2 speed
}
void moveBackward() {
digitalWrite(M1, LOW); // Set motor 1 direction
digitalWrite(M2, LOW); // Set motor 2 direction
analogWrite(E1, 255); // Set motor 1 speed
analogWrite(E2, 255); // Set motor 2 speed
}
void turnLeft() {
digitalWrite(M1, HIGH); // Set motor 1 direction
digitalWrite(M2, LOW); // Set motor 2 direction
analogWrite(E1, 255); // Set motor 1 speed
analogWrite(E2, 255); // Set motor 2 speed
}
void turnRight() {
digitalWrite(M1, LOW); // Set motor 1 direction
digitalWrite(M2, HIGH); // Set motor 2 direction
analogWrite(E1, 255); // Set motor 1 speed
analogWrite(E2, 255); // Set motor 2 speed
}