This commit is contained in:
Hiajen Hiajen 2022-01-08 12:38:11 +01:00
parent 4d9e09165b
commit 61035645d0

317
script.c Normal file
View file

@ -0,0 +1,317 @@
#include <Arduino.h>
#include <IRremoteESP8266.h>
#include <IRrecv.h>
#include <IRutils.h>
// PINS
const uint16_t irRecivePin = 14; // Pin of IR Diode
const uint16_t ledRedPin = 4;
const uint16_t ledGreenPin = 5;
const uint16_t ledBluePin = 0;
// CONST VARS
const int mainLoopDelay = 50;
const int ledCycelSpeed = 50;
const int irReciveSpeed = 100;
const int maxColourRange = 256;
// Struck
enum status {
NONE, STROBE, COLOR_FADE, FADE, COLOR_CHANGE
} status;
//GLOBAL VARS
unsigned long key_value = 0;
int currentRedValue = 0;
int currentGreenValue = 0;
int currentBlueValue = 0;
int dimmValue = 0;
bool cdirection = false;
int deltaRedValue = 0;
int deltaGreenValue = 0;
int deltaBlueValue = 0;
int loopTimerLEDController = 0;
int loopTimerIRReciver = 0;
IRrecv irrecv(irRecivePin);
decode_results results; // store read IR values here
void readIR();
void controllLED();
bool myTurn(int *timer, int target);
void setup() {
Serial.begin(115200);
irrecv.enableIRIn(); // Start the receiver
while (!Serial) // Wait for the serial connection to be establised.
delay(50);
Serial.println();
Serial.print("IRrecvDemo is now running and waiting for IR message on Pin ");
Serial.println(irRecivePin);
pinMode(ledRedPin, OUTPUT);
pinMode(ledGreenPin, OUTPUT);
pinMode(ledBluePin, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT); // Initialize the LED_BUILTIN pin as an output
randomSeed(analogRead(0));
}
void loop() {
readIR();
controllLED();
delay(mainLoopDelay); // Mainloop delay timer
loopTimerLEDController += mainLoopDelay;
loopTimerIRReciver += mainLoopDelay;
}
bool myTurn(int *timer, int target) {
if ((*timer) > target) {
(*timer) = ((*timer) % target); // reset timer and add rest value
return true;
}
return false;
}
// Reads From set Pin IR values
// should only be executet 100mils delay
void readIR() {
if (!myTurn(&loopTimerIRReciver, irReciveSpeed))
return;
if (irrecv.decode(&results)) {
digitalWrite(LED_BUILTIN, LOW); // Turn the LED on (Note that LOW is the voltage level
serialPrintUint64(results.value, HEX);
Serial.println("");
switch (results.value) {
case 0xF700FF: // 1 - 1 | dimm less
dimmValue -= 25;
if (dimmValue < 0)
dimmValue = 0;
break;
case 0xF7807F: // 1 - 2 | dimm more
dimmValue += 25;
if (dimmValue > maxColourRange)
dimmValue = maxColourRange;
break;
case 0xF740BF: // 1 - 3 | random Colour
status = NONE;
currentRedValue = random(maxColourRange + 1);
currentGreenValue = random(maxColourRange + 1);
currentBlueValue = random(maxColourRange + 1);
dimmValue = 0;
break;
case 0xF7C03F: // 1 - 4 | OFF
status = NONE;
currentRedValue = 0;
currentGreenValue = 0;
currentBlueValue = 0;
break;
case 0xF720DF: // 2 - 1 | Red UP
status = NONE;
currentRedValue += 25;
if (currentRedValue > maxColourRange)
currentRedValue = maxColourRange;
break;
case 0xF7A05F: // 2 - 2 | Green UP
status = NONE;
currentRedValue += 25;
if (currentGreenValue > maxColourRange)
currentGreenValue = maxColourRange;
break;
case 0xF7609F: // 2 - 3 | Blue UP
status = NONE;
currentBlueValue += 25;
if (currentBlueValue > maxColourRange)
currentBlueValue = maxColourRange;
break;
case 0xF7E01F: // 2 - 4 | WHITE
status = NONE;
currentRedValue = maxColourRange;
currentGreenValue = maxColourRange;
currentBlueValue = maxColourRange;
break;
case 0xF710EF: // 3 - 1 | Red DOWN
status = NONE;
currentRedValue -= 25;
if (currentRedValue < 0)
currentRedValue = 0;
break;
case 0xF7906F: // 3 - 2 | Green DOWN
status = NONE;
currentRedValue -= 25;
if (currentGreenValue < 0)
currentGreenValue = 0;
break;
case 0xF750AF: // 3 - 3 | Blue DOWN
status = NONE;
currentBlueValue -= 25;
if (currentBlueValue < 0)
currentBlueValue = 0;
break;
case 0xF7D02F: // 3 - 4 | Party Colour
status = COLOR_CHANGE;
deltaRedValue = random(5) + 1;
deltaGreenValue = random(5) + 1;
deltaBlueValue = random(5) + 1;
break;
case 0xF730CF: // 4 - 1 | Dark Orange
status = NONE;
currentRedValue = 143;
currentGreenValue = 61;
currentBlueValue = 75;
break;
case 0xF7B04F: // 4 - 2 | Light BLue
status = NONE;
currentRedValue = 125;
currentGreenValue = 232;
currentBlueValue = 234;
break;
case 0xF7708F: // 4 - 3 | Purble
status = NONE;
currentRedValue = 106;
currentGreenValue = 58;
currentBlueValue = 79;
break;
case 0xF7F00F: // 4 - 4 | Party WHite
break;
case 0xF708F7: // 5 - 1 | Orange
status = NONE;
currentRedValue = 255;
currentGreenValue = 165;
currentBlueValue = 0;
break;
case 0xF78877: // 5 - 2 | Beige
status = NONE;
currentRedValue = 83;
currentGreenValue = 186;
currentBlueValue = 184;
break;
case 0xF748B7: // 5 - 3 | Lila
status = NONE;
currentRedValue = 220;
currentGreenValue = 44;
currentBlueValue = 108;
break;
case 0xF7C837: // 5 - 4 | Current colour fade
status = FADE;
break;
case 0xF728D7: // 6 - 1 | Yellow
status = NONE;
currentRedValue = 252;
currentGreenValue = 236;
currentBlueValue = 203;
break;
case 0xF7A857: // 6 - 2 | Ocean
status = NONE;
currentRedValue = 184;
currentGreenValue = 227;
currentBlueValue = 234;
break;
case 0xF76897: // 6 - 3 | Pink
status = NONE;
currentRedValue = 249;
currentGreenValue = 121;
currentBlueValue = 173;
break;
case 0xF7E817: // 6 - 4 | Colour fade shuffle
status = COLOR_FADE;
break;
}
irrecv.resume(); // Receive the next value
digitalWrite(LED_BUILTIN, HIGH); // Turn the LED off by making the voltage HIGH
}
}
void controllLED() {
if (myTurn(&loopTimerLEDController, ledCycelSpeed)) {
switch (status) {
case FADE:
dimmValue += (cdirection == true ? -2 : 2);
if (dimmValue > maxColourRange || dimmValue < 0)
cdirection = !cdirection;
break;
case COLOR_FADE:
dimmValue += (cdirection == true ? -2 : 2);
if (dimmValue > maxColourRange
|| (((currentRedValue - dimmValue) < 0) && (currentGreenValue - dimmValue) < 0 && (currentBlueValue - dimmValue) < 0))
cdirection = !cdirection;
if (dimmValue > maxColourRange) {
currentRedValue = random(maxColourRange + 1);
currentGreenValue = random(maxColourRange + 1);
currentBlueValue = random(maxColourRange + 1);
}
break;
}
if (status != COLOR_CHANGE) {
deltaRedValue = 0;
deltaGreenValue = 0;
deltaBlueValue = 0;
}
currentRedValue += deltaRedValue;
if (currentRedValue > maxColourRange) {
currentRedValue = maxColourRange;
deltaRedValue *= -1;
}
if (currentRedValue < 0) {
currentRedValue = 0;
deltaRedValue *= -1;
}
currentGreenValue += deltaGreenValue;
if (currentGreenValue > maxColourRange) {
currentGreenValue = maxColourRange;
deltaGreenValue *= -1;
}
if (currentGreenValue < 0) {
currentGreenValue = 0;
deltaGreenValue *= -1;
}
currentBlueValue += deltaBlueValue;
if (currentBlueValue > maxColourRange) {
currentBlueValue = maxColourRange;
deltaBlueValue *= -1;
}
if (currentBlueValue < 0) {
currentBlueValue = 0;
deltaBlueValue *= -1;
}
}
//if(deltaRedValue != 0)
int val = ((PWMRANGE / (((double)maxColourRange)) * (currentRedValue - dimmValue)));
if (val < 0)
val = 0;
analogWrite(ledRedPin, val);
//if(deltaGreenValue != 0)
val = ((PWMRANGE / (((double)maxColourRange)) * (currentGreenValue - dimmValue)));
if (val < 0)
val = 0;
analogWrite(ledGreenPin, val);
//if(deltaBlueValue != 0)
val = ((PWMRANGE / (((double)maxColourRange)) * (currentBlueValue - dimmValue)));
if (val < 0)
val = 0;
analogWrite(ledBluePin, val);
return;
}