Skip to content
Snippets Groups Projects
Commit ad1edefd authored by Tom Tiltmann's avatar Tom Tiltmann
Browse files

Initiale Erstellung

parent a9b4f621
Branches
No related tags found
No related merge requests found
.DS_Store
.vscode
\ No newline at end of file
TT_LED.h 0 → 100644
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
#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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment