Servo isnt responding to ir signals in automatic mode pleeease help me urgently i am a newbie

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;
}

add Serial.print(receivedCode) to see what you really receive when using that button.

This delay might be interfering with the parsing of the IR.

I don't see a manual mode procedure, and there is no code in the automatic mode to switch back to manual.

It relies on parsing an IR message to switch modes:

If the delay() in automatic slows down the IR parsing so that it doesn't receive a good code, then it won't be able to switch out.

Maybe instead of the delay(30) in automatic, wrap it in some rate-limiting code like:

void rateLimitedThing() {
  const unsigned long interval = 30;
  static unsigned long lastMs = 0;
  unsigned long now = millis();
  if (now - lastMs >= interval) {
    lastMs = now; // or lastMs += interval;
    Serial.print(".");
  }
}

Or more explictly:

void rateLimitedThing() {
  const unsigned long interval = 30;
  static unsigned long lastMs = 0;
  unsigned long now = millis();
  if (now - lastMs >= interval) {
    lastMs = now; // or lastMs += interval;

    // 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);
    // Use millis()-based rate-limiting instead of:
    //delay(30); // Short delay for smooth movement 

    distance = calculateDistance();
    Serial.print(servoPosition);
    Serial.print(",");
    Serial.print(distance);
    Serial.print(".");
  }
}

Also, increase the Serial.begin(9600); to modern 115200 baud.

umm what does this code do ? as i told im a newbie

I tried it still is not working

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.

Try:

Here's a toy demonstrating how the different methods of slowing code down affects the ability for the code to do other things:

Thanks A lot it solved my problem

Please post your fixed code, so it has a chance of solving someone else's problem in the future.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.