Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
| Beide Seiten der vorigen Revision Vorhergehende Überarbeitung Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
| decoder:modellbahn-anlage.de:miningen-light-2 [07.01.2026 07:15] – [LED faden mit DCC] Martin Fitzel | decoder:modellbahn-anlage.de:miningen-light-2 [07.01.2026 11:20] (aktuell) – Martin Fitzel | ||
|---|---|---|---|
| Zeile 2: | Zeile 2: | ||
| Miningen Light 2 ist DER Decoder auf unserer [[https:// | Miningen Light 2 ist DER Decoder auf unserer [[https:// | ||
| + | |||
| + | ===== Bibliothek ===== | ||
| + | |||
| + | [[https:// | ||
| ===== Ein- und Ausgänge des Decoders ===== | ===== Ein- und Ausgänge des Decoders ===== | ||
| - | < | + | < |
| ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||
| ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||
| Zeile 61: | Zeile 65: | ||
| - | < | + | < |
| #include < | #include < | ||
| Zeile 93: | Zeile 97: | ||
| ===== LED faden mit DCC ===== | ===== LED faden mit DCC ===== | ||
| - | < | + | < |
| const int dccInputPin = 2; // Eingang vom DCC-Decoder | const int dccInputPin = 2; // Eingang vom DCC-Decoder | ||
| const int ledPin = 9; // LED-Pin (muss PWM-fähig sein: 3, 5, 6, 9, 10, 11) | const int ledPin = 9; // LED-Pin (muss PWM-fähig sein: 3, 5, 6, 9, 10, 11) | ||
| Zeile 124: | Zeile 128: | ||
| </ | </ | ||
| + | |||
| + | ===== LED schaltet hier sofort aus, wenn der DCC Befehl kommt ===== | ||
| + | |||
| + | <code c> | ||
| + | const int dccInputPin = 2; // Eingang vom DCC-Decoder | ||
| + | const int ledPin = 9; // LED-Pin (PWM-fähig: | ||
| + | |||
| + | int brightness = 0; | ||
| + | int fadeAmount = 5; | ||
| + | bool fadeIn = true; | ||
| + | |||
| + | void setup() { | ||
| + | pinMode(dccInputPin, | ||
| + | pinMode(ledPin, | ||
| + | } | ||
| + | |||
| + | void loop() { | ||
| + | if (digitalRead(dccInputPin) == HIGH) { | ||
| + | // LED langsam ein- und ausfaden | ||
| + | analogWrite(ledPin, | ||
| + | brightness = brightness + fadeAmount; | ||
| + | |||
| + | if (brightness <= 0 || brightness >= 255) { | ||
| + | fadeAmount = -fadeAmount; | ||
| + | } | ||
| + | delay(30); | ||
| + | } else { | ||
| + | // LED sofort ausschalten, | ||
| + | analogWrite(ledPin, | ||
| + | brightness = 0; | ||
| + | } | ||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | ===== Mehrere LEDs, wobei jede LED auf eine eigene DCC Adresse hört ===== | ||
| + | |||
| + | * DCC-Signal an Arduino-Pin 2 (über Optokoppler oder DCC-Shield) | ||
| + | * Jede LED an einen PWM-fähigen Pin (z.B. 3, 5, 6, 9, 10, 11) | ||
| + | * Jede LED hat eine eigene DCC-Adresse und wird unabhängig gesteuert. | ||
| + | * Der Fade-Effekt läuft nur, wenn der DCC-Befehl (direction == 1) aktiv ist. | ||
| + | * Bei direction == 0 wird die LED sofort ausgeschaltet. | ||
| + | * Du kannst die Anzahl der LEDs und Adressen einfach anpassen. | ||
| + | |||
| + | <code c> | ||
| + | #include < | ||
| + | |||
| + | NmraDcc Dcc; | ||
| + | const int dccPin = 2; // DCC-Eingang | ||
| + | |||
| + | // LED-Pins und zugehörige DCC-Adressen | ||
| + | struct LED { | ||
| + | int pin; | ||
| + | int dccAddr; | ||
| + | int brightness; | ||
| + | int fadeAmount; | ||
| + | bool isFading; | ||
| + | }; | ||
| + | |||
| + | LED leds[] = { | ||
| + | {3, 1, 0, 5, false}, | ||
| + | {5, 2, 0, 5, false}, | ||
| + | {6, 3, 0, 5, false}, | ||
| + | {9, 4, 0, 5, false}, | ||
| + | {10, 5, 0, 5, false}, // LED an Pin 10, DCC-Adresse 5 | ||
| + | {11, 6, 0, 5, false} // LED an Pin 11, DCC-Adresse 6 | ||
| + | }; | ||
| + | const int ledCount = 6; | ||
| + | |||
| + | void setup() { | ||
| + | for (int i = 0; i < ledCount; i++) { | ||
| + | pinMode(leds[i].pin, | ||
| + | } | ||
| + | Dcc.pin(dccPin, | ||
| + | Dcc.begin(); | ||
| + | } | ||
| + | |||
| + | void loop() { | ||
| + | Dcc.process(); | ||
| + | for (int i = 0; i < ledCount; i++) { | ||
| + | if (leds[i].isFading) { | ||
| + | leds[i].brightness += leds[i].fadeAmount; | ||
| + | if (leds[i].brightness <= 0 || leds[i].brightness >= 255) { | ||
| + | leds[i].fadeAmount = -leds[i].fadeAmount; | ||
| + | } | ||
| + | analogWrite(leds[i].pin, | ||
| + | delay(30); | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | |||
| + | // Wird aufgerufen, wenn ein DCC-Accessory-Befehl kommt | ||
| + | void notifyDccAccTurnout(uint16_t addr, uint8_t direction, uint8_t outputPower) { | ||
| + | for (int i = 0; i < ledCount; i++) { | ||
| + | if (leds[i].dccAddr == addr) { | ||
| + | if (direction == 1) { | ||
| + | leds[i].isFading = true; // LED faden lassen | ||
| + | } else { | ||
| + | leds[i].isFading = false; | ||
| + | analogWrite(leds[i].pin, | ||
| + | leds[i].brightness = 0; | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | ===== Programm Schweißlicht im BW ===== | ||
| + | |||
| + | Hier ist ein Arduino-Programm, | ||
| + | |||
| + | Hardware-Voraussetzungen: | ||
| + | |||
| + | * Arduino (z.B. Uno, Nano, Mega) | ||
| + | * DCC-Schnittstelle (z.B. Optokoppler oder DCC-Shield) an Pin 2 | ||
| + | * 12 PWM-fähige Pins für die LEDs (z.B. 3, 5, 6, 9, 10, 11 und ggf. weitere, je nach Board) | ||
| + | * 12 Widerstände (z.B. 220 Ohm) für die LEDs | ||
| + | |||
| + | Auch wichtig: | ||
| + | * DCC-Adressen: | ||
| + | * Flackerfrequenz: | ||
| + | * Pins: A0–A5 können als digitale Pins verwendet werden, falls nicht genug PWM-Pins verfügbar sind. | ||
| + | * Mehrere LEDs: Das Programm kann mehrere LEDs gleichzeitig flackern lassen. | ||
| + | * DCC-Befehl: Bei direction=1 beginnt die LED zu flackern, bei direction=0 schaltet sie sofort aus. | ||
| + | |||
| + | |||
| + | <code c> | ||
| + | #include < | ||
| + | |||
| + | // DCC-Eingang | ||
| + | const int dccPin = 2; | ||
| + | |||
| + | // Struktur für jede LED | ||
| + | struct FlackerLED { | ||
| + | int pin; // Arduino-Pin | ||
| + | int dccAddr; | ||
| + | bool isFlackern; | ||
| + | unsigned long lastChange; // Letzte Änderung | ||
| + | int flackerDelay; | ||
| + | }; | ||
| + | |||
| + | FlackerLED leds[] = { | ||
| + | {3, 1, false, 0, 50}, // LED an Pin 3, DCC-Adresse 1, 50ms | ||
| + | {5, 2, false, 0, 70}, // LED an Pin 5, DCC-Adresse 2, 70ms | ||
| + | {6, 3, false, 0, 90}, // LED an Pin 6, DCC-Adresse 3, 90ms | ||
| + | {9, 4, false, 0, 110}, // LED an Pin 9, DCC-Adresse 4, 110ms | ||
| + | {10, 5, false, 0, 130}, // LED an Pin 10, DCC-Adresse 5, 130ms | ||
| + | {11, 6, false, 0, 150}, // LED an Pin 11, DCC-Adresse 6, 150ms | ||
| + | {A0, 7, false, 0, 170}, // LED an Pin A0, DCC-Adresse 7, 170ms (als digitaler Pin) | ||
| + | {A1, 8, false, 0, 190}, // LED an Pin A1, DCC-Adresse 8, 190ms | ||
| + | {A2, 9, false, 0, 210}, // LED an Pin A2, DCC-Adresse 9, 210ms | ||
| + | {A3, 10, false, 0, 230}, // LED an Pin A3, DCC-Adresse 10, 230ms | ||
| + | {A4, 11, false, 0, 250}, // LED an Pin A4, DCC-Adresse 11, 250ms | ||
| + | {A5, 12, false, 0, 270}, // LED an Pin A5, DCC-Adresse 12, 270ms | ||
| + | {4, 13, false, 0, 290}, // LED an Pin 4, DCC-Adresse 13, 290ms | ||
| + | {7, 14, false, 0, 310}, // LED an Pin 7, DCC-Adresse 14, 310ms | ||
| + | {8, 15, false, 0, 330} // LED an Pin 8, DCC-Adresse 15, 330ms | ||
| + | // Hier ggf. weitere LEDs ergänzen | ||
| + | }; | ||
| + | const int ledCount = 12; // Anzahl der LEDs | ||
| + | |||
| + | NmraDcc Dcc; | ||
| + | |||
| + | void setup() { | ||
| + | // Alle LED-Pins als Ausgang setzen | ||
| + | for (int i = 0; i < ledCount; i++) { | ||
| + | pinMode(leds[i].pin, | ||
| + | digitalWrite(leds[i].pin, | ||
| + | } | ||
| + | |||
| + | // DCC initialisieren | ||
| + | Dcc.pin(dccPin, | ||
| + | Dcc.begin(); | ||
| + | } | ||
| + | |||
| + | void loop() { | ||
| + | Dcc.process(); | ||
| + | |||
| + | // Flackern für jede LED steuern | ||
| + | for (int i = 0; i < ledCount; i++) { | ||
| + | if (leds[i].isFlackern) { | ||
| + | if (millis() - leds[i].lastChange > leds[i].flackerDelay) { | ||
| + | // LED-Zustand umschalten | ||
| + | digitalWrite(leds[i].pin, | ||
| + | leds[i].lastChange = millis(); | ||
| + | } | ||
| + | } else { | ||
| + | digitalWrite(leds[i].pin, | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | |||
| + | // Wird aufgerufen, wenn ein DCC-Accessory-Befehl kommt | ||
| + | void notifyDccAccTurnout(uint16_t addr, uint8_t direction, uint8_t outputPower) { | ||
| + | for (int i = 0; i < ledCount; i++) { | ||
| + | if (leds[i].dccAddr == addr) { | ||
| + | leds[i].isFlackern = (direction == 1); // Bei direction=1 flackern, sonst aus | ||
| + | if (!leds[i].isFlackern) { | ||
| + | digitalWrite(leds[i].pin, | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </ | ||