ImpulsWahl/src/main.cpp

118 lines
2.4 KiB
C++

#include <Arduino.h>
#ifdef FEATURE_KEYBOARD
#include <Keyboard.h>
#endif
int dialingPin = DIALING_PIN;
int pulsePin = PULSE_PIN;
void setup() {
Serial.begin(9600);
#ifdef FEATURE_KEYBOARD
Keyboard.begin();
#endif
pinMode(dialingPin, INPUT_PULLUP);
pinMode(pulsePin, INPUT_PULLUP);
Serial.println("Start Wählscheibe");
}
bool dialing = false;
bool pulse = false;
bool dialingLast = false;
bool pulseLast = false;
bool dialingVal = false;
bool pulseVal = false;
bool dialingValLast = false;
bool pulseValLast = false;
unsigned long dialingTimeOn = 0;
unsigned long dialingTimeOff = 0;
unsigned long pulseTimeOn = 0;
unsigned long pulseTimeOff = 0;
int count = 0;
void loop() {
dialingVal = digitalRead(dialingPin) == LOW;
pulseVal = digitalRead(pulsePin) == HIGH;
// DIALING
// Schalter geschlossen UND vorher offen
if (dialingVal && !dialingValLast) {
dialingTimeOn = micros();
}
// Schalter offen UND seit mindestens einer Weile
if (dialingVal && ((micros() - dialingTimeOn) > 3000)) {
dialing = true;
}
// Schalter offen UND vorher geschlossen
if (!dialingVal && dialingValLast) {
dialingTimeOff = micros();
}
// Schalter offen UND seit mindestens einer Weile
if (!dialingVal && ((micros() - dialingTimeOff) > 3000)) {
dialing = false;
}
// PULSE
// Schalter geschlossen UND vorher offen
if (pulseVal && !pulseValLast) {
pulseTimeOn = micros();
}
// Schalter offen UND seit mindestens einer Weile
if (pulseVal && ((micros() - pulseTimeOn) > 3000)) {
pulse = true;
}
// Schalter offen UND vorher geschlossen
if (!pulseVal && pulseValLast) {
pulseTimeOff = micros();
}
// Schalter offen UND seit mindestens einer Weile
if (!pulseVal && ((micros() - pulseTimeOff) > 3000)) {
pulse = false;
}
// INTERPRET
if (dialing && !dialingLast) {
#ifdef DEBUG
Serial.println("Start dialing");
#endif
count = 0;
}
if (dialing) {
if (pulse && !pulseLast) {
#ifdef DEBUG
Serial.print("+");
#endif
count++;
}
}
if (!dialing && dialingLast) {
#ifdef DEBUG
Serial.println("");
Serial.println("Stop dialing");
#endif
unsigned int output = count % 10;
Serial.println(output);
#ifdef FEATURE_KEYBOARD
Keyboard.print(output);
#endif
}
dialingLast = dialing;
pulseLast = pulse;
dialingValLast = dialingVal;
pulseValLast = pulseVal;
}