Hallo,
ich versuche eine Motorsteuerung zu bauen. Dafür benötige ich 12 verschiedene Geschwindigkeiten, die über analogWrite eingestellt werden. Das ganze soll über 12 Taster, die mit LEDs ausgestattet sind, angesteuert werden. Dafür habe ich bereits einen Code geschrieben, der soweit auch funktioniert. Jedesmal wenn ich einen Taster drücke soll sich die jeweils dahinter befinde Geschwindigkeit einstelle.. Es gibt jedoch noch ein Problem: Die LEDs in den Tastern sollen immer nur so lange leuchten, bis man eine neue Geschwindigkeit, also Taster, drückt. Das funktioniert jedoch nicht, denn die LEDs leuchten dauerhaft. Ich müsste in jedem if-Statement nochmal vorherige LEDs wieder deaktivieren. Gibt es dafür eine einfachere Möglichkeit, außer alle 11 anderen LEDs immer in jedem if-Statement zu deaktivieren?
Und gibt es insgesamt eine einfachere Variante, als das was ich bisher fabriziert habe?
Anbei noch der bisherige Code:
#include <Bounce2.h>
#define BUTTON_1 44
#define BUTTON_2 45
#define BUTTON_3 42
#define BUTTON_4 43
#define BUTTON_5 40
#define BUTTON_6 41
#define BUTTON_7 38
#define BUTTON_8 39
#define BUTTON_9 36
#define BUTTON_10 37
#define BUTTON_11 34
#define BUTTON_12 35
#define LED_1 30
#define LED_2 22
#define LED_3 33
#define LED_4 31
#define LED_5 32
#define LED_6 28
#define LED_7 26
#define LED_8 29
#define LED_9 24
#define LED_10 27
#define LED_11 23
#define LED_12 25
bool light_on1 = true;
bool light_on2 = true;
bool light_on3 = true;
bool light_on4 = true;
bool light_on5 = true;
bool light_on6 = true;
bool light_on7 = true;
bool light_on8 = true;
bool light_on9 = true;
bool light_on10 = true;
bool light_on11 = true;
bool light_on12 = true;
Bounce debouncer1 = Bounce();
Bounce debouncer2 = Bounce();
Bounce debouncer3 = Bounce();
Bounce debouncer4 = Bounce();
Bounce debouncer5 = Bounce();
Bounce debouncer6 = Bounce();
Bounce debouncer7 = Bounce();
Bounce debouncer8 = Bounce();
Bounce debouncer9 = Bounce();
Bounce debouncer10 = Bounce();
Bounce debouncer11 = Bounce();
Bounce debouncer12 = Bounce();
void setup() {
pinMode(BUTTON_1, INPUT);
pinMode(BUTTON_2, INPUT);
pinMode(BUTTON_3, INPUT);
pinMode(BUTTON_4, INPUT);
pinMode(BUTTON_5, INPUT);
pinMode(BUTTON_6, INPUT);
pinMode(BUTTON_7, INPUT);
pinMode(BUTTON_8, INPUT);
pinMode(BUTTON_9, INPUT);
pinMode(BUTTON_10, INPUT);
pinMode(BUTTON_11, INPUT);
pinMode(BUTTON_12, INPUT);
pinMode(LED_1, OUTPUT);
pinMode(LED_2, OUTPUT);
pinMode(LED_3, OUTPUT);
pinMode(LED_4, OUTPUT);
pinMode(LED_5, OUTPUT);
pinMode(LED_6, OUTPUT);
pinMode(LED_7, OUTPUT);
pinMode(LED_8, OUTPUT);
pinMode(LED_9, OUTPUT);
pinMode(LED_10, OUTPUT);
pinMode(LED_11, OUTPUT);
pinMode(LED_12, OUTPUT);
analogWriteResolution(10);
debouncer1.attach(BUTTON_1);
debouncer1.interval(5);
debouncer2.attach(BUTTON_2);
debouncer2.interval(5);
debouncer3.attach(BUTTON_3);
debouncer3.interval(5);
debouncer4.attach(BUTTON_4);
debouncer4.interval(5);
debouncer5.attach(BUTTON_5);
debouncer5.interval(5);
debouncer6.attach(BUTTON_6);
debouncer6.interval(5);
debouncer7.attach(BUTTON_7);
debouncer7.interval(5);
debouncer8.attach(BUTTON_8);
debouncer8.interval(5);
debouncer9.attach(BUTTON_9);
debouncer9.interval(5);
debouncer10.attach(BUTTON_10);
debouncer10.interval(5);
debouncer11.attach(BUTTON_11);
debouncer11.interval(5);
debouncer12.attach(BUTTON_12);
debouncer12.interval(5);
}
void loop() {
// Update the Bounce instance :
debouncer1.update();
debouncer2.update();
debouncer3.update();
debouncer4.update();
debouncer5.update();
debouncer6.update();
debouncer7.update();
debouncer8.update();
debouncer9.update();
debouncer10.update();
debouncer11.update();
debouncer12.update();
if (debouncer1.fell() ) {
light_on1 = !light_on1;
digitalWrite(LED_1, light_on1 );
analogWrite(66,100);
}
if (debouncer2.fell() ) {
light_on2 = !light_on2;
digitalWrite(LED_2, light_on2 );
analogWrite(66,200);
}
if (debouncer3.fell() ) {
light_on3 = !light_on3;
digitalWrite(LED_3, light_on3 );
analogWrite(66,300);
}
if (debouncer4.fell() ) {
light_on4 = !light_on4;
digitalWrite(LED_4, light_on4 );
analogWrite(66,400);
}
if (debouncer5.fell() ) {
light_on5 = !light_on5;
digitalWrite(LED_5, light_on5 );
analogWrite(66,500);
}
if (debouncer6.fell() ) {
light_on6 = !light_on6;
digitalWrite(LED_6, light_on6 );
analogWrite(66,600);
}
if (debouncer7.fell() ) {
light_on7 = !light_on7;
digitalWrite(LED_7, light_on7 );
analogWrite(66,700);
}
if (debouncer8.fell() ) {
light_on8 = !light_on8;
digitalWrite(LED_8, light_on8 );
analogWrite(66,800);
}
if (debouncer9.fell() ) {
light_on9 = !light_on9;
digitalWrite(LED_9, light_on9 );
analogWrite(66,900);
}
if (debouncer10.fell() ) {
light_on10 = !light_on10;
digitalWrite(LED_10, light_on10 );
analogWrite(66,1000);
}
if (debouncer11.fell() ) {
light_on11 = !light_on11;
digitalWrite(LED_11, light_on11 );
analogWrite(66,10);
}
if (debouncer12.fell() ) {
light_on12 = !light_on12;
digitalWrite(LED_12, light_on12 );
analogWrite(66,1023);
}
}
Ich bin für Tips und HIlfe sehr dankbar
Vielen Dank und viele Grüße
J