Can't figure out how to control servo with IR remote

Hello!

I just got the elegoo arduino kit for Christmas, so I'm about as new as they come to this. I've been trying for days to figure out how to code my Arduino to have a servo that is controlled by an IR remote so that I can turn my light switch on and off! I already did trial and error printing out the servo mount, but in between I've been stressing about my code and was wondering if anyone could help me out with this.

I'm on arduino IDE ver. 2.3.7
IRremote Library version 4.5.0
Servo Library version 1.3.0

Here's the code, I'm sorry if it looks really sloppy :/

#include <IRremote.h>
#include <Servo.h>

int IRpin = 12; //pin for IR sensor

IRrecv irrecv(IRpin);

Servo servoone; 

decode_results results; 

void setup() {
  Serial.begin(9600);
  irrecv.enableIRIn();
  servoone.attach(8);
}

void loop() {
if (irrecv.decode(&results))
{
irrecv.resume();
}
if (results.value = 16736925) //vol+
{
  servoone.write(0);
delay (15);
}
if (results.value = 16754775) //vol-
{
  servoone.write(180);
  delay (15);
}
}


error code:

Sketch uses 7124 bytes (22%) of program storage space. Maximum is 32256 bytes
Global variables use 794 bytes (38%) of dynamic memory, leaving 1254 bytes for local variables. Maximum is 2048 bytes.
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0xac
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 2 of 10: not in sync: resp=0xac
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 3 of 10: not in sync: resp=0xac
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 4 of 10: not in sync: resp=0xac
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 5 of 10: not in sync: resp=0xac
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 6 of 10: not in sync: resp=0xac
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 7 of 10: not in sync: resp=0xac
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 8 of 10: not in sync: resp=0xac
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 9 of 10: not in sync: resp=0xac
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 10 of 10: not in sync: resp=0xac
Failed uploading: uploading error: exit status 1

Reading the IR is one task.

Operating the servo is a different task.

Solve those matters one at the time. Then combine the 2 solutions.

I'm stuck trying to figure out why this code isn't working in the serial monitor to display the values of the buttons pressed on my remote


#include <IRremote.h>
int IRpin = 11;
IRrecv irrecv(IRpin);
decode_results results;

void setup(){
  Serial.begin(9600); 
  irrecv.enableIRIn(); // Start the receiver
  }
  
  void loop() { 
    if (irrecv.decode(&results))    {
           Serial.println(results.value, DEC); // Print the Serial 'results.value'     
           irrecv.resume();   // Receive the next value   
           }
           }

Which Arduino (there are more than 20 different Arduinos)?
Which operating system does your computer use (Windows, Mac, Linux)?

  • Be careful
    = is not the same as ==

if (results.value == 16736925) 

A = B takes the value from B and puts it into A.

A == B checks to see if A and B have the same value

3 Likes

Sorry I didn't even think to mention this! I'm on windows and I'm using th arduino uno r3

ohhh okay that helps a ton thank you! i'll tweak it and try that out

it still isn't working unfortunately

The IR code is too old for the IR remote version used.

Take a look at the following:

An example to try out...

#include "Servo.h"

#define DECODE_NEC            // DECODE_NEC
#include <IRremote.hpp>       // Do not change header order.

//
// Global constants
//
constexpr uint8_t irRecvPin {2};
constexpr uint8_t servoPin {5};
constexpr int servoPos[] {0, 90, 180};

//
// Global objects / variables
//
enum class Index : uint8_t {down, mid, up};
Servo servo;

//
// Function(s)
//
uint16_t irReceive() {
  uint16_t received{0};
  if (IrReceiver.decode()) {
    IrReceiver.printIRResultShort(&Serial);
    if (IrReceiver.decodedIRData.protocol == UNKNOWN) {
      // We have an unknown protocol here, print more info
      IrReceiver.printIRResultRawFormatted(&Serial, true);
    }
    if (IrReceiver.decodedIRData.protocol == NEC) {
      received = IrReceiver.decodedIRData.command;
      Serial.print("Command: 0x");
      Serial.println(received, HEX);
    }
    IrReceiver.resume();
  }
  return received;
}

//
// Main
//
void setup()
{
  Serial.begin(115200);
  IrReceiver.begin(irRecvPin);
  Serial.print(F("Ready to receive IR signals at pin "));
  Serial.println(irRecvPin);
  servo.attach(servoPin);
  servo.write(servoPos[static_cast<uint8_t>(Index::mid)]);
}

void loop()
{
   switch(irReceive()) {
    case 0x90: servo.write(servoPos[static_cast<uint8_t>(Index::up)]); break;
    case 0xA8: servo.write(servoPos[static_cast<uint8_t>(Index::mid)]); break;
    case 0xE0: servo.write(servoPos[static_cast<uint8_t>(Index::down)]); break;
   }
}

i'm sorry i'm very new to coding, what does it mean when you commented this?
The code actually shows values of the buttons i'm pressing in the monitor so that's a good sign!

Okay so I found the values of the up button (0x46) and the down button (0x15), and i tried replacing this line

with

case 0x46: servo.write(servoPos[static_cast<uint8_t>(Index::up)]); break;```


but it just made the servo move continuously once i pressed that button so I had to unplug it.

is there any way to make it so that it's a command that disables once i let go of the button? something like "the servo will move when the button is held and stop when it's released?"

  • Please explain this more fully.

I was thinking a function that does something along the lines of "when button X is held, servo will move, when button X is released servo stops"

  • There are basically two types of servos:
    Type 1, the motor goes to an angle from 0° to 180° then stops when the angle is reached.
    Type 2, (continuous) the motor is stopped when told to go to 90° and CW when the angle is > 90°, CCW when the angle is < 90°.

Which type do you have ?

I believe it's the type 1. I assumed that there would be a way to stop the motor as soon as the button is released.

  • Please show us images of your circuit wiring and the remote control.