Hello,
I wanted to make a group of 5 LED's blink simultaneously with a delay Then turn off for a period of time. After running the loop say 20 times, they would increase the speed at which they blink, and decrease the number of blinking LED's from 5 to 4. The effect I was looking to accomplish is a timer that blinks more rapidly as the number of LED's decreases. I started playing with the following code, and can get all 5 to blink simultaneously, but I am looking for the best way to change the blink rate to be short and shorter (Faster) as it progresses and additional LED's go out.
INO FILE
// Multi_Blink
//
// Blink 5 LED's at a given speed, then reduce the number to 4 LED's and blink faster, then 3, then 2, then 1.
// TODO; Add Piezo Speaker Sound as well
#include "Multi_Blink1.h" // type definitions
const int blink1 = 1000;
const int blink2 = 800;
const int blink3 = 600;
const int blink4 = 400;
const int blink5 = 200;
// Blink Table T - Modify this table to suit whatever the output requirements are
// Add or delete lines as required to achieve the desired effects.
//
ledTable T[] =
//Pin St State 0 State 1 LastTime
{
{ 3, 1, {{HIGH, blink1}, {LOW, blink1}}, 0 },
{ 4, 1, {{HIGH, blink1}, {LOW, blink1}}, 0 },
{ 5, 1, {{HIGH, blink1}, {LOW, blink1}}, 0 },
{ 6, 1, {{HIGH, blink1}, {LOW, blink1}}, 0 },
{ 7, 1, {{HIGH, blink1}, {LOW, blink1}}, 0 },
};
// Self adjusting constants for loop indexes
#define MAX_STATE (sizeof(T[0].state)/sizeof(stateDef))
#define MAX_LED (sizeof(T)/sizeof(ledTable))
int pin = 11;
void setup()
{
pinMode(pin, OUTPUT);
for (int i=0; i < MAX_LED; i++)
{
pinMode(T[i].ledPin, OUTPUT);
T[i].lastTransTime = millis();
digitalWrite(T[i].ledPin, T[i].state[T[i].currentState].activeVal);
}
}
void loop()
{
for (int i=0; i < MAX_LED; i++)
{
// check if the state active time has expired (ie, it is less than current time)
if (millis() >= T[i].lastTransTime + T[i].state[T[i].currentState].activeTime)
{
// switch to the next state with wrapround
T[i].currentState = (T[i].currentState + 1) % MAX_STATE;
// write out the next state value
T[i].lastTransTime = millis();
digitalWrite(T[i].ledPin, T[i].state[T[i].currentState].activeVal);
tone(pin, 261, 1000);
}
}
}
HEADER FILE
// Multi_Blink.h
//
// Blink lots of LEDs at diffreent frequencies simultaneously
//
// Header file is required to be able to define the structured types
//
#include <Arduino.h>
#ifndef MULTIBLINKH
#define MULTIBLINKH
typedef struct
{
uint8_t activeVal; // digital value for this state to be active (HIGH/LOW)
uint16_t activeTime; // time to stay active in this state stay in milliseconds
} stateDef;
typedef struct
{
uint8_t ledPin; // Arduino I/O pin number
uint8_t currentState; // current active state
stateDef state[2]; // the ON and OFF state definitions. Add more states if required
uint32_t lastTransTime; // the 'time' of the last state transition - saves the millis() value
} ledTable;
#endif