Compare commits
4 commits
master
...
use-interr
Author | SHA1 | Date | |
---|---|---|---|
|
22f8aca3ca | ||
|
096155c6cd | ||
|
59ea61af0f | ||
|
31dec770d7 |
1 changed files with 29 additions and 53 deletions
|
@ -22,33 +22,22 @@
|
||||||
|
|
||||||
// Ein-/Ausgänge Bezeichnen
|
// Ein-/Ausgänge Bezeichnen
|
||||||
const int BTN_ON = 2; // Einschalter
|
const int BTN_ON = 2; // Einschalter
|
||||||
|
#define INTERRUPT_NAME_BTN_ON 0
|
||||||
const int BTN_OFF = 3; // Ausschalter
|
const int BTN_OFF = 3; // Ausschalter
|
||||||
|
#define INTERRUPT_NAME_BTN_OFF 1
|
||||||
const int LED_G = 9; // grüne LED
|
const int LED_G = 9; // grüne LED
|
||||||
const int LED_Y = 8; // gelbe LED
|
const int LED_Y = 8; // gelbe LED
|
||||||
const int LED_R = 7; // rote LED
|
const int LED_R = 7; // rote LED
|
||||||
|
|
||||||
// hier wird der aktuelle Zustand gespeichert
|
// hier wird der aktuelle Zustand gespeichert
|
||||||
byte state = STATE_OFF;
|
volatile byte state = STATE_OFF;
|
||||||
|
|
||||||
// hier wird der Beginn des aktuellen Zustand gespeichert in Millisekunden nach Uptime.
|
// hier wird der Beginn des aktuellen Zustand gespeichert in Millisekunden nach Uptime.
|
||||||
unsigned long stateBegan;
|
volatile unsigned long stateBegan;
|
||||||
|
|
||||||
// Debouncer
|
const long debouncing_time = 50; //Debouncing Time in Milliseconds
|
||||||
class Debounce
|
volatile unsigned long last_buttonOnPressed;
|
||||||
{
|
volatile unsigned long last_buttonOffPressed;
|
||||||
public:
|
|
||||||
Debounce(int pin);
|
|
||||||
boolean update();
|
|
||||||
int read();
|
|
||||||
private:
|
|
||||||
int _pin;
|
|
||||||
int _state;
|
|
||||||
int _time;
|
|
||||||
int _delay;
|
|
||||||
};
|
|
||||||
|
|
||||||
Debounce debounceBtnOn(BTN_ON);
|
|
||||||
Debounce debounceBtnOff(BTN_OFF);
|
|
||||||
|
|
||||||
// wird einmalig beim Start des Arduinos ausgeführt
|
// wird einmalig beim Start des Arduinos ausgeführt
|
||||||
void setup() {
|
void setup() {
|
||||||
|
@ -56,7 +45,8 @@ void setup() {
|
||||||
pinMode(LED_Y, OUTPUT);
|
pinMode(LED_Y, OUTPUT);
|
||||||
pinMode(LED_R, OUTPUT);
|
pinMode(LED_R, OUTPUT);
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
setStateOnLeds();
|
attachInterrupt(INTERRUPT_NAME_BTN_ON, buttonOnPressedDebounce, RISING);
|
||||||
|
attachInterrupt(INTERRUPT_NAME_BTN_OFF, buttonOffPressedDebounce, RISING);
|
||||||
}
|
}
|
||||||
|
|
||||||
// bildet den aktuellen Zustand auf die LEDs ab
|
// bildet den aktuellen Zustand auf die LEDs ab
|
||||||
|
@ -77,18 +67,7 @@ unsigned long calcStateTime() {
|
||||||
|
|
||||||
// wird nach dem Starten dauerhaft ausgeführt
|
// wird nach dem Starten dauerhaft ausgeführt
|
||||||
void loop() {
|
void loop() {
|
||||||
// Einschalter auslesen
|
setStateOnLeds();
|
||||||
if (debounceBtnOn.update() && debounceBtnOn.read()) {
|
|
||||||
state = STATE_ON;
|
|
||||||
stateBegan = millis();
|
|
||||||
setStateOnLeds();
|
|
||||||
}
|
|
||||||
// Ausschalter auslesen
|
|
||||||
if (debounceBtnOff.update() && debounceBtnOff.read()) {
|
|
||||||
state = STATE_OFF;
|
|
||||||
setStateOnLeds();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Auswertung des aktuellen Zustandes
|
// Auswertung des aktuellen Zustandes
|
||||||
// ggf Zustand wechseln
|
// ggf Zustand wechseln
|
||||||
if (state == STATE_ON) {
|
if (state == STATE_ON) {
|
||||||
|
@ -110,28 +89,25 @@ void loop() {
|
||||||
delay(10);
|
delay(10);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Debouncer Klasse
|
void buttonOnPressedDebounce() {
|
||||||
Debounce::Debounce(int pin)
|
if (millis() - last_buttonOnPressed >= debouncing_time) {
|
||||||
{
|
buttonOnPressed();
|
||||||
pinMode(pin, INPUT);
|
last_buttonOnPressed = millis();
|
||||||
this->_pin = pin;
|
|
||||||
this->_time = 0;
|
|
||||||
this->_state = LOW;
|
|
||||||
this->_delay = 50;
|
|
||||||
}
|
|
||||||
boolean Debounce::update()
|
|
||||||
{
|
|
||||||
if (millis() - this->_time >= this->_delay) {
|
|
||||||
int reading = digitalRead(this->_pin);
|
|
||||||
if (reading != this->_state) {
|
|
||||||
this->_time = millis();
|
|
||||||
this->_state = reading;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
int Debounce::read()
|
|
||||||
{
|
void buttonOnPressed() {
|
||||||
return this->_state;
|
state = STATE_ON;
|
||||||
|
stateBegan = millis();
|
||||||
|
}
|
||||||
|
|
||||||
|
void buttonOffPressedDebounce() {
|
||||||
|
if (millis() - last_buttonOffPressed >= debouncing_time) {
|
||||||
|
buttonOffPressed();
|
||||||
|
last_buttonOffPressed = millis();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void buttonOffPressed() {
|
||||||
|
state = STATE_OFF;
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue