#include <IRremote.h>
#include <Servo.h>
int RECV_PIN = 5;
int led = 13;
int pinMyServo1=10;
int pinMyServo2=11;
int ircode=0;
IRrecv irrecv(RECV_PIN);
Servo myServo1; // Create a "Servo" object called "arm"
Servo myServo2; // Create a "Servo" object called "arm"
float pos1 = 0.0; // Variable where the arm's position will be stored (in degrees)
float pos2 = 0.0;
float step = 10.0; // Variable used for the arm's position step
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
myServo1.attach(pinMyServo1); // Attache the arm to the pin 2
myServo1.write(pos1); // Initialize the arm's position to 0 (leftmost)
myServo2.attach(pinMyServo2); // Attache the arm to the pin 2
myServo2.write(pos2); // Initialize the arm's position to 0 (leftmost)
}
void loop() {
// Checks received an IR signal
if (irrecv.decode()) {
ircode = irrecv.decodedIRData.command;
Serial.print("code IR: "+String(ircode));
if (ircode==48){
Serial.println("LED ON");
digitalWrite(led, HIGH);
}
if (ircode==104){
Serial.println("LED OFF");
digitalWrite(led, LOW);
}
// myServo1
if (ircode==144){
Serial.println(" POS="+String(pos1));
if (pos1<=80) // Check that the position won't go higher than 180°
{
pos1+=step; // Increment "pos" of "step" value
myServo1.write(pos1);
delay(5); // Wait 5ms for the arm to reach the position
}
}
if (ircode==224){
Serial.println(" step="+String(pos1));
if (pos1>=0) // Check that the position won't go lower than 0°
{
pos1-=step; // Decrement "pos" of "step" value
myServo1.write(pos1); // Set the arm's position to "pos" value
delay(5); // Wait 5ms for the arm to reach the position
}
}
// myServo2
if (ircode==2){
Serial.println(" POS="+String(pos2));
if (pos2<=180) {
pos2+=step;
myServo2.write(pos2);
delay(5);
}
}
if (ircode==152){
Serial.println(" step="+String(pos2));
if (pos2>=0){
pos2-=step;
myServo2.write(pos2);
delay(5);
}
}
//translateIR();
//receiver.resume(); // Receive the next value
irrecv.resume();
}
}