#include <IRremote.h>
#include <LiquidCrystal.h>
#include <Servo.h>
#define PIN_RECEIVER 2 // Signal Pin of IR receiver
Servo sev_1;
float pos = 0.0; // Variable where the arm's position will be stored (in degrees)
float step = 1.0;
int ledPin = 13; // choose the pin for the LED
int inputPin = 4;
int buzzer = 5;
int ring = 6; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // Variable used for the arm's position step
IRrecv receiver(PIN_RECEIVER);
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
void setup()
{
sev_1.attach(3);
sev_1.write(pos); // Initialize the arm's position to 0 (leftmost)
lcd.begin(16, 2);
lcd.print("<press a button>");
receiver.enableIRIn();
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT);
pinMode(buzzer, OUTPUT);
pinMode(ring, OUTPUT);
Serial.begin(9600); // Start the receiver
}
void loop()
{
// Checks received an IR signal
if (receiver.decode()) {
translateIR();
receiver.resume(); // Receive the next value
}
val = digitalRead(inputPin); // read input value
if (val == HIGH) { // check if the input is HIGH
digitalWrite(ledPin, HIGH);
digitalWrite(buzzer, HIGH);
digitalWrite(ring, HIGH); // turn LED ON
if (pirState == LOW) {
// we have just turned on
Serial.println("Motion detected!");
// We only want to print on the output change, not state
pirState = HIGH;
}
} else {
digitalWrite(ledPin, LOW);
digitalWrite(buzzer, LOW);
digitalWrite(ring, LOW); // turn LED OFF
if (pirState == HIGH) {
// we have just turned of
Serial.println("Motion ended!");
// We only want to print on the output change, not state
pirState = LOW;
}
}
}
void lcdPrint(char* text)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("button pressed:");
lcd.setCursor(0, 1);
lcd.print(text);
lcd.print(" code: ");
lcd.print(receiver.decodedIRData.command);
}
void translateIR()
{
// Takes command based on IR code received
switch (receiver.decodedIRData.command) {
case 162:
digitalWrite(ledPin, HIGH);
digitalWrite(buzzer, HIGH);
digitalWrite(ring, HIGH);
delay(10000);
break;
case 226:
digitalWrite(ledPin, LOW);
digitalWrite(buzzer, LOW);
digitalWrite(ring, LOW);
delay(10000);
break;
case 34:
lcdPrint("TEST");
break;
case 2:
lcdPrint("PLUS");
break;
case 194:
lcdPrint("BACK");
break;
case 224:
lcdPrint("PREV.");
break;
case 168:
lcdPrint("PLAY");
break;
case 144:
lcdPrint("NEXT");
break;
case 104:
lcdPrint("num: 0");
break;
case 152:
if (pos<180) // Check that the position won't go higher than 180°
{
sev_1.write(pos); // Set the arm's position to "pos" value
pos+=(step+5); // Increment "pos" of "step" value
delay(0.1); // Wait 5ms for the arm to reach the position
}
break;
case 176:
if (pos>0) // Check that the position won't go lower than 0°
{
sev_1.write(pos); // Set the arm's position to "pos" value
pos-=(step+5); // Decrement "pos" of "step" value
delay(0.1); // Wait 5ms for the arm to reach the position
}
break;
case 48:
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
sev_1.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
break;
case 24:
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
sev_1.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
break;
case 122:
lcdPrint("num: 3");
break;
case 16:
lcdPrint("num: 4");
break;
case 56:
lcdPrint("num: 5");
break;
case 90:
lcdPrint("num: 6");
break;
case 66:
lcdPrint("num: 7");
break;
case 74:
lcdPrint("num: 8");
break;
case 82:
lcdPrint("num: 9");
break;
default:
lcd.clear();
lcd.print(receiver.decodedIRData.command);
lcd.print(" other button");
}
}