Controlling two servos with an ir remote

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

#define IR_RECEIVE_PIN 9 // IR receiver connected to pin 9

Servo servo1, servo2;
int servo1Pin = 3; // Servo 1 on Pin 3
int servo2Pin = 5; // Servo 2 on Pin 5

// :small_blue_diamond: IR Codes (Your Previously Found Values)
#define UP 0xB946FF00 // Move Forward
#define DOWN 0xEA15FF00 // Move Backward
#define LEFT 0xBB44FF00 // Turn Left
#define RIGHT 0xBC43FF00 // Turn Right
#define REPEAT_SIGNAL 0xFFFFFFFF // Holding button repeat signal

uint32_t lastCommand = 0; // Store last valid command
int servo1_d = 90; // Servo 1 default position
int servo2_d = 90; // Servo 2 default position

unsigned long lastMoveTime = 0; // Track time for smooth movement

IRrecv irrecv(IR_RECEIVE_PIN);
decode_results results;

void setup() {
Serial.begin(9600);
irrecv.enableIRIn(); // Start the IR receiver

servo1.attach(servo1Pin);
servo2.attach(servo2Pin);

servo1.write(servo1_d); // Set to neutral
servo2.write(servo2_d);
}

void loop() {
if (IrReceiver.decode()) {
IrReceiver.printIRResultShort(&Serial);
IrReceiver.printIRSendUsage(&Serial);

    if (IrReceiver.decodedIRData.protocol == UNKNOWN) {
        Serial.println(F("Received noise or an unknown protocol."));
        IrReceiver.printIRResultRawFormatted(&Serial, true);
    }

    Serial.println();
    IrReceiver.resume(); // Enable receiving of the next value

    // Check the received data and perform actions according to the received command
    switch(IrReceiver.decodedIRData.command) {
        case UP: // Start moving up
            unsigned long startTime = millis();
            while (IrReceiver.decode() && IrReceiver.decodedIRData.command == UP) {
                if ((millis() - startTime) % 100 == 0) { // Every 100 ms
                    upMove(1); // Move up 1 degree
                }
            }
            break;

        case DOWN: // Start moving down
            startTime = millis();
            while (IrReceiver.decode() && IrReceiver.decodedIRData.command == DOWN) {
                if ((millis() - startTime) % 100 == 0) { // Every 100 ms
                    downMove(1); // Move down 1 degree
                }
            }
            break;

        case LEFT: // Start moving up
            startTime = millis();
            while (IrReceiver.decode() && IrReceiver.decodedIRData.command == LEFT) {
                if ((millis() - startTime) % 100 == 0) { // Every 100 ms
                    leftMove(1); // Move left 1 degree
                }
            }
            break;

        case RIGHT: // Start moving down
            startTime = millis();
            while (IrReceiver.decode() && IrReceiver.decodedIRData.command == RIGHT) {
                if ((millis() - startTime) % 100 == 0) { // Every 100 ms
                    rightMove(1); // Move right 1 degree
                }
            }
            break;

        // Other cases...
    }
}
delay(5);

}
'''
I'm new to c++ and coding in general. I was wondering how to define the "upMove", "downMove", and etc?

  • The first thing you need to do is read the Posting Guidelines.

uh ok

... and format your code...

    if (IrReceiver.decode ()) {
        IrReceiver.printIRResultShort (&Serial);
        IrReceiver.printIRSendUsage (&Serial);

        if (IrReceiver.decodedIRData.protocol == UNKNOWN) {
            Serial.println (F ("Received noise or an unknown protocol."));
            IrReceiver.printIRResultRawFormatted (&Serial, true);
        }

        Serial.println ();
        IrReceiver.resume (); // Enable receiving of the next value

        // Check the received data and perform actions according to the received command
        switch (IrReceiver.decodedIRData.command) {
        case UP: // Start moving up
            unsigned long startTime = millis ();
            while (IrReceiver.decode () && IrReceiver.decodedIRData.command == UP) {
                if ( (millis () - startTime) % 100 == 0) { // Every 100 ms
                    upMove (1); // Move up 1 degree
                }
            }
            break;
  • IrReceiver.resul() is typicall called at the end of processing
  • why not just process the same key if it continues to be recognized each iteration of loop() instead of
    while (IrReceiver.decode () && IrReceiver.decodedIRData.command == UP)
  • looks like you only want to repeat some operation every 100 msec, why not just add a 100 msec delay at the end of loop()

Thanks, but what would that code look like?

are you able to recognize any IR code?
are you able to control a servo?

I'm new to coding in c++ with the arduino, but I can understand most code. The servo does work. I have provided the errors that are made with the code.

C:\Users\Ethan Zhang\Downloads\sketch_mar27a\sketch_mar27a.ino: In function 'void loop()':
C:\Users\Ethan Zhang\Downloads\sketch_mar27a\sketch_mar27a.ino:56:25: error: 'upMove' was not declared in this scope
                         upMove(1); // Move up 1 degree
                         ^~~~~~
C:\Users\Ethan Zhang\Downloads\sketch_mar27a\sketch_mar27a.ino:56:25: note: suggested alternative: 'remove'
                         upMove(1); // Move up 1 degree
                         ^~~~~~
                         remove
C:\Users\Ethan Zhang\Downloads\sketch_mar27a\sketch_mar27a.ino:65:25: error: 'downMove' was not declared in this scope
                         downMove(1); // Move down 1 degree
                         ^~~~~~~~
C:\Users\Ethan Zhang\Downloads\sketch_mar27a\sketch_mar27a.ino:65:25: note: suggested alternative: 'pinMode'
                         downMove(1); // Move down 1 degree
                         ^~~~~~~~
                         pinMode
C:\Users\Ethan Zhang\Downloads\sketch_mar27a\sketch_mar27a.ino:74:25: error: 'leftMove' was not declared in this scope
                         leftMove(1); // Move left 1 degree
                         ^~~~~~~~
C:\Users\Ethan Zhang\Downloads\sketch_mar27a\sketch_mar27a.ino:74:25: note: suggested alternative: 'remove'
                         leftMove(1); // Move left 1 degree
                         ^~~~~~~~
                         remove
C:\Users\Ethan Zhang\Downloads\sketch_mar27a\sketch_mar27a.ino:83:25: error: 'rightMove' was not declared in this scope
                         rightMove(1); // Move right 1 degree
                         ^~~~~~~~~
exit status 1

Compilation error: 'upMove' was not declared in this scope

The errors are telling you that there are NO FUNCTIONS BY THOSE NAMES! Why? Where are they?

please post some code that simply recognizes one IR code

I don't know how to make the functions. That's what I'm asking.

#define UP    0xB946FF00  // Move Forward
#define DOWN  0xEA15FF00  // Move Backward
#define LEFT  0xBB44FF00  // Turn Left
#define RIGHT 0xBC43FF00  // Turn Right

Are you talking about this. I don't know how to make the functions like the "upMove".

This is a function! setup() is a function. loop() is a function. Search for "arduino function" and see others. Read your "C" documentation.

What have you tried?

what do you expect upMove() to do?

This code should theoretically work.

//At the start next to the "int servoPins"
int servo1Position = 90;
int servo2Position = 90;

Then after all the other code add:

void upMove(int degrees){
servo1Position += degrees;
servo1.write(servo1Position);
}

void downMove(int degrees){
servo1Position -= degrees;
servo1.write(servo1Position);
}

void rightMove(int degrees){
servo2Position += degrees;
servo2.write(servo2Position);
}

void leftMove(int degrees){
servo2Position -= degrees;
servo2.write(servo2Position);
}

--- Please note that this code assumes that servo 1 is up and down and servo 2 is left and right ---

Please let me know if this is what you were looking for.

void loop()
{
    if (IrReceiver.decode()) {
        unsigned long code = IrReceiver.decodedIRData.decodedRawData;

        if (code)  {
           Serial.println (code, HEX);
        }

        IrReceiver.resume();
    }
}