#include <LiquidCrystal.h>
#include <NewPing.h>
#include <Wire.h>
#include <Adafruit_HTU21DF.h>
#define PCF8563_ADDRESS 0x51 // I2C address for the PCF8563 RTC
#define TRIGGER_PIN 11 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 12 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters).
unsigned long upButtonCode = 0xFFA857; // Replace with your actual code
unsigned long downButtonCode = 0xFFE01F;
unsigned long leftButtonCode = 0xFFA857; // Replace with your actual code
unsigned long rightButtonCode = 0xFFE01F;
unsigned long okButtonCode = 0xFFA857; // Replace with your a
int menu = 1;
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
// Initialize the library with the pins used for the LCD
// RS, E, D4, D5, D6, D7
#include <IRremote.h>
int IR_PIN = 10; // Define the pin connected to the IR receiver data pin
IRrecv irrecv(IR_PIN);
decode_results results;
void setup() {
Serial.begin(9600);
// Set up the LCD's number of columns and rows:
//lcd.begin(16, 2);
// Print a message to the LCD.
//lcd.print("hello world");
irrecv.enableIRIn();
setDateTime(23, 4, 5, 12, 34, 56);
}
void loop() {
// Set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
printDateTime();
Serial.print("Temp: "); Serial.print(htu.readTemperature());
Serial.print("\tHum: "); Serial.println(htu.readHumidity());
if (irrecv.decode(&results)) {
Serial.println(results.value, HEX);
lcd.setCursor(0, 1);
lcd.print(results.value, HEX); // Print the hexadecimal value of the received IR signal
if (results.value == upButtonCode) {
menu++;
Serial.println("up button pressed");
lcd.setCursor(10, 1);
lcd.print("up ");
} else if (results.value == downButtonCode) {
menu--;
Serial.println("down button pressed");
lcd.setCursor(10, 1);
lcd.print("down ");
}else if (results.value == leftButtonCode) {
// Code to decrease volume or variable
Serial.println("left button pressed");
lcd.setCursor(10, 1);
lcd.print("left ");
}else if (results.value == rightButtonCode) {
// Code to decrease volume or variable
Serial.println("right button pressed");
lcd.setCursor(10, 1);
lcd.print("right");
}else if (results.value == okButtonCode) {
// Code to decrease volume or variable
Serial.println("ok button pressed");
lcd.setCursor(10, 1);
lcd.print("ok ");
}
irrecv.resume();
if (menu > 4) { // Assuming 4 menu items
menu = 1;
} else if (menu < 1) {
menu = 4;
}// Receive the next value
}
delay(50);
updateDisplay(); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
unsigned int uS = sonar.ping();
unsigned int distance = uS / US_ROUNDTRIP_CM ;// Send ping, get ping time in microseconds (uS).
Serial.print("Ping: ");
Serial.print(uS / US_ROUNDTRIP_CM); // Convert time into distance and print result (0 = outside set distance range)
Serial.println("cm");
lcd.setCursor(0, 0);
// Print a message to the LCD.
lcd.print("distance");
if (distance>99){
lcd.setCursor(9, 0);
// Print a message to the LCD.
lcd.print(distance);
}
else if (distance>9){
lcd.setCursor(9, 0);
// Print a message to the LCD.
lcd.print(" ");
lcd.print(distance);
}
else{
lcd.setCursor(9, 0);
// Print a message to the LCD.
lcd.print(" ");
lcd.print(distance);
}
}
void setDateTime(byte year, byte month, byte day, byte hour, byte minute, byte second) {
Wire.beginTransmission(PCF8563_ADDRESS);
Wire.write(0x02); // Start at location 2
Wire.write(decToBcd(second));
Wire.write(decToBcd(minute));
Wire.write(decToBcd(hour));
Wire.write(decToBcd(day));
Wire.write(decToBcd(weekday)); // day of the week (1-7), for example, 1
Wire.write(decToBcd(month));
Wire.write(decToBcd(year));
Wire.endTransmission();
}
byte bcdToDec(byte val) {
return (val / 16 * 10) + (val % 16);
}
byte decToBcd(byte val) {
return (val / 10 * 16) + (val % 10);
}
void printDateTime() {
Wire.beginTransmission(PCF8563_ADDRESS);
Wire.write(0x02); // set the register pointer to (0x02)
Wire.endTransmission();
Wire.requestFrom(PCF8563_ADDRESS, 7);
byte second = bcdToDec(Wire.read() & 0x7F);
byte minute = bcdToDec(Wire.read() & 0x7F);
byte hour = bcdToDec(Wire.read() & 0x3F);
byte day = bcdToDec(Wire.read() & 0x3F);
byte weekday = bcdToDec(Wire.read() & 0x07);
byte month = bcdToDec(Wire.read() & 0x1F);
byte year = bcdToDec(Wire.read());
Serial.print("Date: ");
Serial.print(2000 + year);
Serial.print("-");
Serial.print(month);
Serial.print("-");
Serial.print(day);
Serial.print(" Time: ");
Serial.print(hour);
Serial.print(":");
Serial.print(minute);
Serial.print(":");
Serial.println(second);
}
void updateDisplay() {
lcd.clear();
switch (menu) {
case 1:
lcd.print("Menu 1");
break;
case 2:
lcd.print("Menu 2");
break;
case 3:
lcd.print("Menu 3");
break;
case 4:
lcd.print("Menu 4");
break;
}
// Add more cases as per your menu items
}