When I press a button on my IR remote and it makes my motors run, the Reciever keeps on flashing the red light and won't let me input any other button. What I currently want to make happen is the motors to run foward on a button press, and then stop on a different button press. Here is my code below:
#include <IRremote.h>
// Motor Pins
const int motorPin1 = 5;
const int motorPin2 = 6;
const int motorPin3 = 10;
const int motorPin4 = 9;
// Sensor Pins
const int RECV_PIN = 11;
const int trigPin = 12;
const int echoPin = 8;
const int buzzerPin = 13;
const int soundSensorPin = 7;
// IR Receiver
IRrecv irrecv(RECV_PIN);
decode_results results;
// Threshold for obstacle detection
const int DISTANCE_THRESHOLD = 20; // Distance in cm to stop from obstacle
void setup() {
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(soundSensorPin, INPUT);
irrecv.enableIRIn(); // Start the IR receiver
Serial.begin(9600); // Start the serial monitor for debugging
}
void loop() {
long distance = getDistance(); // Measure distance in cm using pulseIn()
Serial.print("Distance: ");
Serial.println(distance); // Print distance in the Serial Monitor
// Stop motors if an obstacle is within the threshold
if (distance > 0 && distance < DISTANCE_THRESHOLD) {
Serial.println("Obstacle detected, stopping motors.");
stopMotors();
delay(1000); // Short delay to avoid continuous stopping
} else if (irrecv.decode(&results)) {
Serial.print("IR Code: ");
Serial.println(results.value, HEX); // Print the IR code received
// Handle the different remote button presses
switch (results.value) {
case 0x1B103CA82: // Forward code
Serial.println("Moving forward");
moveForward();
break;
case 0x2F807FF: // Reverse code
Serial.println("Moving backward");
moveBackward();
break;
case 0x3BB44FF: // Left code
Serial.println("Turning left");
turnLeft();
break;
case 0x4BC43FF: // Right code
Serial.println("Turning right");
turnRight();
break;
case 0x25AE7EE0: // Stop code
Serial.println("Stop button pressed");
stopMotors();
break;
case 0x6F609FF: // Beep code (example, replace with your buzzer button's code)
Serial.println("Beep button pressed");
buzzerBeep(); // Only beeps when this button is pressed
break;
default:
Serial.println("Unknown IR code, not beeping");
// Optionally stop the buzzer if it's on (in case of incorrect signal)
digitalWrite(buzzerPin, LOW);
break;
}
irrecv.resume(); // Receive the next value
}
}
// Motor Control Functions
void moveForward() {
Serial.println("Moving Forward");
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
}
void moveBackward() {
Serial.println("Moving Backward");
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
}
void turnLeft() {
Serial.println("Turning Left");
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
}
void turnRight() {
Serial.println("Turning Right");
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
}
void stopMotors() {
Serial.println("Stopping Motors");
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
}
// Buzzer beep function
void buzzerBeep() {
digitalWrite(buzzerPin, HIGH); // Turn on the buzzer
delay(100); // Beep duration (100ms)
digitalWrite(buzzerPin, LOW); // Turn off the buzzer
}
// Function to calculate distance using pulseIn()
long getDistance() {
// Send a 10us pulse to trigger the sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Measure the pulse duration from the echo pin
long duration = pulseIn(echoPin, HIGH);
// Calculate the distance in centimeters
long distance = duration * 0.0344 / 2;
return distance;
}