Programmed on/off times for LED blink
the code below works , can blink 2 leds independently ...but appears code !! , if want extend more leds code become large...just wanted know if there simpler method achieve same.
code: [select]
void setup()
{
}
void loop()
{
ledprgoffon(800, 10, 2);
ledprgoffon_01(500, 500, 3);
}
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
// function blink led based on programmed on times , off times..
void ledprgoffon(long offperiod, long onperiod, byte ledpin )
{
unsigned long currentmillis = millis();
static unsigned long previousmillis = 0;
static boolean ledstate;
static boolean onled = 1;
static boolean offled = 0;
if ( onled )
{
if (currentmillis - previousmillis >= offperiod)
{
ledstate = high;
onled = 0;
offled = 1;
previousmillis = currentmillis;
}
}
if ( offled )
{
if (currentmillis - previousmillis >= onperiod)
{
ledstate = low;
onled = 1;
offled = 0;
previousmillis = currentmillis;
}
}
pinmode(ledpin, output);
digitalwrite(ledpin, ledstate); // update led state
}
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
void ledprgoffon_01(long offperiod, long onperiod, byte ledpin )
{
unsigned long currentmillis = millis();
static unsigned long previousmillis = 0;
static boolean ledstate;
static boolean onled = 1;
static boolean offled = 0;
if ( onled )
{
if (currentmillis - previousmillis >= offperiod)
{
ledstate = high;
onled = 0;
offled = 1;
previousmillis = currentmillis;
}
}
if ( offled )
{
if (currentmillis - previousmillis >= onperiod)
{
ledstate = low;
onled = 1;
offled = 0;
previousmillis = currentmillis;
}
}
pinmode(ledpin, output);
digitalwrite(ledpin, ledstate); // update led state
}
quote
but appears codethat's because is.
look @ differences between ledprgoffon_01() , ledprgoffon(). differences static variables.
you use array of static variables, pass function index, , have 1 function. easy extend 100 leds.
Arduino Forum > Using Arduino > Programming Questions > Programmed on/off times for LED blink
arduino
Comments
Post a Comment