Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Opis funckcji:
- Przyciśnięcie przycisku odpala kaskadę i po 1 rundzie wyłącza się. Po wyłączeniu miga dioda LED_BUILTIN.
- Po przyciśnięciu np 3/4s odpala się funckja ledTest() i wyłącza się didoda 13
- */
- #include <SoftwareSerial.h>
- /* Inicjalizacja zmiennych */
- int ledArray[5] = {7, 6, 5, 4, 3};
- int ledState[5] = {1, 1, 1, 1, 0};
- int count = 0;
- int timer = 200;
- unsigned long time_to_press = 3000;//change if you want more time to press to accept as a press
- bool is_pressed = false;
- bool is_clicked = false;
- int button_pin = 2; // button pin
- int led_builtin = 13; //built in led
- int btnState = 0;
- int lastbtnState = 0;
- unsigned long count_time = 0;
- unsigned long end_time = 0;
- unsigned long elapsed_time = 0;
- bool flashing_led = false;
- unsigned long prevmillis = 0;
- int led_state = LOW;
- int second_click = 1;
- //int second_click = 0;
- void setup() {
- Serial.begin(115200);
- //Keep in mind the pull-up means the pushbutton's logic is inverted. It goes
- // HIGH when it's open, and LOW when it's pressed.
- pinMode(button_pin, INPUT_PULLUP); //INPUT_PULLUP <- internal pullup resistor
- pinMode(led_builtin, OUTPUT);
- for (count = 0; count < 5; count++) {
- pinMode(ledArray[count], OUTPUT);
- }
- LedTest();
- digitalWrite(led_builtin, HIGH);
- delay(500);
- digitalWrite(led_builtin, LOW);
- }
- /* Funkcja użytkownika
- Kaskadowe zapalenie i gaszenie diód LED */
- void LedCascada_on_off() {
- for (count = 0; count < 5; count++) {
- if (ledState[count] == 1 ) {
- digitalWrite(ledArray[count], HIGH);
- delay(timer);
- digitalWrite(ledArray[count], LOW);
- delay(timer);
- }
- else {
- }
- }
- }
- void LedCascada_off() {
- for (count = 0; count < 5; count++) {
- digitalWrite(ledArray[count], LOW);
- delay(10);
- }
- }
- /* Funkcja użytkownika
- Zapalenie i zgaszenie wszystkich diód LED jednocześnie */
- void LedTest() {
- for (count = 0; count < 5; count++) {
- digitalWrite(ledArray[count], HIGH);
- }
- delay (2000);
- for (count = 0; count < 5; count++) {
- digitalWrite(ledArray[count], LOW);
- }
- }
- unsigned long Btn_check() {
- if (btnState == LOW) {
- count_time = millis();
- Serial.println("Btn pressed");
- } else if (btnState = HIGH) {
- end_time = millis();
- Serial.println("Btn released");
- elapsed_time = end_time - count_time;
- Serial.println(elapsed_time);
- if (elapsed_time > 300 && elapsed_time < time_to_press) {
- Serial.println("Button_status:clicked");
- flashing_led = true;
- second_click++;
- LedCascada_on_off();
- LedCascada_off();
- } else if (elapsed_time >= time_to_press) {
- Serial.println("Button_status:pressed");
- LedTest();
- flashing_led = false;
- }
- }
- return elapsed_time;
- elapsed_time = 0;
- }
- void Flashing_led_function() {
- unsigned long led_time = millis();
- if (led_time - prevmillis >= 1000) {
- prevmillis = led_time;
- if (led_state == LOW) {
- led_state = HIGH;
- } else {
- led_state = LOW;
- }
- digitalWrite(led_builtin, led_state);
- }
- }
- void Flashing_led_off() {
- led_state = LOW;
- digitalWrite(led_builtin, led_state);
- }
- void loop() {
- btnState = digitalRead(button_pin);
- if (btnState != lastbtnState) {
- Btn_check();
- delay(200); // delay before new check, to prevent from clicking twice in the same time
- }
- lastbtnState = btnState;
- if (flashing_led == true) {
- Flashing_led_function();
- }
- if (flashing_led == false) {
- Flashing_led_off();
- }
- if((second_click%2)== 0){
- LedCascada_on_off();
- }
- if((second_click%2)==1){
- LedCascada_off();}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement