MC2Studio

Untitled

Dec 23rd, 2020
747
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. #define LED1    D0
  3. #define LED2    D1
  4. #define button  13
  5.  
  6. boolean LED1State = false;
  7. boolean LED2State = false;
  8.  
  9. boolean buttonActive = false;
  10. boolean longPressActive = false;
  11.  
  12. unsigned long buttonTimer = 0;
  13. unsigned long longPressTime = 450;
  14.  
  15. void blink()
  16.    {
  17.  
  18.             buttonActive = true;
  19.             buttonTimer = millis();
  20.    }
  21. void switcher(){
  22.  
  23.     if (digitalRead(button) == LOW) {
  24.         if ((millis() - buttonTimer > longPressTime) && (longPressActive == false)) {
  25.             longPressActive = true;
  26.             LED1State = !LED1State;
  27.             digitalWrite(LED1, LED1State);
  28.         }
  29.     } else {
  30.         if (buttonActive == true) {
  31.             if (longPressActive == true) {
  32.                 longPressActive = false;
  33.             } else {
  34.                 LED2State = !LED2State;
  35.                 digitalWrite(LED2, LED2State);
  36.             }
  37.             buttonActive = false;
  38.         }
  39.     }
  40.  
  41. }
  42. void setup() {
  43. Serial.begin ( 115200 );
  44.     pinMode(LED1, OUTPUT);
  45.     pinMode(LED2, OUTPUT);
  46.     pinMode(button, INPUT);
  47. Serial.println("test");
  48.  // attachInterrupt(digitalPinToInterrupt(15), blink,FALLING); //1 czyli INT1 pin 3
  49. }
  50.  
  51.  
  52.  
  53. void loop() {
  54.  
  55. switcher();
  56.  
  57. }
RAW Paste Data