0

I am trying to get a continuous (180 to 0 and back 0 to 180) movement from a servo when I press a button in remote and ONLY stop when I press the other button. So far, I have gotten it to move continuously, but then it doesn't stop when I press the 'stop' button. I know it is because of the while loop. However, I have tried switch-case, if statement, nothing has worked so far. Please help, any advice for it make it work is appreciated.

#include <Servo.h>

#define code1 2534850111 //decimal value of button 1
#define code3 16724175   //decimal value of button 1
#define code 4294967295   //random value
#define code2 16738455   //decimal value of button 0
#define code4 3238126971   //decimal value of button 0

Servo myservo; // servo object
int RECV_PIN = 11; //receiveing pin IR remote

int pos = 0;

IRrecv irrecv(RECV_PIN); 

decode_results results;

void setup() {
  Serial.begin(9600);
  irrecv.enableIRIn();  //start the receiver
  myservo.attach(9);    //servo connect to pin 9
  pinMode(2, OUTPUT);   //LED connect to pin 2
}

void loop() {
  if(irrecv.decode(&results)){
     // if(results.value == code1 || results.value == code3){
     while(results.value == code1 || results.value == code3){
        digitalWrite(2,HIGH); //turn the led on
        for(pos = 0; pos <= 180; pos += 1){ //servo goes form 0 to 180             degrees in steps of 1 degree
    myservo.write(pos);
    delay(7);
    }
    for(pos = 180; pos >= 0; pos -= 1){ //servo goes back from 180 to 0 degrees with 1 degree step
    myservo.write(pos);
    delay(7);
}
    }

 while(results.value == code2 || results.value == code4){
       digitalWrite(2, LOW);  // turn the led off
       myservo.write(pos);
       delay(15);
       break;
 }

  Serial.println(results.value, DEC); //show the decimal value of the     pressed button
  irrecv.resume();  //receive the next value
    }

}
gre_gor
  • 6,669
  • 9
  • 47
  • 52
theagleye
  • 9
  • 2
  • 1
    Please edit your question and show us the code that you currently have. Without that (and often a wiring diagram), there's really no way we can help you. – stevieb Jun 22 '17 at 13:50
  • My crystal ball doesn't tell me anything about your code... :( – Michaël Roy Jun 22 '17 at 13:56
  • Sorry, just learning this field. I thought there was a better way to attach the codes other than the 4 spaces in every line. However, thats not the problem, I hope you can help me now :) – theagleye Jun 22 '17 at 14:12
  • If you actually look at the box you are writing into, you would notice a toolbar with helpful tools, like a button for putting 4 spaces in front of the code. – gre_gor Jun 22 '17 at 15:05

1 Answers1

0

One way to solve your problem would be to check for the presence of a "button push" deeper inside loop(). Put your checks for the button presses inside your movement for loop to catch the changes right away. Looks like you might have two startcodes (?) so you might have to alter the if statements below, but hopefully I demonstrate how to check the condition to "keep going" in the code sample below.

void loop()
{
    if(irrecv.decode(&results))
    {
        // turn one way
        for(pos = 0; pos <= 180; pos += 1)
        {
            // only continue if the start code(s) still active
            if(results.value == STARTCODE || results.value == OTHERSTARTCODE)
            {
                myservo.write(pos);
                delay(7);
                irrecv.resume();  //receive the next value
            }
        }
        // turn the other way
        for(pos = 180; pos >= 0; pos -= 1)
        {
            // only continue if the start code(s) still active
            if(results.value == STARTCODE || results.value == OTHERSTARTCODE)
            {
                myservo.write(pos);
                delay(7);
                irrecv.resume();  //receive the next value
            }
        }
    }   
}
TomServo
  • 7,248
  • 5
  • 30
  • 47
  • Thanks. Your code looks better than mine, however doesn't exactly do what I wanted; the servo still does not continue to roll from 0 to 180 and back and keep on doing that until I press the "endcode". It does move everytime I press the button for 180 to 0 and also for 0 to 180(Ofc thats what the code is telling it do right). I am not sure if I have made the question clear, I hope it is though, please let me know if I can clear it up more somehow. Also, yes, I have two startcodes and also two endcodes. – theagleye Jun 22 '17 at 18:40