Servo code problems?

Hi, I have recently been trying to hook up my servo to my light switch in my room and have it be controlled by both a momentary push button and an IR remote.

My goal is to have the servo be able to be in 2 positions, each either turning the light on or off. I tried to make it so the button and the IR codes would be able to tell the current position of the servo and change to be in the opposite position when either the button is pressed or the IR receiver receives the proper code from the remote.

The code I currently have is my best attempt to mash up the two separate codes. One for the button and one for the IR. The momentary button is doing it's job properly when I uploaded the code to my Arduino but when ever I press the IR remote once the servo will continue move back and forth between the two positions without stopping and without me pressing any buttons.

This is my first time posting and I did my best to look for any solutions already out there. Any help to improve the code and solve my problem would be much appreciated. Thank you.

Here is the code

#include <Servo.h>

#include <IRremote.h>

int inPin = 2;         // the number of the momentary input pin

int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin

int IRpin = 11;  // pin for the IR sensor
IRrecv irrecv(IRpin);
decode_results results;

Servo myservo;

boolean SRVOstate = false; // Initialize the state of servo as off/down

long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

void setup()
{
  pinMode(inPin, INPUT);
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
  myservo.attach(9);
}

void loop()
{
  reading = digitalRead(inPin);

  if (reading == HIGH && previous == LOW && millis() - time > debounce) //if momenatary is pressed
  {
    if (SRVOstate == true) //current state of servo
    {
      SRVOstate = false; //set the new state
      myservo.write(80); //servo down posistion
    }
    else
    {
      SRVOstate = true; //set the new state
      myservo.write(170); //servo up posistion
    }

    time = millis();    
  }
  previous = reading;
  
  if (irrecv.decode(&results)) 
    {
      
      irrecv.resume();   // Receive the next value
    }
  
  if (results.value == 2189279550)  //if IR remote value is recieved
     {
       if (SRVOstate == false)   //current state of servo
         {
           SRVOstate = true;   //set the new state
           myservo.write(170); //servo up posistion
           delay(1000);      // keeps the transistion smooth
             
         }
        else
          {
            SRVOstate = false; //set the new state
            myservo.write(80); //servo down posistion 
            delay(1000);
          }
          
     }
  
}

first....make your time variables "unsigned long"...you will have issues with just "long"
millis() returns unsigned long.

Then verify what you are hoping to achieve with some of your if statements.

eg

if (reading == HIGH && previous == LOW && millis() - time > debounce)

easier for me to read, if it is what you intended ==>

if ( (reading == HIGH) && (previous == LOW) && ((millis() - time) > debounce) )

Unless you are an experienced c coder, I would suggest you use brackets to ensure the order of the comparisons in these statements are what you want/expect.

Thank you for your response. I made the edits to the code but for some reason the servo still rotates back and forth between the two positions continuously once I press the button on my IR remote once. The momentary button is working fine. My goal is to have the servo rotate to the position that it is not currently in once I press the IR button and stay there. Then once I press the same IR button again it changes to the other position and stays there. Any ideas?

Sorry, cant help you much with servo stuff.

But you could try to remove the servo and replace with LEDs or Serial.prints to see if there is any conflict between the libraries. Or maybe just search to see if there are any common conflicts & solutions.

Presumably, you are driving the servos correctly?

I believe the problem is with this section of code.

  if (irrecv.decode(&results))
  {

    irrecv.resume();   // Receive the next value
  }

  if (results.value == 2189279550)  //if IR remote value is recieved
  {
    if (SRVOstate == false)   //current state of servo
    {
      SRVOstate = true;   //set the new state
      myservo.write(170); //servo up posistion
      delay(1000);      // keeps the transistion smooth

    }
    else
    {
      SRVOstate = false; //set the new state
      myservo.write(80); //servo down posistion
      delay(1000);
    }

  }

You are checking for a receive event and then doing the .resume as you should, but you are doing "if (results.value == 2189279550)" whether a receive event has occurred or not, if none has happened since last check then results.value will be unchanged since last time it was set (2189279550), making the if statement always true, meaning your servo just flip flops between the two states.

Perhaps try it this way so that results.value is only checked when a receive event has occurred

  if (irrecv.decode(&results))
  {

    irrecv.resume();   // Receive the next value

    if (results.value == 2189279550)  //if IR remote value is recieved
    {
      if (SRVOstate == false)   //current state of servo
      {
        SRVOstate = true;   //set the new state
        myservo.write(170); //servo up posistion
        delay(1000);      // keeps the transistion smooth

      }
      else
      {
        SRVOstate = false; //set the new state
        myservo.write(80); //servo down posistion
        delay(1000);
      }

    }
  }

Thank you both for the quick replies and your help. Honestly BH72 you are the best person in the world. This is my first time using this forum and i'm so glad I did. Your solution worked perfectly.

Glad to help!