Hello folks,
I'm trying to use an IR remote control to activate and deactivate my robot car, but for some reason after hitting the buttons, only the servo for ultrasonic sensor responds, the DC motors don't move at all, no matter on or off. Much appreciated if somebody can help me out with my code. Thanks!
#include <AFMotor.h> // Library for Adafruit Motor Shield v1
#include <NewPing.h>
#include <Servo.h>
#include <IRremote.h> // Include the IR remote library
#define TRIG_PIN A0
#define ECHO_PIN A1
#define IR_RECEIVE 16 // Define the IR receiver pin (change to your actual connection pin)
#define MAX_DISTANCE 200
#define MAX_SPEED 190 // sets speed of DC motors
// Button codes for the remote (replace with actual values if different)
#define START_BUTTON 0xCE32D5BB // Example code for "Start" (button "5")
#define STOP_BUTTON 0x4625FB32 // Example code for "Stop" (button "2")
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE);
AF_DCMotor motor1(1, MOTOR12_1KHZ); // Connect motor1 to port M1
AF_DCMotor motor2(2, MOTOR12_1KHZ); // Connect motor2 to port M2
AF_DCMotor motor3(3, MOTOR34_1KHZ); // Connect motor3 to port M3
AF_DCMotor motor4(4, MOTOR34_1KHZ); // Connect motor4 to port M4
Servo myservo;
boolean goesForward = false;
boolean isActive = false; // Variable to track if the robot is active or not
int distance = 100;
int speedSet = 0;
IRrecv irrecv(IR_RECEIVE); // Create an IR receiver object
decode_results results; // Variable to store IR results
void setup() {
pinMode(IR_RECEIVE, INPUT);
Serial.begin(9600); // Start serial communication for debugging
myservo.attach(10);
myservo.write(115);
delay(2000);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
irrecv.enableIRIn(); // Start the IR receiver
}
void loop() {
if (irrecv.decode(&results)) { // Check if there's an IR signal
switch(results.value) {
case START_BUTTON: // If the "Start" button is pressed
isActive = true;
Serial.println(results.value, HEX);
break;
case STOP_BUTTON: // If the "Stop" button is pressed
isActive = false;
Serial.println(results.value, HEX);
moveStop(); // Stop all motors when deactivated
break;
}
irrecv.resume(); // Receive the next value
}
if (isActive) { // Only run the algorithm if isActive is true
int distanceR = 0;
int distanceL = 0;
delay(40);
if (distance <= 15) {
moveStop();
delay(100);
distanceR = lookRight();
delay(200);
distanceL = lookLeft();
delay(200);
// Determine the clearest path
if (distanceR >= distanceL && distanceR > 15) {
turnRight();
} else if (distanceL > distanceR && distanceL > 15) {
turnLeft();
} else {
// Both sides are blocked, perform a short backward movement
moveBackward();
delay(200); // Minimal backward movement
}
} else {
moveForward();
}
distance = readPing();
}
}
int lookRight() {
myservo.write(50);
delay(500);
int distance = readPing();
delay(100);
myservo.write(115);
return distance;
}
int lookLeft() {
myservo.write(170);
delay(500);
int distance = readPing();
delay(100);
myservo.write(115);
return distance;
}
int readPing() {
delay(70);
int cm = sonar.ping_cm();
if (cm == 0) {
cm = 250;
}
return cm;
}
void moveStop() {
motor1.run(RELEASE);
motor2.run(RELEASE);
motor3.run(RELEASE);
motor4.run(RELEASE);
}
void moveForward() {
if (!goesForward) {
goesForward = true;
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
for (speedSet = 0; speedSet < MAX_SPEED; speedSet += 2) { // slowly bring the speed up to avoid loading down the batteries too quickly
motor1.setSpeed(speedSet);
motor2.setSpeed(speedSet);
motor3.setSpeed(speedSet);
motor4.setSpeed(speedSet);
delay(5);
}
}
}
void moveBackward() {
goesForward = false;
motor1.run(BACKWARD);
motor2.run(BACKWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
for (speedSet = 0; speedSet < MAX_SPEED / 2; speedSet += 2) { // limit speed for backward movement
motor1.setSpeed(speedSet);
motor2.setSpeed(speedSet);
motor3.setSpeed(speedSet);
motor4.setSpeed(speedSet);
delay(5);
}
}
void turnRight() {
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
delay(500);
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
}
void turnLeft() {
motor1.run(BACKWARD);
motor2.run(BACKWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
delay(500);
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
}