#include <Adafruit_NeoPixel.h>
#define TAGFAHRLICHTSCHALTER 2
#define BLINKERLINKSSCHALTER 3
#define PIXEL_PIN 5 // Digital IO pin connected to the NeoPixels.
#define PIXEL_COUNT 16 // Number of NeoPixels
Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);
bool b_tagfahrlicht=false; //Bool State für Tagfahrlicht
bool b_blinkerlinks=false; //Bool State für Blinker links
uint32_t blinkcounter = 0; //statischer Zähler für Blinker Links
uint32_t i_blinkerlinks = 0; //Speicher zum gucken wann Blinker angemacht wurde
void setup() {
pinMode(TAGFAHRLICHTSCHALTER, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(TAGFAHRLICHTSCHALTER), TagfahrlichtISR, CHANGE);
pinMode(BLINKERLINKSSCHALTER, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(BLINKERLINKSSCHALTER), BlinkerlinksISR, CHANGE);
Serial.begin(115200);
strip.begin(); // Initialize NeoPixel strip object (REQUIRED)
strip.show(); // Initialize all pixels to 'off'
}
void loop() {
static uint32_t lastdebug=0; //statische 32bit unsigned int für debug Interval. inhalt bleibt loop übergreifend erhalten (statisch), aber ist nur im loop aufrufbar!
if(millis()-lastdebug>=1000){
lastdebug=millis();
Serial.print(F("Debug: "));
Serial.println(lastdebug/1000);
Serial.print(F("Tagfahrlicht: "));
Serial.println(b_tagfahrlicht ? "an" : "aus");
Serial.print(F("Blinkschalter links: "));
Serial.println(b_blinkerlinks ? "an" : "aus");
Serial.print(F("Blinkschalter Zeit: "));
Serial.println(i_blinkerlinks/1000);
Serial.println();Serial.println();
}
blink();
tagfahrlicht();
}
void TagfahrlichtISR(){
b_tagfahrlicht = !digitalRead(TAGFAHRLICHTSCHALTER); //invertierte Logik weil LOW aktive (INPUT_PULLUP!)
}
void BlinkerlinksISR(){
b_blinkerlinks = !digitalRead(BLINKERLINKSSCHALTER); //invertierte Logik weil LOW aktive (INPUT_PULLUP!)
if(b_blinkerlinks){ //geht in Wokwi irgendwie nicht richtig??
i_blinkerlinks = millis();
}
}
void tagfahrlicht(){
if(b_blinkerlinks || blinkcounter > 0) return; //abbruch beim Blinken
uint32_t color = 0; //farbe 0 -> aus
if(b_tagfahrlicht) color = 16777215; //wenn schalter Tagfahrlicht an -> Farbe weiß
colorsetzen(color);
}
void blink(){
static bool blinkrun = false; //bool zum merken ob blinker gesetzt wurde
if(b_blinkerlinks || blinkrun){
blinkrun=true;
colorWipe(strip.Color( 127, 127, 0), 93); // 500ms Delay! weil nur 2 LEDs
colorsetzen(0); //aus machen
blinkcounter++;
}
if(blinkcounter>=3 && !b_blinkerlinks){
blinkrun=false; //wenn mehr als 3 mal geblinkt und schalter aus -> laufen aus!
blinkcounter = 0;
}
}
// Fill strip pixels one after another with a color. Strip is NOT cleared
// first; anything there will be covered pixel by pixel. Pass in color
// (as a single 'packed' 32-bit value, which you can get by calling
// strip.Color(red, green, blue) as shown in the loop() function above),
// and a delay time (in milliseconds) between pixels.
void colorWipe(uint32_t color, int wait) {
for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
strip.setPixelColor(i, color); // Set pixel's color (in RAM)
strip.show(); // Update strip to match
delay(wait); // Pause for a moment
}
}
void colorsetzen(uint32_t color){
for(int i=0; i<strip.numPixels(); i++) strip.setPixelColor(i, color); //alle Pixel Farbe setzen
strip.show(); //Pixel zeigen
}
//colorWipe(strip.Color( 0, 0, 0), 50); // Black/off