#include <LiquidCrystal.h>
#include <NewPing.h>
#include <Wire.h>
#include <Adafruit_HTU21DF.h>
#include <Si4703.h>
#include <Arduino.h>
Si4703 radio;
const int ledPin = 7;
Adafruit_HTU21DF htu = Adafruit_HTU21DF();
#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 = 0;
const int upButton = 13;
const int downButton = 6;
const int pinCLK = 14; // Connect KY-040 CLK pin to D4
const int pinDT = 15; // Connect KY-040 DT pin to D3
const int pinSW = 16; // Connect KY-040 SW pin to D2
LiquidCrystal lcd( 9, 8, 2, 3, 4, 5);
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
int counter = 0; // Counter value for the rotary encoder
int currentStateCLK;
int lastStateCLK;
String currentDir =""; // The current direction of rotation
bool buttonPressed = false;
// 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);
//radio.start();
//radio.setChannel(96.4);
pinMode(14, INPUT_PULLUP);
pinMode(15, INPUT_PULLUP);
pinMode(16, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
lastStateCLK = digitalRead(14);
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("hello world");
irrecv.enableIRIn();
//setDateTime(23, 4, 5, 12, 34, 56);
pinMode(13, INPUT_PULLUP);
delay(1000);
}
void loop() {
digitalWrite(ledPin, HIGH);
updateDisplay();
currentStateCLK = digitalRead(14);
// If last and current state of CLK are different, then a pulse occurred
if (currentStateCLK != lastStateCLK) {
// If the DT state is different than the CLK state, that means the encoder is rotating clockwise
if (digitalRead(15) != currentStateCLK) {
counter ++;
currentDir = "CW";
} else {
// Otherwise, it's rotating counter-clockwise
counter --;
currentDir = "CCW";
}
Serial.print("Direction: ");
Serial.print(currentDir);
Serial.print(" | Counter: ");
Serial.println(counter);
}
// Save the current state as the last state, for next time through the loop
lastStateCLK = currentStateCLK;
// Check if push button is pressed
if (!digitalRead(16) && !buttonPressed) { // If the button is pressed
buttonPressed = true;
Serial.println("Button Pressed!");
} else if (digitalRead(16)) { // If the button is released
buttonPressed = false;
}
// (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 (!digitalRead(upButton)) {
menu++;
Serial.print("menu: "); Serial.print(menu);
}
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(100);
updateDisplay(); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
}
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(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:
// Read from the distance sensor
unsigned int us = sonar.ping();
unsigned int distance = us / US_ROUNDTRIP_CM;
// Display the distance on the LCD
lcd.setCursor(0, 0);
lcd.print("Distance: ");
lcd.setCursor(0, 1);
if (distance>99){
// Print a message to the LCD.
lcd.print(distance);
}
else if (distance>9){
// Print a message to the LCD.
lcd.print(" ");
lcd.print(distance);
}
else{
// Print a message to the LCD.
lcd.print(" ");
lcd.print(distance);
}
lcd.print(" cm");
break;
case 2:
//Serial.print("Temp: "); Serial.print(htu.readTemperature());
// Serial.print("\tHum: "); Serial.println(htu.readHumidity());
lcd.print("Menu 2");
break;
case 3:
//printDateTime();
lcd.print("Menu 3");
break;
case 4:
int signalStrength = radio.getRSSI();
lcd.setCursor(0, 0);
lcd.print("frequency: 96.4 ");
lcd.setCursor(0, 1);
lcd.print(" RSSI:.... ");
lcd.print("ass");
Serial.print("Signal strength: ");
Serial.println(signalStrength);
break;
}
// Add more cases as per your menu items
}