#include <LiquidCrystal.h>
#include <IRremote.h>
#define PIN_TRIG 3
#define PIN_ECHO 2
#define PIN_RECEIVER 4
bool bit_on = false;
IRrecv receiver(PIN_RECEIVER);
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
void setup() {
Serial.begin(9600);
pinMode(PIN_TRIG, OUTPUT);
pinMode(PIN_ECHO, INPUT);
lcd.begin(16, 2);
// you can now interact with the LCD, e.g.:
lcd.print(" power off");
delay(1000);
lcd.clear();
receiver.enableIRIn(); // Start the receiver
}
void loop() {
// Start a new measurement:
if(bit_on == true){
//=======================
digitalWrite(PIN_TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(PIN_TRIG, LOW);
// Read the result:
int duration = pulseIn(PIN_ECHO, HIGH);
//lcd.print(" distance meter ");
lcd.setCursor(0,1);
lcd.print("Distance ");
lcd.print(duration / 58);
lcd.print(" ");
lcd.setCursor(14,1);
lcd.print("cm");
if (receiver.decode()) {
translateIR();
receiver.resume(); // Receive the next value
}
delay(1000);}
//=========================
else{
lcd.setCursor(0,0);
lcd.print(" power off");
if (receiver.decode()) {
translateIR();
receiver.resume(); // Receive the next value
}
}
}
void translateIR()
{
// Takes command based on IR code received
switch (receiver.decodedIRData.command) {
case 48:
Serial.println("POWER");
bit_on = !bit_on;
lcd.clear();
break;}}