IRservo controls

Hello, below i have code that takes the input from my t.v remote to a standard Ir receiver and this is fed into my Arduino nano and when my Arduino receives the infrared code and it matches the code in my sketch, it turns a servo to 180 degrees and then sweeps back to zero. I have this servo hooked up to a 3-D printed claw and right now it is useless because normally it is closed and when the code is executed the claw opens then closes.

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

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position

const int RECV_PIN = 4;

IRrecv irrecv(RECV_PIN);
decode_results results;


void setup(){
myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  
  irrecv.enableIRIn();
  

}


void loop(){
    if (irrecv.decode(&results)){

        switch(results.value){
          case 0x20DF807F: 
     
 
         for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       //
        
    }
    irrecv.resume(); 
  }

}}

I want it to open by the press of the button and close by the press of a button.

Is there and easier way than to set a flag? I have been trying to get this code to work for weeks.

Can someone please help?

So, with you particular transmitter, when you first see the code you know the button has been pressed.

How do you know when the button has been released? Until you've worked that out you can't get any further.

It might be easier to use one button/code for "open" and a different button/code for "close".

Steve

If you don’t want to add a flag variable to remember if the claw was opened or closed last time, you can use the ‘pos’ variable you already have:

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


Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards


int pos = 0;    // variable to store the servo position


const int RECV_PIN = 4;


IRrecv irrecv(RECV_PIN);
decode_results results;




void setup()
{
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object


  irrecv.enableIRIn();
}




void loop()
{
  if (irrecv.decode(&results))
  {


    switch (results.value)
    {
      case 0x20DF807F:
        if (pos == -1)
        {
          // It was closed last time, so open it
          for (pos = 0; pos <= 180; pos += 1)   // goes from 0 degrees to 180 degrees
          {
            // in steps of 1 degree
            myservo.write(pos);   // tell servo to go to position in variable 'pos'
            delay(15);            // waits 15ms for the servo to reach the position
          }
        }
        else
        {
          // It was not closed last time, so close it this time

          for (pos = 180; pos >= 0; pos -= 1)   // goes from 180 degrees to 0 degrees
          {
            myservo.write(pos);   // tell servo to go to position in variable 'pos'
            delay(15);
          }
        }
        break;
    } // End switch (results.value)
    
    irrecv.resume();
    
  } // End  if (irrecv.decode(&results))
}