IR Servo control

Hello, i am not good at all at programming and i am honestly not sure if i am close or not, But pretty much i have previously uploaded code that records my IR signals from my t.v remote and then i used code that would turn on an LED here

/*
  IR Receiver Demonstration 3
  IR-Rcv-Demo3.ino
  Control LED's using Unused IR Remote keys

  DroneBot Workshop 2017
  http://dronebotworkshop.com
*/

// Include IR Remote Library by Ken Shirriff
#include <IRremote.h>

// Define sensor pin
const int RECV_PIN = 4;

// Define LED pin constants
const int redPin = 8; 
const int yellowPin = 7;

// Define integer to remember toggle state
int togglestate = 0;

// Define IR Receiver and Results Objects
IRrecv irrecv(RECV_PIN);
decode_results results;


void setup(){
  // Enable the IR Receiver
  irrecv.enableIRIn();
  // Set LED pins as Outputs
  pinMode(redPin, OUTPUT);
  pinMode(yellowPin, OUTPUT);
}


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

        switch(results.value){
          case 0x20DF807F: //Red Keypad Button
        // Turn on LED for 2 Seconds
        digitalWrite(redPin, HIGH);
        delay(2000);
        digitalWrite(redPin, LOW);
        break;
   
          case 0x20DF40BF: //Yellow Keypad Button
        // Toggle LED On or Off
        if(togglestate==0){
        digitalWrite(yellowPin, HIGH);
        togglestate=1;
        }
        else {
        digitalWrite(yellowPin, LOW);
        togglestate=0;
        }
        break;
        
    }
    irrecv.resume(); 
  }

}

and this works fine,
But My goal is to convert the results from my Red keypad button and when it receives this code i want it to rotate a servo from 0-180 degrees instead of turning on an LED.

Here is what i have so far but i dont know enough to know what to do next

#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(); 
  }

}}

Can someone please help?

What exactly does it do when you test it? What should it do that's different?

Steve

slipstick:
What exactly does it do when you test it? What should it do that's different?

Steve
[/qu

slipstick:
What exactly does it do when you test it? What should it do that's different?

Steve

Pretty much now, when i press the button the servo sweeps forward one direction then back the other direction, I just want it to sweep one direction then stop, then when i press it again, i want it to sweep back.
I have the single servo hooked up to a 3d printed claw, so right now that code is making it open then close, which is useless. I just want it to open-stop,close-stop.

So set a flag to tell you what the current position is, call it something like openFlag. Each time you complete a servo movement set it to an appropriate value e.g. 0 = closed, 1 = open. Then put some if statements round the two for loops you have to check the flag. If it's at 0 use just the for loop that sends it the other way and vice versa.

Steve

slipstick:
So set a flag to tell you what the current position is, call it something like openFlag. Each time you complete a servo movement set it to an appropriate value e.g. 0 = closed, 1 = open. Then put some if statements round the two for loops you have to check the flag. If it’s at 0 use just the for loop that sends it the other way and vice versa.

Steve

Im not sure how to do that, could you show me please?

Over 300 posts and you still don't know how to create a variable and use it in if statements? Give it a good try and post your attempt. I'll help you to get it working but I'm not going to just write your code for you.

Steve

slipstick:
Over 300 posts and you still don’t know how to create a variable and use it in if statements? Give it a good try and post your attempt. I’ll help you to get it working but I’m not going to just write your code for you.

Steve

Thank you for the response, 90% of my post are about general electronics, I barely understand the basics and i know this code is wrong but here is my best shot

#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 openFlag
int openFlag(0=closed,1=open)
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 0x20DF40BF: 
     
 openFlag,(1)
         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
  openFlag,(0)
  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(); 
  }

}}

A best shot would at least involve trying to compile the program and getting rid of some of the more obvious errors e.g. you can't define the same variable twice and if you want to say "if openFlag is 0" you can't leave the "if" out. And "openFlag, (0)" doesn't say anything useful. But openFlag == 0 just might.

if - Arduino Reference will show you how to write an if statement properly.

Steve