/*
Forum: https://forum.arduino.cc/t/i-need-help-with-a-panel-to-activate-practical-effects-for-a-short-film-shoot/1243104/2
Wokwi: https://wokwi.com/projects/394089604589582337
2024/04/02
ec2021
*/
const byte ledPin1 = 12;
const byte ledPin2 = 7;
struct switchType {
byte pin;
byte state = HIGH;
byte lastState = HIGH;
unsigned long lastChange = 0;
void init(byte aPin) {
pin = aPin;
pinMode(pin, INPUT_PULLUP);
}
boolean pressed() {
byte actState = digitalRead(pin);
if (actState != lastState) {
lastChange = millis();
lastState = actState;
}
if (actState != state && millis() - lastChange > 20) {
state = actState;
return !state;
}
return false;
}
boolean getState(){
byte actState = digitalRead(pin);
if (actState != lastState) {
lastChange = millis();
lastState = actState;
}
if (actState != state && millis() - lastChange > 20) {
state = actState;
}
return state;
}
};
switchType choice;
switchType effect1;
switchType effect2;
void setup() {
Serial.begin(115200);
choice.init(8);
effect1.init(11);
effect2.init(10);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
}
void loop() {
if (!choice.getState()){
switchEffect(1);
if (effect1.pressed()) {
Serial.println("Effect 1 initiated");
}
} else {
switchEffect(2);
if (effect2.pressed()) {
Serial.println("Effect 2 initiated");
}
}
}
void switchEffect(byte No){
switch(No){
case 1:
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, LOW);
break;
case 2:
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, HIGH);
break;
}
}