My project is a sonar/radar project involving 1 servo motor (180 degree rotation) , an ir receiver , a remote, ultrasonic sensor. THERE are 2 modes - automatic and manual modes in automatic mode the servo rotates automatically but i manual mode it rotate 10 degrees when right button is pressed in right direction and same applies to left on the remote and the problem is when in automatic mode it isnt switching back to manual mode
heres the code
#include <Servo.h>
#include <IRremote.hpp>
const int trigPin = 8;
const int echoPin = 9;
const int IR_RECEIVE_PIN = 7; // Pin connected to the IR receiver
// Define the IR remote button codes (replace with actual values)
const unsigned long buttonLeft = 0xF708FF00; // Replace with your remote's Left button code
const unsigned long buttonRight = 0xA55AFF00; // Replace with your remote's Right button code
const unsigned long buttonAutomatic = 0xE31CFF00; // Replace with your remote's Automatic button code
const unsigned long buttonManual = 0xFF22DD00; // Replace with your remote's Manual button code
long duration;
int distance;
Servo myServo; // Object servo
bool automaticMode = false; // Start in manual mode
int servoPosition = 90; // Initial servo position (middle)
int step = 1; // Servo movement step
void setup() {
pinMode(trigPin, OUTPUT); // trigPin as an Output
pinMode(echoPin, INPUT); // echoPin as an Input
Serial.begin(9600);
myServo.attach(10); // Pin Connected To Servo
myServo.write(servoPosition); // Set initial servo position
IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK); // Start the receiver
}
void loop() {
if (IrReceiver.decode()) {
unsigned long receivedCode = IrReceiver.decodedIRData.decodedRawData;
if (receivedCode == buttonLeft && !automaticMode) {
if (servoPosition > 15) {
servoPosition -= 10; // Move servo 10 degrees to the left
myServo.write(servoPosition);
}
} else if (receivedCode == buttonRight && !automaticMode) {
if (servoPosition < 165) {
servoPosition += 10; // Move servo 10 degrees to the right
myServo.write(servoPosition);
}
} else if (receivedCode == buttonAutomatic) {
automaticMode = true; // Enter automatic mode
} else if (receivedCode == buttonManual) {
automaticMode = false; // Exit automatic mode
}
IrReceiver.resume(); // Enable receiving of the next value
}
if (automaticMode) {
// Rotating servo automatically from 15 to 165 degrees
servoPosition += step;
if (servoPosition >= 165 || servoPosition <= 15) {
step = -step; // Reverse direction at the limits
}
myServo.write(servoPosition);
delay(30); // Short delay for smooth movement
distance = calculateDistance();
Serial.print(servoPosition);
Serial.print(",");
Serial.print(distance);
Serial.print(".");
}
}
int calculateDistance() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
return distance;
}
Hello @sharwilstechexperiments - The suggestion by @kmin was to discover the de-coded signal sent by pressing a button on your IR Remote. Show those numbers here. It will help in finding the problem.
In general, saying "not working" does not convey information needed. Please, be more descriptive.
Do you write your code? Do you understand what it does?
I do not have your remote, or your hardware, so I'm a newbie with your setup. The best I can do is look at your code and guess what it does for you based on your descriptions.
The best I can figure from looking at your code, is that I think when it is in automatic mode, each time it goes through the loop(), it hits the delay(30) and pauses for 30 milliseconds, which prevents the IrReceiver.decode() from accurately decoding signals from your remote control. I would bet that if you print out the signals, (maybe add Serial.println(receivedCode ,HEX); after you decode them ?) you would get some unexpected codes.
My suggestion is to eliminate the delay(30) by using the tricks in the BlinkWithoutDelay example and I wrote a snippet of the trick in a couple functions to illustrate how that might be done.