This repository has been archived on 2024-01-26. You can view files and clone it, but cannot push or open issues or pull requests.
hackerspace-status-arduino/status/status.cpp

201 lines
4.7 KiB
C++
Raw Permalink Normal View History

2016-05-11 00:03:35 +02:00
#include <cstddef>
#include <climits>
#include "Arduino.h"
void setup();
void testLeds();
void loop();
void changeStateTo(char state_new);
bool transition();
void sendState();
unsigned long calcStateTime();
2014-01-27 22:37:49 +01:00
/*
* es gibt folgende Zustände:
* 0 - Aus
* 1 - An, aber auf dem weg zu aus
* 2 - An
*/
2016-05-11 00:03:35 +02:00
constexpr char STATE_OFF = 3;
constexpr char STATE_HALF = 1;
constexpr char STATE_ON = 2;
2014-01-27 22:37:49 +01:00
/*
* Zeit wie lange in einem Zustände verharrt werden soll
* bis zum nächsten umgeschaltet wird in Millisekunden.
* TIME_HALF - Zeitspanne von Zustand 2 bis Wechsel zu Zustand 1
* TIME_OFF - Zeitspanne von Zustand 2 bis Wechsel zu Zustand 0
*/
2016-05-11 00:03:35 +02:00
constexpr int TIME_HALF = 5400000; // 1,5h
constexpr int TIME_OFF = 7200000; // 2h
2014-01-27 22:37:49 +01:00
// Ein-/Ausgänge Bezeichnen
2016-05-11 00:03:35 +02:00
constexpr int BTN_ON = 2; // Einschalter
constexpr int BTN_OFF = 3; // Ausschalter
constexpr int LED_G = 9; // grüne LED
constexpr int LED_Y = 8; // gelbe LED
constexpr int LED_R = 7; // rote LED
2014-01-27 22:37:49 +01:00
// hier wird der aktuelle und vorherige Zustand gespeichert
2016-05-11 00:03:35 +02:00
char state_current = STATE_OFF;
char state_previous = STATE_OFF;
2014-01-27 22:37:49 +01:00
// hier wird der Beginn des aktuellen Zustand gespeichert in Millisekunden nach Uptime.
unsigned long stateBegan;
// Debouncer
class Debounce
{
public:
Debounce(int pin);
2016-05-11 00:03:35 +02:00
bool update();
2014-01-27 22:37:49 +01:00
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
void setup() {
pinMode(LED_G, OUTPUT);
pinMode(LED_Y, OUTPUT);
pinMode(LED_R, OUTPUT);
Serial.begin(9600);
2015-09-24 13:58:16 +02:00
testLeds();
changeStateTo(STATE_OFF);
2014-01-27 22:37:49 +01:00
}
2015-09-24 13:58:16 +02:00
// Schaltet alle LEDs nacheinander an
void testLeds() {
digitalWrite(LED_R, HIGH);
delay(1000);
digitalWrite(LED_Y, HIGH);
delay(1000);
digitalWrite(LED_G, HIGH);
delay(1000);
}
// wechselt zu neuen Zustand
2016-05-11 00:03:35 +02:00
void changeStateTo(char state_new) {
state_previous = state_current;
state_current = state_new;
transition();
}
// behandelt die Zustandübergänge
2016-05-11 00:03:35 +02:00
bool transition() {
if (state_previous == STATE_OFF && state_current == STATE_ON) {
digitalWrite(LED_R, LOW);
digitalWrite(LED_G, HIGH);
stateBegan = millis();
return true;
}
if (state_previous == STATE_ON && state_current == STATE_ON) { // STATE_ON ist reflexiv
stateBegan = millis();
return true;
}
if (state_previous == STATE_ON && state_current == STATE_HALF) {
digitalWrite(LED_G, LOW);
digitalWrite(LED_Y, HIGH);
return true;
}
if (state_previous == STATE_ON && state_current == STATE_OFF) {
digitalWrite(LED_G, LOW);
digitalWrite(LED_R, HIGH);
return true;
}
if (state_previous == STATE_HALF && state_current == STATE_OFF) {
digitalWrite(LED_Y, LOW);
digitalWrite(LED_R, HIGH);
return true;
}
2015-09-28 22:37:19 +02:00
if (state_previous == STATE_HALF && state_current == STATE_ON) {
digitalWrite(LED_Y, LOW);
digitalWrite(LED_G, HIGH);
stateBegan = millis();
return true;
}
if (state_previous == NULL && state_current == STATE_OFF) {
digitalWrite(LED_G, LOW);
digitalWrite(LED_Y, LOW);
digitalWrite(LED_R, HIGH);
return true;
}
return false;
}
// information über aktuellen Zustand auf die Serielle Verbindung schreiben
void sendState() {
if (state_current == STATE_ON || state_current == STATE_HALF) {
Serial.print("1");
} else {
Serial.print("0");
}
2014-01-27 22:37:49 +01:00
}
unsigned long calcStateTime() {
// Variablen überlauf von millis erkennen
2016-05-10 21:02:35 +02:00
unsigned long current_uptime = millis();
// kein überlauf
if (current_uptime > stateBegan) {
return current_uptime - stateBegan;
2014-01-27 22:37:49 +01:00
}
2016-05-10 21:02:35 +02:00
return current_uptime + (ULONG_MAX - stateBegan);
2014-01-27 22:37:49 +01:00
}
// wird nach dem Starten dauerhaft ausgeführt
void loop() {
// Einschalter auslesen
if (debounceBtnOn.update() && debounceBtnOn.read()) {
changeStateTo(STATE_ON);
2014-01-27 22:37:49 +01:00
}
// Ausschalter auslesen
if (debounceBtnOff.update() && debounceBtnOff.read()) {
changeStateTo(STATE_OFF);
2014-01-27 22:37:49 +01:00
}
// Auswertung des aktuellen Zustandes
// ggf Zustand wechseln
if (state_current == STATE_ON) {
2014-01-27 22:37:49 +01:00
if (calcStateTime() >= TIME_HALF) {
changeStateTo(STATE_HALF);
2014-01-27 22:37:49 +01:00
}
} else if (state_current == STATE_HALF && calcStateTime() >= TIME_OFF) {
changeStateTo(STATE_OFF);
2014-01-27 22:37:49 +01:00
}
// kommunizieren
sendState();
2015-07-14 23:53:49 +02:00
delay(10);
2014-01-27 22:37:49 +01:00
}
// Debouncer Klasse
Debounce::Debounce(int pin)
{
pinMode(pin, INPUT);
this->_pin = pin;
this->_time = 0;
this->_state = LOW;
this->_delay = 50;
}
2016-05-11 00:03:35 +02:00
bool Debounce::update()
2014-01-27 22:37:49 +01:00
{
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()
{
return this->_state;
}