Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: Button Blink
- - Source Code compiled for: Arduino Uno
- - Source Code created on: 2024-04-07 11:37:14
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* When the EasyButton connected to pin D2 is */
- /* pressed, the green LED connected to pin D4 shall */
- /* blink precisely 10 times and then stop blinking, */
- /* waiting for the EasyButton to be pressed again. */
- /****** SYSTEM REQUIREMENT 2 *****/
- /* Αφού η πράσινη λυχνία LED στον ακροδέκτη D4 */
- /* ολοκληρώσει να αναβοσβήνει, η κόκκινη λυχνία LED */
- /* στον ακροδέκτη D3 θα πρέπει να παραμείνει αναμμένη */
- /* για 10 δευτερόλεπτα και στη συνέχεια να σβήσει. */
- /****** END SYSTEM REQUIREMENTS *****/
- /****** DEFINITION OF LIBRARIES *****/
- #include <EasyButton.h>
- /****** SYSTEM REQUIREMENTS *****/
- const uint8_t BUTTON_PIN = 2;
- const uint8_t GREEN_LED_PIN = 4;
- const uint8_t RED_LED_PIN = 3;
- const int BLINK_COUNT = 10;
- const int BLINK_DURATION = 200;
- // Instance of the button.
- EasyButton button(BUTTON_PIN);
- // Variables to track the state of the system requirements.
- bool isGreenLedBlinking = false;
- bool isRedLedOn = false;
- int blinkCount = 0;
- void setup()
- {
- pinMode(BUTTON_PIN, INPUT_PULLUP);
- pinMode(GREEN_LED_PIN, OUTPUT);
- pinMode(RED_LED_PIN, OUTPUT);
- // Initialize the button.
- button.begin();
- button.onPressedFor(BLINK_DURATION * BLINK_COUNT, []() {
- isGreenLedBlinking = false;
- digitalWrite(GREEN_LED_PIN, LOW);
- isRedLedOn = true;
- digitalWrite(RED_LED_PIN, HIGH);
- delay(10000);
- isRedLedOn = false;
- digitalWrite(RED_LED_PIN, LOW);
- });
- }
- void loop()
- {
- // Continuously read the status of the button.
- button.read();
- if (button.wasPressed())
- {
- if (!isGreenLedBlinking)
- {
- blinkCount = 0;
- isGreenLedBlinking = true;
- digitalWrite(GREEN_LED_PIN, HIGH);
- delay(BLINK_DURATION);
- }
- else
- {
- blinkCount++;
- if (blinkCount >= BLINK_COUNT)
- {
- isGreenLedBlinking = false;
- digitalWrite(GREEN_LED_PIN, LOW);
- }
- else
- {
- delay(BLINK_DURATION);
- digitalWrite(GREEN_LED_PIN, !digitalRead(GREEN_LED_PIN));
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement