Ir remote control servo motor

Hey,

I tried to type script with using if statement where i typed something like if (irrecv.decode(&results) == "FF52AD") //According to the image its 9 key

but it didnt work and i need some help with how to make these if statements with IR remote

Are you using the IRremote library? If so, what version?

Please read the how to get the best out of this forum post. To see what sort of info that we need in order to help you.

Please post your code. Read the forum guidelines to see how to properly post code and some good information on making a good post.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

If there are compiler errors, please include the entire error message. It is easy to do. There is a button (lower right of the IDE window) called "copy error message". Copy the error and paste into a post in code tags. Paraphrasing the error message leaves out important information.

That is not a very good description of the problem. The statement conveys no useful information that we don't already know. If it did work you would not be here.

Please post a schematic. Written descriptions are always more ambiguous than a drawing. Hand drawn, photographed and posted is fine. Include all pin names/numbers, components, their part numbers and/or values and power supplies.

Here is an example that may help. There is an array of positions and an array of commands. In the example the elements of the commands array correspond to the '1' - '9' and '0' keys. The elements of the position array represent servo positions that correspond to the command element of the same index.

This is written with the later version of the IRremote library (> 3.0). The new version does not return a long value like the results. The key codes are return in:

IrReceiver.decodedIRData.command
IrReceiver.decodedIRData.address

To see the documentation on the IRremote library go here: GitHub - Arduino-IRremote/Arduino-IRremote: Infrared remote library for Arduino: send and receive infrared signals with multiple protocols. Lots of example code.

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

Servo servo;

const byte positions[] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
const byte NUM_POSITIONS = sizeof(positions);
// must be same number of elements
const byte commands[NUM_POSITIONS] = {19, 16, 17, 15, 12, 13, 11, 8, 9, 73}; // codes for '1' - '9'

const byte servoPin = 7;
const byte IR_RECEIVE_PIN = 4; // Signal pin that the IR reciever is connected to

#define DECODE_NEC

bool newCode = false;

void setup()
{
   Serial.begin(115200);
   IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);
   Serial.println(F("Ready to receive IR signals of protocols: "));
   servo.attach(servoPin);
   Serial.print(F("Ready to command servo on pin: "));
   Serial.println(servoPin);
}

void loop()
{
   getIR();
   if (newCode)
   {
      for (uint16_t n = 0; n < sizeof(commands); n++)
      {
         if (IrReceiver.decodedIRData.command == commands[n])
         {
            servo.write(positions[n]);
            newCode = false;
            break;
         }
      }
   }
}

void getIR()
{
   if (IrReceiver.decode())
   {
      newCode = true;
      IrReceiver.resume(); // Enable receiving of the next value
      // uncomment this print to get command codes.
      //Serial.println(IrReceiver.decodedIRData.command);
   }
}

Tested and working on my Uno with IR receiver and servo powered with an external 5V supply.

thx dude I've read the the code and search some in internet and tred to type it easier for me, is it still good? I mean your version is better or its just diffrent solution. On my serial monitor I saw wierd "codes" randomly genereted and I thought its beacuse of my environment around cuz stripes started appeared on my webcam while I was talking with my friends but I dont know meaby u know ?

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

//servo var
Servo servo;
const byte servoPin = 7;

//IR var
const int ir = 4;
IRrecv irrecv(ir);
decode_results results;


void setup() {
  Serial.begin(9600);
  irrecv.enableIRIn();
  servo.attach(servoPin);
}

void loop() {
  // these for remote names
  if(irrecv.decode(&results))
  {
    Serial.println(results.value, HEX);
    irrecv.resume();
    delay(100);
  }
  //servo 
  if(results.value == 0xFF52AD)
  {
    servo.write(180);
    delay(1000);
    servo.write(0);
    delay(1000);
    
  }
}

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