Skip to content
Open

17 #16

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions Бабинцев Сергей.h/2lab17/2lab.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <Servo.h>
#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);
}
}
47 changes: 47 additions & 0 deletions Бабинцев Сергей.h/2lab17/button.h
Original file line number Diff line number Diff line change
@@ -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;
};