#include <IRremote.h>
// Define pin numbers for touch sensors and LEDs
const int touchSensorPins[] = {A1, A0, 13, 6, 5, 8, 10, 9}; // Define touch sensor pins
const int ledPins[] = {A5, A4, A3, A2, 2, 3, 4, 7}; // Define LED pins
const int numButtons = sizeof(touchSensorPins) / sizeof(touchSensorPins[0]); // Calculate the number of buttons
boolean isTouched[numButtons] = {false}; // Initialize touch states
boolean ledStates[numButtons] = {false}; // Initialize LED states
const int offButton = 11; // Define off button pin (9th button)
const int relay = 12; // Define relay pin
String row; // String to store LED states in a row
int RECV_PIN = 0;
IRrecv irrecv(RECV_PIN); // Define IR receiver object
decode_results results;
void setup() {
Serial.begin(9600); // Start serial communication for debugging
irrecv.enableIRIn(); // Enable IR receiver
// Initialize LED pins as outputs
for (int i = 0; i < numButtons; i++) {
pinMode(ledPins[i], OUTPUT);
}
pinMode(relay, OUTPUT); // Initialize relay pin as output
pinMode(offButton, INPUT); // Initialize off button pin as input
digitalWrite(relay, LOW); // Turn off the relay initially
}
// Function to turn off all LEDs
void noViewers() {
for (int i = 0; i < numButtons; i++) {
digitalWrite(ledPins[i], LOW);
ledStates[i] = false;
}
}
// Function to handle IR commands
void handleIRCommand(String irNum) {
irNum.toLowerCase(); // Convert IR command to lowercase for consistent comparison
// Check if the received IR command matches known commands
if (irNum == "30" || irNum == "18" || irNum == "7a" || irNum == "10" ||
irNum == "38" || irNum == "5a" || irNum == "42" || irNum == "4a") {
int index = -1;
// Determine the index based on the IR command
if (irNum == "30") index = 0;
else if (irNum == "18") index = 1;
else if (irNum == "7a") index = 2;
else if (irNum == "10") index = 3;
else if (irNum == "38") index = 4;
else if (irNum == "5a") index = 5;
else if (irNum == "42") index = 6;
else if (irNum == "4a") index = 7;
// Toggle LED state if a valid index is found
if (index != -1) {
ledStates[index] = !ledStates[index];
digitalWrite(ledPins[index], ledStates[index]);
}
} else {
// If the command is not recognized, turn off all LEDs
noViewers();
}
}
void loop() {
// Turn relay on or off based on the row of LED states
if (row == "00000000") {
digitalWrite(relay, LOW);
} else {
digitalWrite(relay, HIGH);
}
// Decode IR signal
if (irrecv.decode()) {
String irNum = String(irrecv.decodedIRData.command, HEX);
handleIRCommand(irNum); // Handle the decoded IR command
irrecv.resume(); // Receive the next IR signal
delay(100);
}
row = ""; // Clear the row string
for (int i = 0; i < numButtons; i++) {
if (digitalRead(offButton) == HIGH) {
digitalWrite(relay, LOW);
noViewers();
} else {
int touchSensorValue = digitalRead(touchSensorPins[i]);
if (touchSensorValue == HIGH) {
if (!isTouched[i]) {
delay(100);
ledStates[i] = !ledStates[i];
digitalWrite(ledPins[i], ledStates[i]);
isTouched[i] = true;
}
} else {
isTouched[i] = false;
}
}
row += ledStates[i]; // Update the row string with LED states
}
delay(100); // Delay for stability
Serial.println(row); // Print the row of LED states to the serial monitor
}