I have found the following code and it works fine on a breadboard controlling small LEDs but I want it to work with LED strip lighting with external 240v transformers. We have built the circuit using a 5v to 240v relay and the connections are functional but the code does not seem to translate. Does the built in blink class in Arduino function with relays? As it is either High or Low with no variation in voltage I don't see why it wouldn't work as a switch. Has anyone got any suggestions? Thank you
class LED
{
public :
LED();
void init( uint8_t pin );
void update();
void toggle( bool const on );
void on() { toggle( true ); };
void off() { toggle( false ); };
void blink( uint16_t const * const pattern, size_t const length, int8_t const count = -1 );
void blink( uint16_t const timeOn = 1000, uint16_t const timeOff = 1000, int8_t const count = -1 );
void flash( uint16_t const timeOn );
private :
uint8_t m_pin;
bool m_isOn;
uint32_t m_millis;
uint16_t m_time;
int8_t m_count;
uint16_t const * m_patternPtr;
uint16_t m_pattern[2];
size_t m_patternLength;
size_t m_patternIndex;
void write( bool const on );
};
LED::LED()
: m_pin ( 255 )
, m_isOn ( false )
, m_millis ( 0 )
, m_time ( 0 )
, m_count ( 0 )
, m_patternPtr ( nullptr )
, m_pattern { 0, 0 }
, m_patternLength ( 0 )
, m_patternIndex ( 0 )
{
}
void LED::init( uint8_t pin )
{
m_pin = pin;
pinMode( pin, OUTPUT );
write( false );
}
void LED::toggle( bool const on )
{
m_count = 0;
m_patternPtr = nullptr;
m_patternIndex = 0;
m_patternLength = 0;
write( on );
}
void LED::write( bool const on )
{
m_isOn = on;
digitalWrite( m_pin, on ? HIGH : LOW );
}
void LED::blink( uint16_t const * const pattern, size_t const length, int8_t const count )
{
if ( count != 0 && length > 0 && pattern != nullptr )
{
write( true );
m_millis = millis();
m_count = count;
m_patternPtr = pattern;
m_patternLength = length;
m_patternIndex = 0;
m_time = m_patternPtr[0];
}
}
void LED::blink( uint16_t const timeOn, uint16_t const timeOff, int8_t const count )
{
m_pattern[0] = timeOn;
m_pattern[1] = timeOff;
blink( m_pattern, 2, count );
}
void LED::flash( uint16_t const timeOn )
{
m_pattern[0] = timeOn;
blink( m_pattern, 1, 1 );
}
void LED::update()
{
if ( m_count != 0 && millis() - m_millis >= m_time )
{
if ( m_patternLength == 1 )
{
toggle( false );
}
else if ( ++m_patternIndex < m_patternLength )
{
m_time = m_patternPtr[ m_patternIndex ];
}
else
{
if ( m_count != -1 )
{
m_count--;
}
if ( m_count == 0 )
{
toggle( false );
}
else
{
m_patternIndex = 0;
m_time = m_patternPtr[0];
}
}
if ( m_count != 0 )
{
m_millis = millis();
write( !m_isOn );
}
}
}
LED led1;
LED led2;
LED led3;
void setup()
{
led1.init( 10 );
led2.init( 11 );
led3.init( 9 );
// 2 flashs toutes les secondes on off on off
static const uint16_t led1_pattern[] = { 100, 100, 2000, 400, 5000, 5000, 1000, 200 };
led1.blink( led1_pattern, 8 );
// 2 flashs toutes les deux secondes on off on off
static const uint16_t led2_pattern[] = { 2000, 100, 250, 1400, 2000, 400, 800 };
led2.blink( led2_pattern, 8);
// 2 flashs toutes les secondes on off on off
static const uint16_t led3_pattern[] = { 100, 5000, 2000, 400, 5000, 500, 1000, 2000 };
led3.blink( led3_pattern, 8 );
}
void loop()
{
led1.update();
led2.update();
led3.update();
}
digitalWrite( )
... that command actually sets the state of a hardware pin ... the library is actually irrelevant to controlling a 250 V light ... build a circuit that allows a GPIO pin to control a light ... test withdigitalWrite( )
command ... if that works, then the library should also work