#include <IRremote.h>
//declaring IR-Remote codes
const unsigned long value_0 = 2540240640;
const unsigned long value_1 = 3476094720;
const unsigned long value_2 = 3877175040;
const unsigned long value_3 = 2239430400;
const unsigned long value_4 = 4010868480;
const unsigned long value_5 = 3342401280;
const unsigned long value_6 = 2774204160;
const unsigned long value_7 = 3175284480;
const unsigned long value_8 = 3041591040;
const unsigned long value_9 = 2907897600;
const unsigned long value_pause = 1470693120;
const unsigned long value_plus = 4244832000;
const unsigned long value_minus = 1738080000;
//declaring Pins
const int remote_input = 12;
//LED Pins for each color [LED_PIN_1, LED_PIN_2, ...]
int redLedPins[] = {8, 9, 10};
const int numOfRedLeds = sizeof(redLedPins) / sizeof(redLedPins[0]);
int greenLedPins[] = {5, 6, 7};
const int numOfGreenLeds = sizeof(greenLedPins) / sizeof(greenLedPins[0]);
int blueLedPins[] = {2, 3, 4};
const int numOfBlueLeds = sizeof(blueLedPins) / sizeof(blueLedPins[0]);
int activePins[] = {}; //stores all active pins
int numOfActiveLeds() {
/*
returns the amount of active Pins
*/
return (sizeof(activePins) / sizeof(activePins[0]));
}
//declaring default mode of leds
bool redEnabled = false;
bool greenEnabled = false;
bool blueEnabled = false;
//other globals for the program
int modeStep = 0; //stores the current step in the lighting mode
//creating object for IR-Sensor
IRrecv irrecv(remote_input);
void activateRedLeds() {
/*
activates the red LEDs
When the red LEDs are already active nothing will happen
*/
if (redEnabled) {
return;
}
redEnabled = true;
for (int i = 0; i < numOfRedLeds; i++) {
activePins[numOfActiveLeds() + i] = redLedPins[i];
}
}
void activateGreenLeds() {
/*
activates the green LEDs
When the green LEDs are already active nothing will happen
*/
if (greenEnabled) {
return;
}
greenEnabled = true;
for (int i = 0; i < numOfGreenLeds; i++) {
activePins[numOfActiveLeds() + i] = greenLedPins[i];
}
}
void activateBlueLeds() {
/*
activates the blue LEDs
When the blue LEDs are already active nothing will happen
*/
if (blueEnabled) {
return;
}
blueEnabled = true;
for (int i = 0; i < numOfBlueLeds; i++) {
activePins[numOfActiveLeds() + i] = blueLedPins[i];
}
}
void seq_leds(int* LED_PINS, int numOfLeds, int current_step) {
//loops through all given LED_PINS while lighting them up,
//wait for LedDelay in ms and turn them off
digitalWrite(LED_PINS[current_step], HIGH);
delay(1000);
digitalWrite(LED_PINS[current_step], LOW);
//error preventing
if (current_step + 1 >= numOfLeds) { //when the last LED was reached
modeStep = 0;
return;
}
modeStep += 1;
}
void static_leds(int* LED_PINS, int numOfLeds, int current_step) {
//lights all leds up
digitalWrite(LED_PINS[current_step], HIGH);
//error preventing
if (current_step + 1 >= numOfLeds) { //when the last LED was reached
modeStep = 0;
}
modeStep += 1;
}
void reset_mode(int* LED_PINS, int numOfLeds) {
//
//turns all leds off to start the next lighting mode
//
Serial.println("beginn to turn all leds off");
Serial.println(numOfLeds);
for (int i = 0; i < numOfLeds; i++) {
int pin = LED_PINS[i];
digitalWrite(pin, LOW);
}
modeStep = 0;
}
void (*pLedModeFunc)(int*, int, int) = static_leds; //points to a function that determins the mode of the LEDs
bool checkIrInput() {
/*
checks if the IR-receiver detected an IR-input.
When an input is detected checks which button is pressed
and changes the led-lighting mode arccodingly
returns true when an IrInput was detected and false when there was
no Input detected
*/
//when the IR-receiver detects an input
if (irrecv.decode()) {
unsigned long remote_data = irrecv.decodedIRData.decodedRawData;
Serial.println(remote_data);
//checks wjich button got pressed on the remote
switch (remote_data) {
case value_1: // switch red leds
activateRedLeds();
break;
case value_2: // switch green leds
activateGreenLeds();
break;
case value_3: // switch blue leds
activateBlueLeds();
break;
case value_8:
pLedModeFunc = seq_leds;
Serial.println("Mode changed to seq_leds");
break;
case value_9:
pLedModeFunc = static_leds;
Serial.println("Mode changed to static_leds");
break;
}
irrecv.resume();
return true;
}
return false;
}
void setup() {
Serial.begin(9600); //Serial for debug printing
irrecv.enableIRIn(); //enabling the IR-receiver
//setup redLedPins
for (int i = 0; i < numOfRedLeds; i++) {
pinMode(redLedPins[i], OUTPUT);
}
for (int i = 0; i < numOfBlueLeds; i++) {
pinMode(blueLedPins[i], OUTPUT);
}
for (int i = 0; i < numOfGreenLeds; i++) {
pinMode(greenLedPins[i], OUTPUT);
}
}
void loop() {
if (checkIrInput()) {
reset_mode(activePins, numOfActiveLeds());
modeStep = 0;
}
pLedModeFunc(activePins, numOfActiveLeds(), modeStep);
}