Moving servo using IR Remote

Hello!

I am fairly new to arduino and I need some guidance with a code.

I am trying to move a servo 90 degrees when one button on my IR remote is pressed. When this same button is pressed again I want the servo to move back to 0 degrees.

Right now this process is being done with two seperate buttons on my IR remote.

Here is the code that I am working with.


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

unsigned long Value1 = 0xFF6897; // where XXXXXXXX is on our your remote's values (number 1 on remote)
unsigned long Value2 = 0xFF9867; // where XXXXXXXX is another button on your remote (number 2 on remote)

int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;

Servo servo1;

// the setup routine runs once when you press reset:
void setup() {              

Serial.begin(9600);
 irrecv.enableIRIn(); // Start the receiver

 // initialize the digital pin as an output.

servo1.attach(10); // attack servo to digital pin 10
}

// the loop routine runs over and over again forever:
void loop() {

if (irrecv.decode(&results)) {
   Serial.println(results.value, HEX);
   irrecv.resume(); // Receive the next value
 }

if(results.value == Value1) {
servo1.write(160);
}
else if  (results.value == Value2){
servo1.write(75);
} 
}

My goal is to make this code work with the use of a single button on the IR remote.

Welcome to the Forum. You have posted code without using code tags. That can be a problem. Please read these two posts:

How to use this forum - please read.
and
Read this before posting a programming question …

The code tags make the code look

like this

when posting source code files. It makes it easier to read, and can be copied with a single mouse click. Also, if you don’t do it, some of the character sequences in the code can be misinterpred by the forum code as italics or funny emoticons.

Many questions can be answered by simply reading the documentation which is provided with the IDE, available under the help tab, or online here.

If you have already posted without using code tags, open your message and select “modify” from the pull down menu labelled, “More”, at the lower left corner of the message. Highlight your code by selecting it (it turns blue), and then click on the “</>” icon at the upper left hand corner. Click on the “Save” button.

There are many other things that programmers do to make their code understandable. Please do them, as a courtesy to the members who volunteer their time to help you here. One is to use a standard indentation to clearly show the code blocks. Never put more than one statement per line. Place any brackets by themselves on a separate line. Before posting the code, use Ctrl-T in the IDE to reformat the code in a standard format, which makes it easier for us to read.

Please also explain what your problem is, because you didn’t actually specify that.

Declare a new global variable named flag and set it to true. Then use this after you have read the IR input in loop()

  if (results.value == Value1 && flag == false)
  {
    servo1.write(160);
    flag == true;
  }
  else
  {
    servo1.write(75);
    flag = false;
  }
}

Thank you very much I will give this a try. Sorry, because I am new to this I am not sure how to declare a new global variable named flag and set it to true. What do you mean by this?

At the top of your program, outside setup() and loop():

bool flag = true;

austiwawa: Thank you very much I will give this a try. Sorry, because I am new to this I am not sure how to declare a new global variable named flag and set it to true. What do you mean by this?

byte flag = 1; https://www.arduino.cc/en/Reference/Scope

Thank you all for your help. This is what I have so far. I am not sure if I put it together correctly but it does not seem to be working.

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

unsigned long Value1 = 0xFF6897; // where XXXXXXXX is on our your remote's values


int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;

Servo servo1;
bool flag = true;

// the setup routine runs once when you press reset:
void setup() {              

Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver

  // initialize the digital pin as an output.

servo1.attach(10); // attack servo to digital pin 10
}

// the loop routine runs over and over again forever:
void loop() {

if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    irrecv.resume(); // Receive the next value
  }

  if (results.value == Value1 && flag == false)
  {
    servo1.write(160);
    flag == true;
  }
  else
  {
    servo1.write(75);
    flag = false;
  }
}
    flag == true;

Wrong.

I have not had any luck getting this code to work. Does anybody have any more thoughts ? Just a refresher, I am trying to control a servo with an IR remote. I want to move the servo with the use of only one button. So when I hit the button the servo will move 90 degrees, and when I hit that same button again I want it to move back to the original position.

Thank you all

aarg:     flag == true;

Wrong.

That's my typo from the original code that I suggested. Did you correct it as pointed out by aarg ?