Hi zusammen,
ich habe mir aus Resten ein Fahrad-Rucksatz-Zusatzbeleuchtung gebaut.
Im Prinzip sind es 8x 2801er LEDs an einem Arduino Nano mit 3 Tastern.
Die LEDS un der Arduino werden über eine Powerbank versorgt.
Die Schaltung soll so funktionieren:
Druck1 auf Taster1 startet Blinker rechts.
Druck2 auf Taster1 stoppt Blinker rechts
Druck1 auf Taster2 startet Blinker links.
Druck2 auf Taster2 stoppt Blinker links.
Druck1 auf Taster2 startet Bremslicht.
Druck2 auf Taster2 stoppt Bremslicht.
Sonderfälle:
Blinkt Blinker R und Taster 2 wird betätigt, blinkt Blinker L.
Blinkt Blinker L und Taster 1 wird betätigt, blinkt Blinker R.
Blinkt ein Blinker und Taster 3 Wird gedrückt stoppt Blinker und Bremslicht startet.
Hier mein Code.
Ich muss noch Basics wie Debounce, Statemaschine und mehr lernen, selbst zu schreiben, verstehe aber schon fast alles.
Main Problem:
Sowohl der Blinker als auch das Bremslicht gehen nach einiger Zeit (30-90sek) einfach wieder aus.
Laut dem Verständnis meines Codes sollte das aber nicht so sein.
Kann mir jemand sagen, was ich falsch mache?
Gerne auch generelle Verbesserungen.
Hier mein "Code"
#include <Bounce2.h>
int menu;
#include "FastLED.h"
#define NUM_LEDS 8
#define DATA_PIN 2
#define CLOCK_PIN 3
CRGB leds[NUM_LEDS];
#define COLOR_ORDER BGR
#define MAX_BRIGHTNESS 100
#define BRIGHTNESS 100
uint8_t gHue = 0;
const int button1Pin = 5; // the number of the pushbutton pin
const int button2Pin = 7; // the number of the pushbutton pin
const int button3Pin = 9; // the number of the pushbutton pin
Bounce debouncer1= Bounce();
Bounce debouncer2= Bounce();
Bounce debouncer3= Bounce();
int lastButton1State = 1; // previous state of the button
int lastButton2State = 1; // previous state of the button
int lastButton3State = 1; // previous state of the button
int button1PushCounter = 1;
int button2PushCounter = 1;
int button3PushCounter = 1;
int button1State = 0;
int button2State = 0;
int button3State = 0;
int lastButtonpressed = 0;
void setup()
{
pinMode(button1Pin,INPUT_PULLUP);
debouncer1.attach(button1Pin);
debouncer1.interval(5);
pinMode(button2Pin,INPUT_PULLUP);
debouncer2.attach(button2Pin);
debouncer2.interval(5);
pinMode(button3Pin,INPUT_PULLUP);
debouncer3.attach(button3Pin);
debouncer3.interval(5);
Serial.begin(38400);
#
FastLED.addLeds<WS2801, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
FastLED.setBrightness(BRIGHTNESS);
}
void loop() {
//Check for Input
input();
while (menu == 1){
blinkrechts(0,0,0,0);
blinklinks(0,0,0,0);
blinkrechts(255,0,255,100);
blinkrechts(0,0,0,50);
}
while (menu == 2){
blinklinks(0,0,0,0);
blinkrechts(0,0,0,0);
blinklinks(255,0,255,100);
blinklinks(0,0,0,100);
}
while (menu == 3){
brems(255,0,0,1);
}
while (menu == 0){
blinkrechts(0,0,0,0);
blinklinks(0,0,0,0);
}
while (menu == 11){
blinkrechts(0,0,0,0);
blinklinks(0,0,0,0);
}
while (menu == 12){
blinkrechts(0,0,0,0);
blinklinks(0,0,0,0);
}
while (menu == 13){ /
blinkrechts(0,0,0,0);
blinklinks(0,0,0,0);
}
}
void blinklinks(uint32_t rc, uint32_t gc, uint32_t bc, uint8_t wait) {
int i;
unsigned long time = 0;
FastLED.setBrightness(BRIGHTNESS);
input();
for(int i = 3; i > -1; i--) {
//leds[i].setHSV(c, 200, 200);
leds[i] = CRGB( rc, gc, bc);
delay(wait);
FastLED.show();
input();
}
}
void blinkrechts(uint32_t rc, uint32_t gc, uint32_t bc, uint8_t wait) {
int i;
FastLED.setBrightness(BRIGHTNESS);
input();
for(int i = 4; i < 8; i++) {
//leds[i].setHSV(c, 200, 200);
leds[i] = CRGB( rc, gc, bc);
delay(wait);
FastLED.show();
input();
}
}
void brems(uint32_t rc, uint32_t gc, uint32_t bc, uint8_t wait) {
int i;
FastLED.setBrightness(BRIGHTNESS);
for(int i = 0; i < 8; i++) {
//leds[i].setHSV(c, 200, 200);
leds[i] = CRGB( rc, gc, bc);
delay(wait);
FastLED.show();
input();
}
}
void input() {
button1State = digitalRead(button1Pin);
button2State = digitalRead(button2Pin);
button3State = digitalRead(button3Pin);
if (button1State != lastButton1State) {
if (button1State == LOW) {
button1PushCounter++;
lastButtonpressed = 1;
} else {
lastButtonpressed = 1;
}
delay(50);
}
lastButton1State = button1State;
if (button2State != lastButton2State) {
if (button2State == LOW) {
button1PushCounter++;
lastButtonpressed = 2;
} else {
lastButtonpressed = 2;
}
delay(50);
}
lastButton2State = button2State;
if (button3State != lastButton3State) {
if (button3State == LOW) {
button3PushCounter++;
lastButtonpressed = 3;
} else {
lastButtonpressed = 3;
}
delay(50);
}
lastButton3State = button3State;
if (menu == 1 && lastButtonpressed == 1) {
menu = 0;
}
if (menu == 2 && lastButtonpressed == 2) {
menu = 0;
}
if (menu == 3 && lastButtonpressed == 3) {
menu = 0;
}
if (button1PushCounter % 2 == 0 && lastButtonpressed == 1) {
menu = 1;
}
if (button1PushCounter % 2 == 0 && lastButtonpressed == 2) {
menu = 2;
}
if (button3PushCounter % 2 == 0 && lastButtonpressed == 3) {
menu = 3;
}
if (menu == 1 && lastButtonpressed == 2) {
menu = 2;
}
if (menu == 2 && lastButtonpressed == 1) {
menu = 1;
}
}