/*
power- 162
1- 48
2- 24
3- 122
*/
#include <IRremote.h>
#define RECV_PIN 13 // Signal Pin of IR receiver
const int ledPins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; // Define an array of LED pin numbers
const int numLeds = sizeof(ledPins) / sizeof(ledPins[0]); // Calculate the number of LEDs
unsigned long previousMillis = 0; // Variable to store the previous time
unsigned long interval = 100; // Interval between LED state changes (in milliseconds)
int ledStates[] = {LOW, LOW, LOW, LOW, LOW}; // Array to store the state of each LED
int currentLed = 0; // Index of the currently active LED
///////////////////////////////////////////
String irdata = ""; //
String power = "162"; //power button
String func_one = "48"; //button number 1
String func_two = "24"; //button number 2
String func_three = "122"; //button number 3
String func_four = "16"; //button number 4
//////////////////////////////////////////
bool power_on = false;
bool reverseDirection = false;
IRrecv irrecv(RECV_PIN);
void setup() {
Serial.begin(9600);
// Set LED pins as OUTPUT
for (int i = 0; i < numLeds; i++) {
pinMode(ledPins[i], OUTPUT);
}
irrecv.enableIRIn(); // Start the receiver
}
void loop() {
if (irrecv.decode()) {
irdata = irrecv.decodedIRData.command;
Serial.println(irdata);
irrecv.resume();
}
if (irdata == power) {
power_on = !power_on;
led_power();
irdata = "";
}
if (irdata == func_one && power_on) {
led_one();
}
if (irdata == func_two && power_on) {
led_two();
}
if (irdata == func_three && power_on) {
led_three();
}
if (irdata == func_four && power_on) {
led_four();
}
}
void led_one() {
unsigned long currentMillis = millis(); // Get the current time
// Check if it's time to change the state of the LEDs
if (currentMillis - previousMillis >= interval) {
// Save the current time as the previous time for the next iteration
previousMillis = currentMillis;
// Turn off the current LED
digitalWrite(ledPins[currentLed], LOW);
// Generate a random number to select the next LED
int nextLed = random(numLeds);
// Make sure we don't select the same LED again
while (nextLed == currentLed) {
nextLed = random(numLeds);
}
// Update the current LED
currentLed = nextLed;
// Turn on the new LED
digitalWrite(ledPins[currentLed], HIGH);
}
}
void led_two() {
unsigned long currentMillis = millis(); // Get the current time
// Check if it's time to change the state of the LEDs
if (currentMillis - previousMillis >= interval) {
// Save the current time as the previous time for the next iteration
previousMillis = currentMillis;
// Turn off the previous LED
digitalWrite(ledPins[currentLed], LOW);
// Move to the next LED
currentLed = (currentLed + 1) % numLeds;
// Turn on the current LED
digitalWrite(ledPins[currentLed], HIGH);
}
}
void led_three() {
unsigned long currentMillis = millis(); // Get the current time
// Check if it's time to change the state of the LEDs
if (currentMillis - previousMillis >= interval) {
// Save the current time as the previous time for the next iteration
previousMillis = currentMillis;
// Turn off all LEDs
for (int i = 0; i < numLeds; i++) {
digitalWrite(ledPins[i], LOW);
}
// Turn on the next LED
digitalWrite(ledPins[currentLed], HIGH);
// Calculate the index of the LED on the other end
int oppositeLed = (numLeds - 1) - currentLed;
// Turn on the LED on the other end
digitalWrite(ledPins[oppositeLed], HIGH);
// Move to the next LED
currentLed = (currentLed + 1) % numLeds;
}
}
void led_four() {
unsigned long currentMillis = millis(); // Get the current time
// Check if it's time to change the state of the LEDs
if (currentMillis - previousMillis >= interval) {
// Save the current time as the previous time for the next iteration
previousMillis = currentMillis;
// Turn off all LEDs
for (int i = 0; i < numLeds; i++) {
digitalWrite(ledPins[i], LOW);
}
// Turn on the LEDs for the first set (pins 2 to 11)
digitalWrite(ledPins[currentLed], HIGH);
//digitalWrite(ledPins[currentLed + numLeds], HIGH);
// Move to the next LED position for the first set
if (currentLed < numLeds - 1 && !reverseDirection) {
// Move forward
currentLed++;
} else {
// Change direction and move backward
reverseDirection = true;
currentLed--;
// If reached the first LED, change direction to forward again
if (currentLed == 0) {
reverseDirection = false;
}
}
}
}
void led_power() {
if (power_on) {
for (int i = 0; i < numLeds; i++) {
digitalWrite(ledPins[i], HIGH);
}
} else {
for (int i = 0; i < numLeds; i++) {
digitalWrite(ledPins[i], LOW);
}
}
}