#include <IRremote.h>
#include <Servo.h>
// Set up IR reciever pin
const byte IR_RECEIVE_PIN = 3;
// Create variable to store value recieved from the ir remote
int ir_command;
// Create Servo object
Servo myservo;
// Create servo angle variable, start at 90 degrees, can be any angle between 0 to 180 degrees
int servoangle = 90;
// Set an initial angle increment that the up/down arrows will move the servo by (in degrees)
int increment=10;
void setup()
{
Serial.begin(9600); // start serial monitor to visualize data
IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK); // start IR receiver library
Serial.println("IR Receiver Online"); // print initial message
myservo.attach(9); // Servo signal wire on pin 9
myservo.write(servoangle); // write the desired servo angle (servoangle) to the servo motor
delay(20); // every servoname.write(angle); function call needs a 20 ms delay or timer to update servo position
}
void loop()
{
// Move servo to new position, just need to update servoangle variable inside IrReceiver().decode if statement below
myservo.write(servoangle); // write the desired servo angle (servoangle) to the servo motor
delay(20); // every servoname.write(angle); function call needs a 20 ms delay or timer to update servo position
if (IrReceiver.decode()) // Only perform an action if an IR signal is read by the reciever from the remote
{
ir_command = IrReceiver.decodedIRData.command; // Decode IR remote button press signal data
//Serial.println(ir_command); // Print decoded IR remote button press signal signal received
switch(ir_command) {
case 162: servoangle = 90; break; //Power = 90 degrees
case 224: servoangle = 0; break; //|<< = 0 degrees
case 144: servoangle = 180; break; //>>| = 180 degrees
case 194: servoangle += increment; break; //Up arrow = angle + increment
case 34: servoangle -= increment; break; //Down arrow = angle - increment
case 2: increment ++; break; //Vol + = increment + 1
case 152: increment --; break; //Vol - = increment - 1
}
if (servoangle > 180) {servoangle = 180};
else if (servoangle < 0) {servoangle = 0};
else if (increment < 1) {increment = 1};
/* No changes needed below */
// Prints the final values of 'increment' and servoangle - check these to make sure they don't exceed limits
// and are responding correctly to button presses
Serial.print("***Increment = ");
Serial.println(increment);
Serial.print("***Servo Angle = ");
Serial.println(servoangle);
// must always have this line at the end of "if (IrReceiver.decode())"
IrReceiver.resume(); // Resume waiting for the next IR remote button press
} // End IR receiver decode if statement after button pressed
} // End void loop, head back to the top of the void loop again