Trying to control a servo using a IR remote and a toggle switch

Hello everyone, so I created a project to control a servo 2 ways, one by a toggle switch and the other by an IR Remote. This project is used to control my dorm room lights. (I know there is an easier way but using relays but I can’t mess with the electrical on campus. With the code I have, the only way that I can get the IR portion of the project to work is if I comment out the ToggleSwitch loop (you will see what I mean in my code that I am posting below). I know that there is some sort of interference with the ToggleSwitch portion of the code since when it does run that portion works by the IR portion does not. If anyone can help me out please it would be much appreciated.

#include <IRremote.h>
#include <Servo.h>
#define on 0x57E37887 //clockwise rotation button
#define off 0x57E3B44B //counter clockwise rotation button
int RECV_PIN = 2; //IR receiver pin
Servo servo;
int val; //rotation angle for IR
int val2; //rotation angle for ToggleSwitch
bool cwRotation, ccwRotation; //the states of rotation
int switch_pin = 5; //toggleswitch

IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
servo.attach(9); //servo pin
pinMode(switch_pin, INPUT_PULLUP);
}

void IR_Receive()
{
if (irrecv.decode(&results)) {
Serial.println(results.value, HEX);
irrecv.resume(); // Receive the next value
if (results.value == on)
{
cwRotation = !cwRotation; //toggle the rotation value
ccwRotation = false; //no rotation in this direction
}
if (results.value == off)
{
ccwRotation = !ccwRotation; //toggle the rotation value
cwRotation = false; //no rotation in this direction
}
}
if (cwRotation && (val != 135)) {
val++; //for colockwise button
}
if (ccwRotation && (val != 0)) {
val--; //for counter colockwise button
}
servo.write(val);
delay(1); //General speed
}//IR_Receive

void ToggleSwitch()
{
if (digitalRead(switch_pin)== HIGH)
{
cwRotation = !cwRotation; //toggle the rotation value
ccwRotation = false; //no rotation in this direction
}
if (digitalRead(switch_pin)== LOW)
{
ccwRotation = !ccwRotation; //toggle the rotation value
cwRotation = false; //no rotation in this direction
}
if (cwRotation && (val2 != 135)) {
val2++; //for colockwise button
}
if (ccwRotation && (val2 != 0)) {
val2--; //for counter colockwise button
}
servo.write(val2);
delay(1); //General speed

}//ToggleSwitch

void loop()
{
IR_Receive();
ToggleSwitch(); //If I comment this out of the loop the IR portion works, but when this runs the IR portion doesn't work.
}

Please correct your post above and add code tags around your code:
[code]`` [color=blue]// your code is here[/color] ``[/code].

It should look like this:// your code is here
(Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)

Read the forum rules please.

What’s with all the extra blank lines? They make it really difficult to read.

Oh and if that’s really a toggle switch what is supposed to happen when the switch is set to go on and the IR sends an “off” or vice versa? What overrides what and how?

Steve

Your switch is either HIGH or LOW so you keep messing around with your rotation variables... you need to track the change of the state to the switch, not just its state (ie when it goes HIGH to LOW or LOW to HIGH, not when it is HIGH or LOW)

slipstick:
What's with all the extra blank lines? They make it really difficult to read.

Oh and if that's really a toggle switch what is supposed to happen when the switch is set to go on and the IR sends an "off" or vice versa? What overrides what and how?

Steve

They are supposed to be two different entities, so I can independently control the toggle switch to control the servo, same as the IR.