So hier in mein template die functions von xy-geklaut und eingesetzt.
Wenn man einen Code verallgemeinert und universalisiert dann wird er länger.
aber erst einmal ein Überblick wie es funktioniert
es gibt eine function
GetToggleSwitchState()
die entweder true oder false zurückgibt
Mit jedem Tastendruck wechselt es zwischen true und false
Die Auswertung des Tasters ist entprellt.
In function loop() wird der "Schaltzustand" abgefragt und dann eine function aufgerufen
die als Parameter die "Feuer ein" / "Feuer aus" -Information hat.
void loop () {
activationMode = GetToggleSwitchState(); // must be executed all the time
execute_if_Active(activationMode); // function that does what its name says
}
Das bedeutet die Variable activationMode hat also entweder den Wert true odr false
Abhängig von diesem true/false werden dann die LEDs entweder feuergeflackert oder ausgeschaltet
void execute_if_Active(bool p_IsActivated) {
if (p_IsActivated) {
// prüfe ob die Wartezeit die in Variable Blinkpause gespeichert ist
// schon vorbei ist
if ( TimePeriodIsOver(BlinkTimerStarted, blinkPause) ) {
// wenn Wartezeit WIRKLICH vorbei dann
ledFeuer(); // LED Helligkeit neu setzen
blinkPause = random(100) + 100; // neue Blinkopausenzeit speichern
}
}
else { // DE-activated
ledAus();
}
PrintToSerialMonitor(p_IsActivated); // deactivate through commenting if not wanted
}
Das Flackern wird nicht-blockierend gemacht damit das Programm jederzeit innerhalb von Millisekunden auf das betätigen des Tasters reagieren kann.
So viel zur prinzipiellen Funktion die ganzen restlichen Zeilen machen die Details.
Schön aufgeteilt in functions die alle einen selbsterklärenden Namen haben.
getestet durch Umbiegen von Led3 auf onboard-LED (io-pin 13) eines Arduino-Unos
So jetzt kannst auswählen welchen Code du benutzen willst.
#define ProjectName "elektronikfreak start stop 004"
// define IO-states for inputs with pull-up-resistors
// pull-up-resistors invert the logig
#define unPressed HIGH
#define pressed LOW
const byte ToggleButtonPin = 8;
const byte Led1 = 9;
const byte Led2 = 10;
const byte Led3 = 11;
bool activationMode = false;
unsigned long BlinkTimerStarted = 0;
unsigned long blinkPause = 150;
void setup() {
Serial.begin(115200); // adjust baudrate in the serial monitor to match the number
Serial.println( F("Setup-Start") );
printFileNameDateTime();
pinMode (LED_BUILTIN, OUTPUT); // used for indicating logging active or not
digitalWrite(LED_BUILTIN, LOW);
// wire button between IO-pin and GND
// Pull-up-resistor inverts the logic
// unpressed: IO-pin detects HIGH
// pressed: IO-Pin detects LOW
pinMode(ToggleButtonPin, INPUT_PULLUP);
pinMode(Led1, OUTPUT);
pinMode(Led2, OUTPUT);
pinMode(Led3, OUTPUT);
ledAus();
BlinkTimerStarted = millis();
}
void loop () {
activationMode = GetToggleSwitchState(); // must be executed all the time
execute_if_Active(activationMode); // function that does what its name says
}
bool GetToggleSwitchState() {
// "static" makes variables persistant over function calls
static bool toggleState = false;
static byte buttonStateOld = unPressed;
unsigned long buttonDebounceTime = 50;
unsigned long buttonDebounceTimer = 0;
byte buttonStateNew;
if ( TimePeriodIsOver(buttonDebounceTimer, buttonDebounceTime) ) {
// if more time than buttonDebounceTime has passed by
// this means let pass by some time until
// bouncing of the button is over
buttonStateNew = digitalRead(ToggleButtonPin);
if (buttonStateNew != buttonStateOld) {
// if button-state has changed
buttonStateOld = buttonStateNew;
if (buttonStateNew == unPressed) {
// if button is released
toggleState = !toggleState; // toggle state-variable
} // the attention-mark is the NOT operator
} // which simply inverts the boolean state
} // !true = false NOT true is false
// !false = true NOT false is true
return toggleState;
}
void ledFeuer() {
analogWrite(Led1, random(220) + 35);
analogWrite(Led2, random(220) + 35);
analogWrite(Led3, random(220) + 35);
}
void ledAus() {
analogWrite(Led1, LOW);
analogWrite(Led2, LOW);
analogWrite(Led3, LOW);
}
void execute_if_Active(bool p_IsActivated) {
if (p_IsActivated) {
// prüfe ob die Wartezeit die in Variable Blinkpause gespeichert ist
// schon vorbei ist
if ( TimePeriodIsOver(BlinkTimerStarted, blinkPause) ) {
// wenn Wartezeit vorbei dann
ledFeuer(); // LED Helligkeit neu setzen
blinkPause = random(100) + 100; // neue Blinkopausenzeit speichern
}
}
else { // DE-activated
ledAus();
}
PrintToSerialMonitor(p_IsActivated); // deactivate through commenting if not wanted
}
// helper-function ignore at first
void printFileNameDateTime() {
Serial.print( F("File : ") );
Serial.println( F(__FILE__) );
Serial.print( F("Date : ") );
Serial.println( F(__DATE__) );
Serial.print( F("Project: ") );
Serial.println( F(ProjectName) );
}
// ignore at first
// helper-function for easy to use non-blocking timing
boolean TimePeriodIsOver (unsigned long & expireTime, unsigned long TimePeriod) {
unsigned long currentMillis = millis();
if ( currentMillis - expireTime >= TimePeriod ) {
expireTime = currentMillis; // set new expireTime
return true; // more time than TimePeriod) has elapsed since last time if-condition was true
}
else return false; // not expired
}
void PrintToSerialMonitor(boolean p_IsActivated) {
static bool lastIsActivated;
// only in case the activation-mode has CHANGED print ONE time
if (p_IsActivated != lastIsActivated) {
// only if state of parameter p_logIsActivated has changed
if (p_IsActivated) {
Serial.println();
Serial.println("start executing");
Serial.println();
digitalWrite(LED_BUILTIN, HIGH);
}
else { // not activated
ledAus();
Serial.println();
Serial.println("stopp executing");
Serial.println();
digitalWrite(LED_BUILTIN, LOW);
}
lastIsActivated = p_IsActivated; // update variable lastSDlogActive
}
}
vgs