Hello, I am experimenting with the Elegoo Smart Car 4.0 and I want to be able to record button presses (so which button and the duration untill next button press), memorise its path, and with another button execute it's path. While it drives fine, it doesn't seem to even start recording as none of my messages seem to show up on the serial motor (but the hex codes do) is there any way to improve this code?
#include <IRremote.h>
// Motor driver pin configuration
#define PWMA 5 // Controls power to right motor
#define PWMB 6 // Controls power to left motor
#define AIN 7 // Controls direction of right motor, HIGH = FORWARD, LOW = REVERSE
#define BIN 8 // Controls direction of right motor, HIGH = FORWARD, LOW = REVERSE
#define STBY 3 // Place H-Bridge in standby if LOW, Run if HIGH
// #define resetPin A0
int path[50]; // Store path as an array
int pathIndex = 0; // Index to keep track of the current step in the path
bool recording = false; // Indicates whether we are recording a path
// IR remote setup
int RECV_PIN = 9; // IR receiver pin
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup() {
// Motor pins setup
pinMode(PWMA, OUTPUT); //set IO pin mode OUTPUT
pinMode(PWMB, OUTPUT);
pinMode(BIN, OUTPUT);
pinMode(AIN, OUTPUT);
pinMode(STBY, OUTPUT);
digitalWrite(STBY, HIGH); //Enable Motors to run
digitalWrite(PWMA, LOW); // Fully on
// digitalWrite(PWMA, HIGH); // Fully on
// Start the IR receiver
Serial.begin(9600);
irrecv.enableIRIn();
}
void loop() {
if (irrecv.decode(&results)) {
int value = results.value;
// Print the received IR code to the serial monitor
Serial.println(results.value, HEX);
// Handle the IR remote buttons
if (value == 0xFF6897) {
if (!recording) {
// Start recording
recording = true;
pathIndex = 0;
Serial.println("Recording started.");
} else {
// Stop recording
recording = false;
Serial.println("Recording stopped.");
}
delay(500); // Debounce the button
}
if (recording && pathIndex < 50) {
// Store the received IR code in the recorded path
path[pathIndex] = value;
pathIndex++;
Serial.print("Recorded");
Serial.println(value, HEX);
delay(500); // Delay to prevent recording multiple actions in one press
}
if (value == 0xFF9867) { // Replace with your playback button code
Serial.println("Playback started.");
executePath();
delay(500); // Debounce the button
} else {
switch (results.value) {
case 0xFF629D: // Forward
moveForward();
break;
case 0xFFA857: // Backward
moveBackward();
break;
case 0xFF22DD: // Left
turnLeft();
break;
case 0xFFC23D: // Right
turnRight();
break;
case 0xFF02FD: // Stop
stopCar();
break;
default:
// Unknown code; do nothing
break;
}
}
irrecv.resume(); // Receive the next value
}
}
// Functions to store and recall path
void executePath() {
for (int i = 0; i < pathIndex; i++) {
int value = path[i];
switch (value) {
case 0xFF629D:
moveForward();
break;
case 0xFFA857:
moveBackward();
break;
case 0xFF22DD:
turnLeft();
break;
case 0xFFC23D:
turnRight();
break;
case 0xFF02FD:
stopCar();
break;
default:
// Handle unknown path steps
break;
}
delay(1000); // Delay between transmissions (adjust as needed)
}
irrecv.resume(); // Receive the next value
}
// Functions to control the car's movement
void moveForward() {
digitalWrite(AIN, HIGH); // Forward direction
analogWrite(PWMA, 120); // Full power
digitalWrite(BIN, HIGH); // Forward direction
analogWrite(PWMB, 120); // Full power
}
void moveBackward() {
digitalWrite(AIN, LOW); // Backwards direction
analogWrite(PWMA, 120); // Full power
digitalWrite(BIN, LOW); // Backwards direction
analogWrite(PWMB, 120); // Full power
}
void turnLeft() {
digitalWrite(AIN, HIGH); // Backwards direction
analogWrite(PWMA, 60); // Full power
digitalWrite(PWMB, LOW);
}
void turnRight() {
digitalWrite(PWMA, LOW); // No power on Right
digitalWrite(BIN, HIGH); // Forward direction
analogWrite(PWMB, 60); // Full power
}
void stopCar() {
digitalWrite(PWMA, LOW); // No power on Right
digitalWrite(PWMB, LOW); // No power on Left
}
/* void resetArduino() {
pinMode(resetPin, OUTPUT);
digitalWrite(resetPin, HIGH);
} */