#include <LiquidCrystal.h>
#include <Wire.h>
#include "SparkFunHTU21D.h"
#define TRIGGER_PIN 5
#define ECHO_PIN 6
HTU21D myHumidity;
int pinA = 2; // Rotary encoder CLK
int pinB = 3; // Rotary encoder DT
int pinC = 4; // Rotary encoder button
float encoderPosCount = 76.0;
int pinALast;
bool buttonPressed = false;
int mode = -1; // 0 for frequency, 1 for temperature/humidity, 2 for RTC, 3 for IR Receiver, 4 for Distance Sensor
LiquidCrystal lcd(12,11,10,9,8,7);
// Initial functions
void initialiseSystem() {
Serial.begin(9600);
lcd.begin(16, 2);
pinMode(pinA, INPUT);
pinMode(pinB, INPUT);
pinMode(pinC, INPUT_PULLUP);
pinMode(TRIGGER_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinALast = digitalRead(pinA);
myHumidity.begin();
lcd.print("System Initialising");
delay(1000);
lcd.clear();
Serial.println("System Initialising");
}
void setup() {
initialiseSystem();
}
void loop() {
static unsigned long lastButtonPressTime = 0;
static unsigned long lastUpdateTime = 0;
if (millis() - lastButtonPressTime > 200) {
if (digitalRead(pinC) == LOW) {
handleRotaryPress();
lastButtonPressTime = millis();
} else {
buttonPressed = false;
}
}
handleRotaryRotate();
}
//Print functions for conciseness
void printLCD(String line1, String line2 = "") {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(line1);
lcd.setCursor(0, 1);
lcd.print(line2);
}
void printSerial(String line1, String line2 = "") {
Serial.print(line1);
Serial.println(line2);
}
// Placeholder functions for additional modules
void RTC() {
// Implement RTC functionality to show time/date, and set an alarm.
printLCD("RTC Mode");
delay(400);
lcd.clear();
}
void IRReceiver() {
// Implement IR receiver functionality to represent each button as a function.
printLCD("IR Mode");
delay(400);
lcd.clear();
}
float measureDistance() {
digitalWrite(TRIGGER_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIGGER_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGGER_PIN, LOW);
long duration = pulseIn(ECHO_PIN, HIGH);
return (duration * 0.034) / 2; // Speed of sound wave divided by 2 (go and return)
}
float calculateVolumeFromDistance(float distance) {
float volume;
if (distance > 0 && distance < 30) {
volume = map(distance, 5, 30, 0, 100);
} else {
volume = 100;
}
return volume;
}
void DistanceSensor() {
// Implement distance sensor functionality to show distance and gesture control to turn the radio on/off.
printLCD("Distance Mode");
delay(400);
lcd.clear();
float distance = measureDistance();
float volume = calculateVolumeFromDistance(distance);
// Display volume if in the correct mode
printLCD("Distance: " + String(distance), "Volume: " + String(volume));
}
void FMModule(int frequency) {
// Implement FM module functionality here
printSerial("Frequency: ", String(frequency, 1) + " MHz");
}
void updateTemperatureAndHumidity() {
printLCD("Temp/Humd Mode");
delay(400);
lcd.clear();
float humd = myHumidity.readHumidity();
float temp = myHumidity.readTemperature();
printLCD("Temp: " + String(temp, 1) + (char)223 + "C", "Humidity: " + String(humd, 1) + "%");
printSerial("Temp: ", String(temp, 1) + "C");
printSerial("Humidity: ", String(humd, 1) + "%");
}
void handleRotaryPress() {
// Function to handle rotary encoder button press
buttonPressed = true;
if (mode >= 4) {
mode = 0; // Wrap around to 0 if it exceeds 3
} else {
mode++;
}
switch (mode) {
case 0:
updateTemperatureAndHumidity();
break;
case 1:
RTC();
break;
case 2:
IRReceiver();
break;
case 3:
DistanceSensor();
break;
case 4:
mode = -1;
printLCD("Frequency:", String(encoderPosCount) + " MHz");
break;
default:
break;
}
// Reset the buttonPressed flag after handling the press
buttonPressed = false;
}
void handleRotaryRotate() {
int aVal = digitalRead(pinA);
if (aVal != pinALast) {
if (digitalRead(pinB) != aVal) {
encoderPosCount += 0.1;
if (encoderPosCount > 108.0) {
encoderPosCount = 76.0;
}
} else {
encoderPosCount -= 0.1;
if (encoderPosCount < 76.0) {
encoderPosCount = 108.0;
}
}
pinALast = aVal;
// Update LCD and Serial output with the new frequency
printLCD("Frequency:", String(encoderPosCount) + " MHz");
printSerial("Frequency:", String(encoderPosCount) + " MHz");
FMModule(encoderPosCount);
mode = -1;
}
}