Hey guys, so I need help with my code. I'm trying to make a switch for my light switch where i can use an IR remote, a photo sensor (light), and a switch(push button) to control a servo to turn on or off my lights. I'm stuck on the part of the code where i coded the push button in to control the rotation of the servo, but where I'm stuck is that I cant quite figure out how to make a loop so that the servo can be used by the other switches as well. Right now because the pushbutton is always giving a reading, the servo is stuck on one angle and won't respond to another switch (IR remote). THANKS IN ADVANCE!!!
// C++ code
//
#include <Servo.h>
#include <IRremote.hpp>
int servoPin=6;
int pos1=180;
int pos2=0;
int buttonPin=12;
int buttonNew;
int buttonOld=1;
int photoPin=A4;
int photoVal;
int IRpin=3;
int lastswing;
int test;
int dt=1500;
Servo myServo;
void setup()
{
Serial.begin(9600);
IrReceiver.begin(IRpin);
myServo.attach(servoPin);
pinMode(buttonPin,INPUT);
pinMode(photoPin,INPUT);
}
void loop()
{
myServo.write=test;
photoVal=analogRead(photoPin); //photo resistor
Serial.print("light = ");
Serial.println(photoVal); //290 min
while (IrReceiver.decode()){
Serial.println(IrReceiver.decodedIRData.decodedRawData,HEX);
switch(IrReceiver.decodedIRData.decodedRawData){
case 0xBC43FF00:
Serial.println("works");
myServo.write(pos1); //Servo
}
switch(IrReceiver.decodedIRData.decodedRawData){
case 0xBB44FF00:
Serial.println("Also works");
myServo.write(pos2); //Servo
}
IrReceiver.resume();
}
buttonNew=digitalRead(buttonPin); //Push button
if (buttonOld==0 && buttonNew==1){
myServo.write(pos1);
}
else{
myServo.write(pos2);
}
delay(250);
}