#include "FastLED.h";
bool On = false;
bool NightOrDay = false;
CRGB LED;
enum ledPin: byte {
red = 3,
green = 5,
blue = 6,
white = 9
};
byte button[4] = {12, 11, 10, 8};
bool buttonPressed[5] = {false, false, false, false, false};
byte Br = 10;
// https://stackoverflow.com/questions/1952175/how-can-i-call-a-function-using-a-function-pointer
//
// void A() { Serial.println("first");}; // declare three functions
// void B() { Serial.println("second");};
// void C() { Serial.println("third");};
// void (*choice) (); // defines a pointer "choice"
// choice = B; // "choice" points to function "B()""
// choice(); // this calls function "B()", prints "second"
// functions associated with the four buttons
// placed in an array so that a "for" loop could be used to scan buttons
// and call the functions directly, without using "if" or "switch-case"
void f0() { On = !On; Serial.print("on " ); Serial.println(On); delay(500); }
void f1() { NightOrDay = !NightOrDay; Serial.print("n/d "); Serial.println(NightOrDay); delay(500); }
void f2() { Br--; Serial.print("br "); Serial.println(Br); delay(50); }
void f3() { Br++; Serial.print("br "); Serial.println(Br); delay(50); }
// https://stackoverflow.com/questions/1952175/how-can-i-call-a-function-using-a-function-pointer
typedef void (*func_ptr)();
func_ptr function[4] = {f0, f1, f2, f3}; // array of functions
// function[0] (); // functions are called this way
// function[1] ();
void setup() {
Serial.begin(9600);
for (int i = 0; i<4; i++) {
pinMode(button[i], INPUT_PULLUP);
}
}
byte buttonTick() {
byte a = 0;
for (int i = 0; i<4; i++) {
if (buttonPressed[i]) return i;
}
}
void loop() {
// Read the button inputs
for (int i = 0; i<4; i++) {
buttonPressed[i] = digitalRead(button[i]) == LOW;
// do debouncing here
if (buttonPressed[i]) {
buttonPressed[i] = false;
function[i] (); // call function directly
}
}
if(On) {
if(NightOrDay){
analogWrite (ledPin::white, Br);
digitalWrite(ledPin::red, 0);
digitalWrite(ledPin::green, 0);
digitalWrite(ledPin::blue, 0);
}
else {
LED = ColorFromPalette(HeatColors_p,inoise8(millis()/16));
analogWrite (ledPin::red, LED.r);
analogWrite (ledPin::green, LED.g);
analogWrite (ledPin::blue, LED.b);
digitalWrite(ledPin::white, 0);
}
}
else {
digitalWrite(ledPin::red, 0);
digitalWrite(ledPin::green, 0);
digitalWrite(ledPin::blue, 0);
digitalWrite(ledPin::white, 0);
}
}