I want to have two sets of LEDs flashing at different rates running at the same time. Since scheduler.h is not compatible with Nano, how else may I achieve this as easy as it would be if said program was compatible? Thank you. Attached is my current code in which only the 1st set of LEDs flash. Thank you in advance, and I have search forum but wanted some more help.
int ledDelay = 50; // delay by 50ms
int red2 = 2;
int red3 = 3;
int wig4 = 4;
int wig5 = 5;
int onTime1 = 500; //Declare on time as .5 second
int offTime1 = 500; //Declare on time as .5 second
void setup() {
pinMode(red2, OUTPUT);
pinMode(red3, OUTPUT);
pinMode(wig4, OUTPUT);
pinMode(wig5, OUTPUT);
}
// 1st set of alternating strobe (2 flash per LED) LEDs
void loop() {
digitalWrite(red2, HIGH); // turn the red light on
delay(ledDelay); // wait 50 ms
digitalWrite(red2, LOW); // turn the red light off
delay(ledDelay); // wait 50 ms
digitalWrite(red2, HIGH); // turn the red light on
delay(ledDelay); // wait 50 ms
digitalWrite(red2, LOW); // turn the red light off
delay(ledDelay); // wait 50 ms
digitalWrite(red2, HIGH); // turn the red light on
delay(ledDelay); // wait 50 ms
digitalWrite(red2, LOW); // turn the red light off
delay(ledDelay); // wait 50 ms
delay(75); // midpoint by 75ms
digitalWrite(red3, HIGH); // turn the blue light on
delay(ledDelay); // wait 50 ms
digitalWrite(red3, LOW); // turn the blue light off
delay(ledDelay); // wait 50 ms
digitalWrite(red3, HIGH); // turn the blue light on
delay(ledDelay); // wait 50 ms
digitalWrite(red3, LOW); // turn the blue light off
delay(ledDelay); // wait 50 ms
digitalWrite(red3, HIGH); // turn the blue light on
delay(ledDelay); // wait 50 ms
digitalWrite(red3, LOW); // turn the blue light off
delay(ledDelay); // wait 50 ms
}
void loop2()
// 2nd second set of alternating (one flash each) LEDs
{
digitalWrite(wig4, LOW);
delay(onTime1);
digitalWrite(wig5, HIGH);
delay(offTime1);
}
Thank you Bob. I will be sure to use the code tags next time. I did read some about millis already but I was hoping for something even simpler. I will check out BlinkWithoutDelay and the guide again as well. Thanks again.
Hello, @misterbee1.
This is your sketch in a simulation:
I like to point to YouTube/ProgrammingElectronicsAcademy for learning Arduino programming. Here is the first video of their multi-part series on millis() (mentioned above)... (about 15 minutes per video)
Ok, I hate to sound ignorant, but this non millis coding shows the exact two programs I want running at the same time, but of course can't do without the help of millis. 1st program is two LEDs flashing two times each while alternating and 2nd program is two LEDs flashing once each alternately. Need it running at the same time with millis:
int ledDelay = 70;
int red2 = 2;
int red3 = 3;
int wig4 = 4;
int wig5 = 5;
void setup() {
pinMode(red2, OUTPUT);
pinMode(red3, OUTPUT);
pinMode(wig4, OUTPUT);
pinMode(wig5, OUTPUT);
}
// 1st set of alternating strobe (2 flash per LED) LEDs
void loop1() {
digitalWrite(red2, HIGH); // turn the red light on
delay(ledDelay); // wait 50 ms
digitalWrite(red2, LOW); // turn the red light off
delay(ledDelay); // wait 50 ms
digitalWrite(red2, HIGH); // turn the red light on
delay(ledDelay); // wait 50 ms
digitalWrite(red2, LOW); // turn the red light off
delay(ledDelay); // wait 50 ms
digitalWrite(red2, HIGH); // turn the red light on
delay(ledDelay); // wait 50 ms
digitalWrite(red2, LOW); // turn the red light off
delay(ledDelay); // wait 50 ms
digitalWrite(red3, HIGH); // turn the blue light on
delay(ledDelay); // wait 50 ms
digitalWrite(red3, LOW); // turn the blue light off
delay(ledDelay); // wait 50 ms
digitalWrite(red3, HIGH); // turn the blue light on
delay(ledDelay); // wait 50 ms
digitalWrite(red3, LOW); // turn the blue light off
delay(ledDelay); // wait 50 ms
digitalWrite(red3, HIGH); // turn the blue light on
delay(ledDelay); // wait 50 ms
digitalWrite(red3, LOW); // turn the blue light off
delay(ledDelay); // wait 50 ms
}
void loop2()
// 2nd second set of alternating (one flash each) LEDs
{
digitalWrite(wig4, HIGH);
delay(50);
digitalWrite(wig5, LOW);
delay(250);
digitalWrite(wig4, LOW);
delay(50);
digitalWrite(wig5, HIGH);
delay(250);
}
void loop() { // call other functions from here to make a modular sketch
loop1(); // was inside loop()... moved it to its own function
loop2(); // was not called in original sketch
}
Thank you Bob. Others reading my thread please feel free to respond with the exact coding I need for this to work - so that I can see it correctly and understand it finally.
Better still, run the sketch using your pin numbers then adjust the parameters in the leds array until you understand what they do and set them accordingly to do what you want
Thanks, but all that does is blink pin 13 constantly. I feel like you guys are my grandfather from when I was 13 just throwing math books at me and saying figure it out. Some people, like myself, learn differently and need to see the final product and work backward.
const byte ledPins[] = {3,5,6,9};
const byte NUMBER_OF_LEDS = sizeof(ledPins);
unsigned long startTimes[NUMBER_OF_LEDS] = {};
byte indexes[NUMBER_OF_LEDS] = {0};
const byte MAX_NUMBER_OF_PERIODS = 10;
long periods[][MAX_NUMBER_OF_PERIODS] = //on/off periods for each LED. -1 indicates end of sequence
{
{1000, 2000, 1500, 2500, -1}, //on/off/on/off/start again
{500, 200, 1000, 2000, 3000, 4000, -1}, //on/off/on/off/on/off/start again
{400, 1000, 1500, 2000, -1}, //on/off/on/off/start again
{500, 500, -1} //on/off/start again
};
void setup()
{
Serial.begin(115200);
for (int led = 0; led < NUMBER_OF_LEDS; led++)
{
pinMode(ledPins[led], OUTPUT);
}
}
void loop()
{
unsigned long currentTime = millis();
for (int led = 0; led < NUMBER_OF_LEDS; led++) //iterate through the LEDs
{
if (currentTime - startTimes[led] >= periods[led][indexes[led]]) //? time to change ?
{
digitalWrite(ledPins[led], !digitalRead(ledPins[led])); //flip led state
startTimes[led] = currentTime; //save start time of state
indexes[led]++; //increment index
}
if (periods[led][indexes[led]] == -1) //if next period is zero (end of sequence)
{
indexes[led] = 0; //reset period index for this LED
}
} //end for loop
}
Here is what the code does that you posted in post #16:
I can't figure out how to adjust those times to have 3 and 5 alternate with a single flash each and to have 6 and 9 alternate with a double flash each...