Advertisement
kamiln61

Untitled

Apr 12th, 2021
705
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.63 KB | None | 0 0
  1. /*
  2.   Opis funckcji:
  3.   Przyciśnięcie przycisku odpala kaskadę i po 1 rundzie wyłącza się. Po wyłączeniu miga dioda LED_BUILTIN.
  4.   Po przyciśnięciu np 3/4s odpala się funckja ledTest() i wyłącza się didoda 13
  5. */
  6.  
  7. #include <SoftwareSerial.h>
  8.  
  9. /* Inicjalizacja zmiennych */
  10. int ledArray[5] = {7, 6, 5, 4, 3};
  11. int ledState[5] = {1, 1, 1, 1, 0};
  12. int count = 0;
  13. int timer = 200;
  14. unsigned long time_to_press = 3000;//change if you want more time to press to accept as a press
  15.  
  16. bool is_pressed = false;
  17. bool is_clicked = false;
  18.  
  19. int button_pin = 2; // button pin
  20. int led_builtin = 13; //built in led
  21. int btnState = 0;
  22. int lastbtnState = 0;
  23.  
  24. unsigned long count_time = 0;
  25. unsigned long end_time = 0;
  26. unsigned long elapsed_time = 0;
  27.  
  28. bool flashing_led = false;
  29. unsigned long prevmillis = 0;
  30.  
  31. int led_state = LOW;
  32. int second_click = 1;
  33. //int second_click = 0;
  34.  
  35. void setup() {
  36.   Serial.begin(115200);
  37.   //Keep in mind the pull-up means the pushbutton's logic is inverted. It goes
  38.   // HIGH when it's open, and LOW when it's pressed.
  39.   pinMode(button_pin, INPUT_PULLUP); //INPUT_PULLUP <- internal pullup resistor
  40.   pinMode(led_builtin, OUTPUT);
  41.   for (count = 0; count < 5; count++) {
  42.     pinMode(ledArray[count], OUTPUT);
  43.   }
  44.   LedTest();
  45.   digitalWrite(led_builtin, HIGH);
  46.   delay(500);
  47.   digitalWrite(led_builtin, LOW);
  48. }
  49. /* Funkcja użytkownika
  50.   Kaskadowe zapalenie i gaszenie diód LED  */
  51. void LedCascada_on_off() {
  52.   for (count = 0; count < 5; count++) {
  53.     if (ledState[count] == 1 ) {
  54.       digitalWrite(ledArray[count], HIGH);
  55.       delay(timer);
  56.       digitalWrite(ledArray[count], LOW);
  57.       delay(timer);
  58.     }
  59.     else {
  60.     }
  61.   }
  62. }
  63. void LedCascada_off() {
  64.   for (count = 0; count < 5; count++) {
  65.     digitalWrite(ledArray[count], LOW);
  66.     delay(10);
  67.   }
  68. }
  69. /* Funkcja użytkownika
  70.   Zapalenie i zgaszenie wszystkich diód LED jednocześnie  */
  71. void LedTest() {
  72.   for (count = 0; count < 5; count++) {
  73.     digitalWrite(ledArray[count], HIGH);
  74.   }
  75.   delay (2000);
  76.   for (count = 0; count < 5; count++) {
  77.     digitalWrite(ledArray[count], LOW);
  78.   }
  79. }
  80. unsigned long Btn_check() {
  81.   if (btnState == LOW) {
  82.     count_time = millis();
  83.     Serial.println("Btn pressed");
  84.   } else if (btnState = HIGH) {
  85.     end_time = millis();
  86.     Serial.println("Btn released");
  87.     elapsed_time = end_time - count_time;
  88.     Serial.println(elapsed_time);
  89.     if (elapsed_time > 300 && elapsed_time < time_to_press) {
  90.      
  91.       Serial.println("Button_status:clicked");
  92.       flashing_led = true;
  93.       second_click++;
  94.       LedCascada_on_off();
  95.       LedCascada_off();
  96.     } else if (elapsed_time >= time_to_press) {
  97.       Serial.println("Button_status:pressed");
  98.       LedTest();
  99.       flashing_led = false;
  100.     }
  101.   }
  102.   return elapsed_time;
  103.   elapsed_time = 0;
  104. }
  105. void Flashing_led_function() {
  106.   unsigned long led_time = millis();
  107.   if (led_time - prevmillis >= 1000) {
  108.     prevmillis = led_time;
  109.     if (led_state == LOW) {
  110.       led_state = HIGH;
  111.     } else {
  112.       led_state = LOW;
  113.     }
  114.     digitalWrite(led_builtin, led_state);
  115.   }
  116. }
  117. void Flashing_led_off() {
  118.   led_state = LOW;
  119.   digitalWrite(led_builtin, led_state);
  120. }
  121. void loop() {
  122.   btnState = digitalRead(button_pin);
  123.   if (btnState != lastbtnState) {
  124.     Btn_check();
  125.     delay(200); // delay before new check, to prevent from clicking twice in the same time
  126.   }
  127.   lastbtnState = btnState;
  128.   if (flashing_led == true) {
  129.     Flashing_led_function();
  130.   }
  131.   if (flashing_led == false) {
  132.     Flashing_led_off();
  133.   }
  134.   if((second_click%2)== 0){
  135.     LedCascada_on_off();
  136.   }
  137.   if((second_click%2)==1){
  138.     LedCascada_off();}
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement