Hier mal ein kurzer Preview ...
// Author: fliegerle.com, V0.9
//
// Small sketch whichs let's LEDs make blink blink blink ...
// How it works: Just create a light object with "Create" and put an "Init" into Setup() and a "Check" in Loop().
// After on_1 ms the light is turned off, then after off_1 turned on again. You can define up to
// 2 periods. Each light gets its own counter, so you don't need to compute divisors or counters. I'm using JeeLib
// for the MilliTimer.
#include <JeeLib.h>
struct Light
{
Light() : m_event(0),
m_status(255),
m_period(0)
{};
void Create( byte led_pin, int on_1=0, int off_1=0, int on_2=0, int off_2=0 )
{
m_period_switch[0] = on_1;
m_period_switch[1] = ( off_1 == 0 ) ? on_1 : off_1;
m_period_switch[2] = ( on_2 == 0 ) ? on_1 : on_2;
m_period_switch[3] = ( off_2 == 0 ) ? off_1 : off_2;
m_led_pin = led_pin;
m_status = 255;
}
void Init()
{
m_event = 1;
m_period = m_period_switch[0];
m_counter = 0;
pinMode( m_led_pin, OUTPUT );
Off();
}
void Check()
{
if( m_status == 255 )
{
Init();
}
if ( m_period_switch[0] == 0 )
{
On();
return;
}
if ( m_timer.poll(m_period) )
{
switch( m_event )
{
case 1:
{
digitalWrite( m_led_pin, 1 );
m_event = 0;
m_period = m_period_switch[2*m_counter+1];
m_counter++;
if ( m_counter>1 )
{
m_counter=0;
}
break;
}
case 0:
{
digitalWrite( m_led_pin, 0 );
m_event = 1;
m_period = m_period_switch[2*m_counter];
break;
}
default:;
}
}
}
void Off()
{
if ( m_status!=0 )
{
m_status = 0;
digitalWrite( m_led_pin, m_status );
}
}
void On()
{
if ( m_status!=1 )
{
m_status = 1;
digitalWrite( m_led_pin, m_status );
}
}
MilliTimer m_timer;
byte m_event;
int m_period;
int m_period_switch[4];
byte m_counter;
byte m_led_pin;
byte m_status;
};
const int max_leds = 5;
Light test_led[max_leds];
void setup()
{
for ( int i=0; i<max_leds; i++ )
{
switch( i )
{
case 0: test_led[i].Create( 13, 50, 1000 ); break; // ON for 50ms, OFF for 1000ms
case 1: test_led[i].Create( 3, 50, 100, 50, 3000 ); break; // double blink
case 2: test_led[i].Create( 4, 1500, 100 ); break; // long on, then off
case 3: test_led[i].Create( 5, 50, 100, 150, 200 ); break; // now to something completely different
case 4: test_led[i].Create( 6 ); break; // permanently ON
default:;
}
test_led[i].Init();
}
}
void loop()
{
for ( int i=0; i<max_leds; i++ )
{
test_led[i].Check();
}
}