#include <math.h>
#include <IRremote.h>
// Pin definitions
#define TRIG_PIN 3
#define ECHO_PIN 2
#define LED_BLUE_PIN 15
#define LED_RED_PIN 14
#define THERMISTOR_PIN 28
#define RGB_RED_PIN 8
#define RGB_GREEN_PIN 7
#define RGB_BLUE_PIN 6
#define IR_RECV_PIN 11
// Constants for the thermistor
const float SERIES_RESISTOR = 10000.0; // Ohm
const float NOMINAL_RESISTANCE = 10000.0; // Resistance is at 25°C
const float NOMINAL_TEMPERATURE = 25; // Celsius
const float BETA_COEFFICIENT = 3950;
IRrecv receiver(IR_RECV_PIN);
void setup() {
// Initialize GPIO pins
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(LED_BLUE_PIN, OUTPUT);
pinMode(LED_RED_PIN, OUTPUT);
receiver.enableIRIn(); // Initialize IR receiver
Serial1.begin(115200); // Initialize serial console
}
void loop() {
float distance = measureDistance(); // Measure distance from HC-SR04 ultrasonic distance sensor
Serial1.print("\nDistance: ");
Serial1.print(distance);
Serial1.println(" cm");
float tempC = readTemperature(); // Read temperature from thermistor
Serial1.print("Temperature: ");
Serial1.print(tempC);
Serial1.print(" °C | ");
float tempF = (tempC * 9.0 / 5.0) + 32.0; // Convert Celsius to Fahrenheit
Serial1.print(tempF);
Serial1.println(" °F");
// If distance is lower than 40 cm, turn on the blue LED
if (distance < 40.0)
digitalWrite(LED_BLUE_PIN, HIGH);
else
digitalWrite(LED_BLUE_PIN, LOW);
// If temperature is 40 °C or higher, turn on the red LED
if (tempC >= 40.0)
digitalWrite(LED_RED_PIN, HIGH);
else
digitalWrite(LED_RED_PIN, LOW);
// Read values from IR Receiver
if (receiver.decode()) {
remote_RGB_LED();
receiver.resume(); // Read the next value
}
delay(100); // 100 ms delay
}
float measureDistance() { // for HC-SR04 ultrasonic distance sensor
// Trigger a 10us pulse to the TRIG pin
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Wait for the echo pin to go HIGH and start time
while (digitalRead(ECHO_PIN) == LOW);
int start_time = micros();
// Wait for the echo pin to go LOW and stop time
while (digitalRead(ECHO_PIN) == HIGH);
int end_time = micros();
// Calculate the time difference and convert to distance (cm)
float time_diff = (float)(end_time - start_time);
float distance = (time_diff * 0.0343) / 2; // Speed of sound is 0.0343 cm/us
return distance;
}
float readTemperature() { // for thermistor
int sensorValue = analogRead(THERMISTOR_PIN);
int resistorValue = 10000;
float resistance = resistorValue / ((1023.0 / sensorValue) - 1.0); // ADC is 10-bit
// Steinhart-Hart equation
float steinhart;
steinhart = resistance / 10000.0;
steinhart = log(steinhart);
steinhart /= 3950.0;
steinhart += 1.0 / (25.0 + 273.15);
steinhart = 1.0 / steinhart;
float tempC = steinhart - 273.15; // Kelvin to Celsius
return tempC;
}
void remote_RGB_LED() { // for IR receiver
switch (receiver.decodedIRData.command) {
case 104: // Button 0 -> Off
analogWrite(RGB_RED_PIN, 0);
analogWrite(RGB_GREEN_PIN, 0);
analogWrite(RGB_BLUE_PIN, 0);
break;
case 48: // Button 1 -> Red
analogWrite(RGB_RED_PIN, 255);
analogWrite(RGB_GREEN_PIN, 0);
analogWrite(RGB_BLUE_PIN, 0);
break;
case 24: // Button 2 -> Green
analogWrite(RGB_RED_PIN, 0);
analogWrite(RGB_GREEN_PIN, 255);
analogWrite(RGB_BLUE_PIN, 0);
break;
case 122: // Button 3 -> Blue
analogWrite(RGB_RED_PIN, 0);
analogWrite(RGB_GREEN_PIN, 0);
analogWrite(RGB_BLUE_PIN, 255);
break;
case 16: // Button 4 -> Cyan
analogWrite(RGB_RED_PIN, 0);
analogWrite(RGB_GREEN_PIN, 255);
analogWrite(RGB_BLUE_PIN, 255);
break;
case 56: // Button 5 -> Magenta
analogWrite(RGB_RED_PIN, 255);
analogWrite(RGB_GREEN_PIN, 0);
analogWrite(RGB_BLUE_PIN, 255);
break;
case 90: // Button 6 -> Yellow
analogWrite(RGB_RED_PIN, 255);
analogWrite(RGB_GREEN_PIN, 255);
analogWrite(RGB_BLUE_PIN, 0);
break;
case 66: // Button 7 -> White
analogWrite(RGB_RED_PIN, 255);
analogWrite(RGB_GREEN_PIN, 255);
analogWrite(RGB_BLUE_PIN, 255);
break;
default:
Serial1.println("Other button pressed");
}
}