Stap 5: De volledige Code
Hier is de volledige code klaar voor uploaden naar je Arduino.
int LED1 = 12; int LED2 = 13; int button = 3; boolean LED1State = false; boolean LED2State = false; long buttonTimer = 0; long longPressTime = 250; boolean buttonActive = false; boolean longPressActive = false; void setup() { pinMode(LED1, OUTPUT); pinMode(LED2, OUTPUT); pinMode(button, INPUT); } void loop() { if (digitalRead(button) == HIGH) { if (buttonActive == false) { buttonActive = true; buttonTimer = millis(); } if ((millis() - buttonTimer > longPressTime) && (longPressActive == false)) { longPressActive = true; LED1State = !LED1State; digitalWrite(LED1, LED1State); } } else { if (buttonActive == true) { if (longPressActive == true) { longPressActive = false; } else { LED2State = !LED2State; digitalWrite(LED2, LED2State); } buttonActive = false; } } } Boole LED1State = false; Boole LED2State = false; lange buttonTimer = 0; lange longPressTime = 250; int LED1 = 8; int LED2 = 9; int LED3 = 10; int LED4 = 11; int LED5 = 12; int LED6 = 13; int button1 = 3; int button2 = 4; long buttonTimer = 0; long buttonTime = 250; boolean buttonActive = false; boolean longPressActive = false; boolean button1Active = false; boolean button2Active = false; boolean LED1Active = false; boolean LED2Active = false; boolean LED3Active = false; boolean LED4Active = false; boolean LED5Active = false; boolean LED6Active = false; void setup() { pinMode(LED1, OUTPUT); pinMode(LED2, OUTPUT); pinMode(LED3, OUTPUT); pinMode(LED4, OUTPUT); pinMode(LED5, OUTPUT); pinMode(LED6, OUTPUT); pinMode(button1, INPUT); pinMode(button2, INPUT); } void loop() { if (digitalRead(button1) == HIGH) { if (buttonActive == false) { buttonActive = true; buttonTimer = millis(); } button1Active = true; } if (digitalRead(button2) == HIGH) { if (buttonActive == false) { buttonActive = true; buttonTimer = millis(); } button2Active = true; } if ((buttonActive == true) && (millis() - buttonTimer > buttonTime) && (longPressActive == false)) { longPressActive = true; if ((button1Active == true) && (button2Active == true)) { LED4Active = !LED4Active; digitalWrite(LED4, LED4Active); } else if((button1Active == true) && (button2Active == false)) { LED5Active = !LED5Active; digitalWrite(LED5, LED5Active); } else { LED6Active = !LED6Active; digitalWrite(LED6, LED6Active); } } if ((buttonActive == true) && (digitalRead(button1) == LOW) && (digitalRead(button2) == LOW)) { if (longPressActive == true) { longPressActive = false; } else { if ((button1Active == true) && (button2Active == true)) { LED1Active = !LED1Active; digitalWrite(LED1, LED1Active); } else if ((button1Active == true) && (button2Active == false)) { LED2Active = !LED2Active; digitalWrite(LED2, LED2Active); } else { LED3Active = !LED3Active; digitalWrite(LED3, LED3Active); } } buttonActive = false; button1Active = false; button2Active = false; } } void setup {pinMode (LED1, OUTPUT); pinMode (LED2, OUTPUT); pinMode (button, INPUT);} void loop {als (digitalRead(button) == HIGH) {}Als ((millis() - buttonTimer > longPressTime) & & (longPressActive == false)) {longPressActive = true; LED1State =! LED1State; digitalWrite (LED1, LED1State); }} else {als (buttonActive == true) {}