mirror of
https://github.com/KevinMidboe/Arduino.git
synced 2026-04-25 08:14:06 +00:00
Init commit with many years of arduino sketches and projects. I dont know if the esp8266 includes much, but there are also libraries. I hope they dont have crazy automatic versioning through the Arduino IDE.
This commit is contained in:
62
Projects/libraries/plab-library-master/PushButton.cpp
Normal file
62
Projects/libraries/plab-library-master/PushButton.cpp
Normal file
@@ -0,0 +1,62 @@
|
||||
#include "Arduino.h"
|
||||
#include "PushButton.h"
|
||||
|
||||
#define DEBOUNCE 20 // button debouncer, how many ms to debounce, 5+ ms is usually plenty
|
||||
|
||||
PushButton::PushButton(int pin)
|
||||
{
|
||||
_pin = pin;
|
||||
_pressed = 0;
|
||||
justpressed = 0;
|
||||
justreleased = 0;
|
||||
previousstate = 0;
|
||||
currentstate = 0;
|
||||
lasttime = millis();
|
||||
pinMode(_pin, INPUT);
|
||||
}
|
||||
|
||||
void PushButton::update()
|
||||
{
|
||||
justreleased = 0;
|
||||
justpressed = 0;
|
||||
|
||||
if ((lasttime + DEBOUNCE) > millis()) {
|
||||
// not enough time has passed to debounce
|
||||
return;
|
||||
}
|
||||
// ok we have waited DEBOUNCE milliseconds, lets reset the timer
|
||||
lasttime = millis();
|
||||
|
||||
currentstate = digitalRead(_pin); // read the button
|
||||
|
||||
if (currentstate == previousstate) {
|
||||
if ((_pressed == LOW) && (currentstate == LOW)) {
|
||||
// just pressed
|
||||
justpressed = 1;
|
||||
}
|
||||
else if ((_pressed == HIGH) && (currentstate == HIGH)) {
|
||||
// just released
|
||||
justreleased = 1;
|
||||
}
|
||||
_pressed = !currentstate; // remember, digital HIGH means NOT pressed
|
||||
}
|
||||
//Serial.println(pressed[index], DEC);
|
||||
previousstate = currentstate; // keep a running tally of the buttons
|
||||
}
|
||||
|
||||
|
||||
boolean PushButton::isDown()
|
||||
{
|
||||
return _pressed;
|
||||
}
|
||||
|
||||
boolean PushButton::pressed()
|
||||
{
|
||||
return justpressed;
|
||||
}
|
||||
|
||||
boolean PushButton::released()
|
||||
{
|
||||
return justreleased;
|
||||
}
|
||||
|
||||
31
Projects/libraries/plab-library-master/PushButton.h
Normal file
31
Projects/libraries/plab-library-master/PushButton.h
Normal file
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
PushButton.h - Library for Button with cebounce.
|
||||
Created by Dag Svanæs, 2015.
|
||||
Released into the public domain.
|
||||
*/
|
||||
|
||||
#ifndef PushButton_h
|
||||
#define PushButton_h
|
||||
|
||||
#include "Arduino.h"
|
||||
|
||||
|
||||
#define DEBOUNCE 20 // button debouncer, how many ms to debounce, 5+ ms is usually plenty
|
||||
|
||||
class PushButton
|
||||
{
|
||||
public:
|
||||
PushButton(int pin);
|
||||
void update();
|
||||
boolean isDown();
|
||||
boolean pressed();
|
||||
boolean released();
|
||||
private:
|
||||
int _pin;
|
||||
byte _pressed, justpressed, justreleased;
|
||||
byte previousstate;
|
||||
byte currentstate;
|
||||
long lasttime;
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user