Hello Folks,
I am working on a program that uses an arduino to light LEDS mounted on a nautical chart. The LEDS flash in the same fashion as real lighted buoys on the ocean.
You folks have been very kind in helping me. The first program listed below works for 90% of the buoys as they blink at constant rates even though the on time and off time varies from buoy to buoy. There are some buoys however that flash in a Morse code fashion. The off time is the same but the on time alternates between a long spell and a short spell. The second bit of code was contributed by Delta G. and works perfectly for the Morse A buoy. My problem is how to combine both programs into one. Would someone pint me in the right direction? Thanks so much.
BTW I used the # sign to indicate code but am not sure I am using it correctly.
first code:
class Flasher
{
// Class Member Variables
// These are initialized at startup
int ledPin; // the number of the LED pin
long OnTime; // milliseconds of on-time
long OffTime; // milliseconds of off-time
// These maintain the current state
int ledState; // ledState used to set the LED
unsigned long previousMillis; // will store last time LED was updated
// Constructor - creates a Flasher
// and initializes the member variables and state
public:
Flasher(int pin, long on, long off)
{
ledPin = pin;
pinMode(ledPin, OUTPUT);
OnTime = on;
OffTime = off;
ledState = LOW;
previousMillis = 0;
}
void Update()
{
// check to see if it's time to change the state of the LED
unsigned long currentMillis = millis();
if((ledState == HIGH) && (currentMillis - previousMillis >= OnTime))
{
ledState = LOW; // Turn it off
previousMillis = currentMillis; // Remember the time
digitalWrite(ledPin, ledState); // Update the actual LED
}
else if ((ledState == LOW) && (currentMillis - previousMillis >= OffTime))
{
ledState = HIGH; // turn it on
previousMillis = currentMillis; // Remember the time
digitalWrite(ledPin, ledState); // Update the actual LED
}
}
};
Flasher led0(0, 1000, 15000);
Flasher led1(1, 100, 400);
Flasher led2(2, 1000, 6000);
Flasher led3(3, 1000, 4000);
Flasher led4(4, 1000, 4000);
Flasher led5(5, 1000, 10000);
void setup()
{
}
void loop()
{
led0.Update();
led1.Update();
led2.Update();
led3.Update();
led4.Update();
led5.Update();
}
second code:
int ledPin = 13; // the number of the LED pin
int ledState = LOW; // ledState used to set the LED
unsigned long previousMillis = 0; // will store last time LED was updated
long OnTime = 1000; // milliseconds of on-time
long OffTime = 1000; // milliseconds of off-time
void setup()
{
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
}
void loop()
{
// check to see if it's time to change the state of the LED
unsigned long currentMillis = millis();
if((ledState == HIGH) && (currentMillis - previousMillis >= OnTime))
{
ledState = LOW; // Turn it off
if(OnTime == 1000)
{
OnTime = 2000;
}
else
{
OnTime = 1000;
}
previousMillis = currentMillis; // Remember the time
digitalWrite(ledPin, ledState); // Update the actual LED
}
else if ((ledState == LOW) && (currentMillis - previousMillis >= OffTime))
{
ledState = HIGH; // turn it on
previousMillis = currentMillis; // Remember the time
digitalWrite(ledPin, ledState); // Update the actual LED
}
}