diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..2608ec266b13f4917739bbef920194301497cac3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.DS_Store +.vscode \ No newline at end of file diff --git a/TT_LED.h b/TT_LED.h new file mode 100644 index 0000000000000000000000000000000000000000..c4e78ba17e4d518fb993c433fe6f3e7a5783b725 --- /dev/null +++ b/TT_LED.h @@ -0,0 +1,115 @@ +class TT_LED +{ +public: + TT_LED(byte aPin) : PIN(aPin) { pinMode(aPin, OUTPUT); }; + void switch_to(bool b) { digitalWrite(PIN, b); }; + byte read_pin_state() { return digitalRead(PIN); }; + void on() { switch_to(HIGH); }; + void off() { switch_to(LOW); }; + void toggle() { switch_to(!read_pin_state()); }; + + void blink(uint16_t interval) + { + if ((millis() - timestamp) > interval) + { + digitalWrite(PIN, !digitalRead(PIN)); + timestamp = millis(); + } + }; + + void blink(uint16_t interval_off, uint16_t interval_on) + { + switch (digitalRead(PIN)) + { + case HIGH: + if ((millis() - timestamp) > interval_on) + { + digitalWrite(PIN, !digitalRead(PIN)); + timestamp = millis(); + } + break; + case LOW: + if ((millis() - timestamp) > interval_off) + { + digitalWrite(PIN, !digitalRead(PIN)); + timestamp = millis(); + } + break; + } + } + +private: + const byte PIN; + uint32_t timestamp; +}; + +enum Chainmode +{ + LEFT, + RIGHT, + TOGGLE +}; + +class TT_LED_chain +{ +public: + TT_LED_chain(byte aPin_count, byte aPin_chain[], Chainmode chainmode = RIGHT) : PIN_CHAIN(aPin_chain), PIN_COUNT(aPin_count), chainmode(chainmode) + { + for (byte i = 0; i < aPin_count; i++) + pinMode(aPin_chain[i], OUTPUT); + }; + void run(uint16_t interval) + { + if ((millis() - timestamp) > interval) + { + digitalWrite(PIN_CHAIN[active_chain_index], LOW); + timestamp = millis(); + active_chain_index = next_pin(PIN_COUNT, &direction, active_chain_index, chainmode); + } + else + { + digitalWrite(PIN_CHAIN[active_chain_index], HIGH); + } + } + void set_chainmode(Chainmode cm) { chainmode = cm; }; + void on() + { + for (byte i = 0; i < PIN_COUNT; i++) + digitalWrite(PIN_CHAIN[i], HIGH); + }; + void off() + { + for (byte i = 0; i < PIN_COUNT; i++) + digitalWrite(PIN_CHAIN[i], LOW); + }; + +private: + int8_t next_pin(int8_t aCount, int8_t *aDir, int8_t aIndex, Chainmode aMode) + { + switch (aMode) + { + case RIGHT: + return (aIndex + 1) % aCount; + break; + case LEFT: + if (aIndex <= 0) + return aCount - 1; + else + return (aIndex - 1) % aCount; + break; + case TOGGLE: + if ((aIndex <= 0) || (aIndex >= aCount - 1)) + direction *= -1; + return aIndex + direction; + break; + default: + return aIndex; + } + }; + const byte *PIN_CHAIN; + const byte PIN_COUNT; + Chainmode chainmode; + int8_t active_chain_index = 0; + uint32_t timestamp; + int8_t direction = -1; +}; \ No newline at end of file diff --git a/examples/led_blink/led_blink.ino b/examples/led_blink/led_blink.ino new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/examples/ledchain_use/ledchain_use.ino b/examples/ledchain_use/ledchain_use.ino new file mode 100644 index 0000000000000000000000000000000000000000..4011e34ed583bad2ac39e357dd60344a2ea8b3ee --- /dev/null +++ b/examples/ledchain_use/ledchain_use.ino @@ -0,0 +1,39 @@ +#include "TT_LED.h" + +// Definiere die Pins, an denen die LEDs angeschlossen sind: +byte pins[] = {4, 5, 6, 7}; + +// Erzeuge ein ledchain-Objekt +// 1. Parameter: Größe des Pin-Arrays +// 2. Parameter: Pin-Array +// 3. Parameter: RIGHT=von rechts nach links (default) +// LEFT= von links nach rechts +// TOGGLE=von links nach rechts nach links nach ... +TT_LED_chain ledchain(4, pins, TOGGLE); + +void setup() +{ + +} + +void loop() +{ + uint32_t t=millis(); + + while (millis()-t<3000) + ledchain.run(300); + + ledchain.off(); + t=millis(); + + + delay(1000); + + while (millis()-t<3000) + ledchain.run(100); + + ledchain.off(); + t=millis(); + + delay(1000); +} \ No newline at end of file