Help with IR remote code

Hello!

My goal in the code posted below is to turn a relay on and off with the use of one button. With this same button I want a servo to move 90 degrees. So when the button is pressed once, the relay turns on and the servo moves 90 degrees. Now when that same button is pressed again the relay turns off and I want the servo to move back to its original position.

The relay is turning on and off and is working great and the servo is moving to its given position.

The problem is I dont know how to get the servo to move back to its original position when the button is pressed a second time.

If any one has any thoughts I would love to hear!

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

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


int relay_pin = 13;
int recv_pin = 11;
int actual_state = LOW;


IRrecv irrecv(recv_pin);
decode_results results;

Servo servo1;

void setup() {
    pinMode(relay_pin, OUTPUT);
    Serial.begin(9600);
    irrecv.enableIRIn(); // Start the receiver
    servo1.attach(10); // attack servo to digital pin 10
}

void loop() {
    if (irrecv.decode(&results)) {
        Serial.println(results.value, HEX);
        if (results.value == Value1) { //  button
            actual_state = (actual_state == LOW) ? HIGH : LOW;
            digitalWrite(relay_pin, actual_state);
        }
        irrecv.resume(); // Receive the next value
        if(results.value == Value1) {
        servo1.write(160);
        }

        }  
        }

pseudocode:

if position_var = A then
{ move to position B, position_var := B }
else if  position_var = B then
{ move to position A, position_var := A }

Thank you for your reply. I am having trouble inserting this with what I have. I am fairly new to this

One way to look at it is that on odd numbered clicks you want the servo in one position and another position on even numbered clicks. You can count the clicks and move accordingly on each one.

You could also use a boolean value as a direction flag and set it to true or false on each click and move to the desired position.

Below is your could modified to count clicks, there are three variables added near the top and the write is changed to use the desired position for odd and even clicks.

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

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


int relay_pin = 13;
int recv_pin = 11;
int actual_state = LOW;

// Postions for even, odd clicks
const byte oddPosition = 160;
const byte evenPostion =  70;
int clickCount = 0; // # of clicks

IRrecv irrecv(recv_pin);
decode_results results;

Servo servo1;

void setup() {
  pinMode(relay_pin, OUTPUT);
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
  servo1.attach(10); // attack servo to digital pin 10
}

void loop() {
  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    if (results.value == Value1) { //  button
      actual_state = (actual_state == LOW) ? HIGH : LOW;
      digitalWrite(relay_pin, actual_state);
    }
    irrecv.resume(); // Receive the next value
    if(results.value == Value1) {
      //  servo1.write(160);

      // increment click count
      clickCount++;

      // move to even or odd position based on click count
      servo1.write( (clickCount % 2) ? oddPosition : evenPostion);

    }

  } 
}

Very helpful! It works perfectly! Thank you very much for your time and help. I appreciate it.