digitalelektronik:moving-light
Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
Nächste Überarbeitung | Vorhergehende ÜberarbeitungLetzte ÜberarbeitungBeide Seiten der Revision | ||
digitalelektronik:moving-light [2015/02/12 08:25] – angelegt ros | digitalelektronik:moving-light [2015/03/05 09:09] – ros | ||
---|---|---|---|
Zeile 9: | Zeile 9: | ||
<code c++> | <code c++> | ||
const int DELAY_MS = 500; | const int DELAY_MS = 500; | ||
- | const int PIN[] = { 2, 3, 4, 5, 6, 7, 8, 9 }; | + | |
- | const int PIN_COUNT = sizeof(PIN) / sizeof(int); | + | const int PIN[] = { 2, 3, 4, 5 }; |
+ | const int PIN_COUNT = 4; | ||
int currentLed; | int currentLed; | ||
Zeile 26: | Zeile 28: | ||
void loop() { | void loop() { | ||
digitalWrite(PIN[currentLed], | digitalWrite(PIN[currentLed], | ||
- | currentLed = (currentLed + 1) % PIN_COUNT; | + | currentLed = currentLed + 1; |
+ | if (PIN_COUNT | ||
+ | currentLed = 0; | ||
+ | } | ||
digitalWrite(PIN[currentLed], | digitalWrite(PIN[currentLed], | ||
delay(DELAY_MS); | delay(DELAY_MS); | ||
Zeile 33: | Zeile 39: | ||
==== Hin- und herbewegendes Lauflicht ==== | ==== Hin- und herbewegendes Lauflicht ==== | ||
+ | |||
+ | Bei dieser Variante bewegt sich das Lauflicht hin und her. | ||
<code c++> | <code c++> | ||
const int DELAY_MS = 100; | const int DELAY_MS = 100; | ||
const int PIN[] = { 2, 3, 4, 5, 6, 7, 8, 9 }; | const int PIN[] = { 2, 3, 4, 5, 6, 7, 8, 9 }; | ||
- | const int PIN_COUNT = sizeof(PIN) / sizeof(int); | + | const int PIN_COUNT = 8; |
int currentLed; | int currentLed; | ||
int dir; | int dir; | ||
Zeile 66: | Zeile 75: | ||
</ | </ | ||
+ | ==== Harmonische Schwingung ==== | ||
+ | Bei dieser Variante stellt die Bewegung eine harmonische Schwingung dar. | ||
+ | |||
+ | <code c++> | ||
+ | const int DELAY_MS = 100; | ||
+ | const int PIN[] = { 2, 3, 4, 5, 6, 7, 8, 9 }; | ||
+ | const int PIN_COUNT = 8; | ||
+ | |||
+ | int currentLed; | ||
+ | int dir; | ||
+ | |||
+ | void setup() { | ||
+ | int i = 0; | ||
+ | while (i < PIN_COUNT) { | ||
+ | pinMode(PIN[i], | ||
+ | i = i + 1; | ||
+ | } | ||
+ | |||
+ | currentLed = 0; | ||
+ | dir = 1; | ||
+ | } | ||
+ | |||
+ | void loop() { | ||
+ | digitalWrite(PIN[currentLed], | ||
+ | currentLed = currentLed + dir; | ||
+ | if (currentLed < 0 || PIN_COUNT <= currentLed) { | ||
+ | dir = -dir; | ||
+ | currentLed = currentLed + dir; | ||
+ | } | ||
+ | |||
+ | digitalWrite(PIN[currentLed], | ||
+ | double maxAmp = PIN_COUNT / 2.0; | ||
+ | double currAmp = currentLed - maxAmp; | ||
+ | delay(DELAY_MS * abs(sin(currAmp / maxAmp)) + 100); | ||
+ | } | ||
+ | </ |
digitalelektronik/moving-light.txt · Zuletzt geändert: 2020/10/13 14:25 von 127.0.0.1