diff --git "a/\320\221\320\260\320\261\320\270\320\275\321\206\320\265\320\262 \320\241\320\265\321\200\320\263\320\265\320\271.h/2lab17/2lab.ino" "b/\320\221\320\260\320\261\320\270\320\275\321\206\320\265\320\262 \320\241\320\265\321\200\320\263\320\265\320\271.h/2lab17/2lab.ino" new file mode 100644 index 0000000..0937bef --- /dev/null +++ "b/\320\221\320\260\320\261\320\270\320\275\321\206\320\265\320\262 \320\241\320\265\321\200\320\263\320\265\320\271.h/2lab17/2lab.ino" @@ -0,0 +1,23 @@ +#include +#include "button.h" + +#define buttonPin 1 +#define servoPin 2 + +Servo myservo; +Button buttonOn(buttonPin); + +void setup() +{ + myservo.attach(servoPin); + myservo.write(0); +} + +void loop() { + if (buttonOn.wasPressed()) + { + myservo.write(90); + delay(3000); + myservo.write(0); + } +} diff --git "a/\320\221\320\260\320\261\320\270\320\275\321\206\320\265\320\262 \320\241\320\265\321\200\320\263\320\265\320\271.h/2lab17/button.h" "b/\320\221\320\260\320\261\320\270\320\275\321\206\320\265\320\262 \320\241\320\265\321\200\320\263\320\265\320\271.h/2lab17/button.h" new file mode 100644 index 0000000..f7d4c1f --- /dev/null +++ "b/\320\221\320\260\320\261\320\270\320\275\321\206\320\265\320\262 \320\241\320\265\321\200\320\263\320\265\320\271.h/2lab17/button.h" @@ -0,0 +1,47 @@ +#pragma once + +#define BUTTON_DEBOUNCE_DELAY 50 + +class Button +{ +public: + Button(int _pin) + { + pin = _pin; + state = LOW; + debounceState = LOW; + + pinMode(pin, INPUT_PULLUP); + } + + bool wasPressed() + { + bool result = false; + int lastSeen = state; + readButtonWithDebounce(); + + if (lastSeen == HIGH && state == LOW) + result = true; + + return result; + } + + void readButtonWithDebounce() + { + int current = digitalRead(pin); + + if (current != debounceState) + debounceLastMs = millis(); + + if ((millis() - debounceLastMs) > BUTTON_DEBOUNCE_DELAY) + state = current; + + debounceState = current; + } + +private: + int pin; + int state; + int debounceState; + unsigned long debounceLastMs; +};