#include <IRremote.h>
//declaring IR-Remote codes
const unsigned long value_0 = 3910598400;
const unsigned long value_1 = 3476094720;
const unsigned long value_2 = 3877175040;
const unsigned long value_3 = 2239430400;
const unsigned long value_4 = 4144561920;
const unsigned long value_5 = 3810328320;
const unsigned long value_6 = 2774204160;
const unsigned long value_7 = 3175284480;
const unsigned long value_8 = 2907897600;
const unsigned long value_9 = 3041591040;
const unsigned long value_pause = 3158572800;
const unsigned long value_plus = 3927310080;
const unsigned long value_minus = 4161273600;
//declaring Pins
const int remote_input = 12;
int redLedPins[] = {8, 9, 10};
int greenLedPins[] = {5, 6, 7};
int blueLedPins[] = {2, 3, 4};
int activeLedGroups[9] = {0}; // array for all current active led-pins (0 = None)
//creating object for IR-Sensor
IRrecv irrecv(remote_input);
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
Serial.println("Toggle red leds");
toggleLedGroup(redLedPins, 3);
break;
case value_2: // switch green leds
Serial.println("Toggle green leds");
toggleLedGroup(greenLedPins, 3);
break;
case value_3: // switch blue leds
Serial.println("Toggle blue leds");
toggleLedGroup(blueLedPins, 3);
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() {
irrecv.enableIRIn(); //enabling the IR-receiver
Serial.begin(9600); //Serial for debug printing
// initialise led pins as output pins
for (int i = 0; i < 3; i++) {
pinMode(redLedPins[i], OUTPUT);
pinMode(greenLedPins[i], OUTPUT);
pinMode(blueLedPins[i], OUTPUT);
}
Serial.println("Starting process finished");
}
void loop() {
if (checkIrInput()) {
Serial.println("Remote Input");
}
// Schalte alle aktiven LEDs ein
for (int i = 0; i < 9; i++) {
if (activeLedGroups[i] != 0) {
digitalWrite(activeLedGroups[i], HIGH);
}
}
// Schalte alle inaktiven LEDs aus
for (int i = 0; i < 9; i++) {
if (activeLedGroups[i] != 0) {
bool isActive = false;
for (int j = 0; j < 9; j++) {
if (activeLedGroups[j] == activeLedGroups[i] && i != j) {
isActive = true;
}
}
if (!isActive) {
digitalWrite(activeLedGroups[i], LOW);
}
}
}
}
void toggleLedGroup(int ledPins[], int numLeds) {
bool isActive = false;
for (int i = 0; i < numLeds; i++) {
for (int j = 0; j < 9; j++) {
if (activeLedGroups[j] == ledPins[i]) {
isActive = true;
}
}
}
if (!isActive) {
addLedGroup(ledPins, numLeds);
} else {
removeLedGroup(ledPins, numLeds);
}
Serial.print("activeLedGroups: ");
for (int i = 0; i < 9; i++) {
Serial.print(activeLedGroups[i]);
}
Serial.println("");
}
void addLedGroup(int ledPins[], int numLeds) {
int index = 0;
while (activeLedGroups[index] != 0 && index < 9) {
index++;
}
if (index < 9) {
for (int i = 0; i < numLeds; i++) {
activeLedGroups[index + i] = ledPins[i];
}
}
}
void removeLedGroup(int ledPins[], int numLeds) {
for (int i = 0; i < 9; i++) {
if (activeLedGroups[i] == ledPins[0]) {
for (int j = 0; j < numLeds; j++) {
activeLedGroups[i + j] = 0;
}
break;
}
}
}