Bob, here are the two programs I want to run at the same time. I have them with the exact timing and pinouts that I need already. I just need them to run at the same time in one program:
First Program:
/*
Alternate Blink
1. Turn on LED1, turn off LED2 for
300ms second (at the same time)
2. Turn off LED1, turn on LED2 for
300ms second (at the same time)
3. Repeat
*/
int pinA=3;
int pinB=4;
void setup() {
pinMode(pinA,OUTPUT);
pinMode(pinB,OUTPUT);
}
void loop() {
//turn on pinA and turn off pinB
digitalWrite(pinA,HIGH);
digitalWrite(pinB,LOW);
//wait 1 sec
delay(300);
//turn off pinA and turn on pinB
digitalWrite(pinA,LOW);
digitalWrite(pinB,HIGH);
//wait 1 sec
delay(300);
}
Second Program:
// Alternating Strobes
int ledDelay = 50; // delay by 50ms
int redPin = 5;
int bluePin = 6;
void setup() {
pinMode(redPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop() {
digitalWrite(redPin, HIGH); // turn the red light on
delay(ledDelay); // wait 50 ms
digitalWrite(redPin, LOW); // turn the red light off
delay(ledDelay); // wait 50 ms
digitalWrite(redPin, HIGH); // turn the red light on
delay(ledDelay); // wait 50 ms
digitalWrite(redPin, LOW); // turn the red light off
delay(ledDelay); // wait 50 ms
digitalWrite(redPin, HIGH); // turn the red light on
delay(ledDelay); // wait 50 ms
digitalWrite(redPin, LOW); // turn the red light off
delay(ledDelay); // wait 50 ms
delay(50); // delay midpoint by 50ms
digitalWrite(bluePin, HIGH); // turn the blue light on
delay(ledDelay); // wait 50 ms
digitalWrite(bluePin, LOW); // turn the blue light off
delay(ledDelay); // wait 50 ms
digitalWrite(bluePin, HIGH); // turn the blue light on
delay(ledDelay); // wait 50 ms
digitalWrite(bluePin, LOW); // turn the blue light off
delay(ledDelay); // wait 50 ms
digitalWrite(bluePin, HIGH); // turn the blue light on
delay(ledDelay); // wait 50 ms
digitalWrite(bluePin, LOW); // turn the blue light off
delay(ledDelay); // wait 50 ms
}
That is nearly perfect. The only thing off is that the alternating strobes are starting in a lit state instead of a non-lit. Other than that, it is perfect and I THANK YOU tremendously for your help. I am learning.
Surely one of the alternating pair of LEDs has to start in a lit state. Whatever LED starts lit, if you want the other one to start lit instead then swap the first and second rows of the array
The first sketch in post #21 alternates between 2 LEDs being on or off with an equal on/off period of 300 milliseconds
The second sketch in that post does something similar using 2 other LEDs but each of them flashes 3 times with an equal on/off period of 50 milliseconds before changing to the other LED and doing the same thing
Do you agree ?
As far as I am aware the timing array in post #21 does both at once
If it is not doing what you want then I cannot do anything more until you provide a timing diagram as previously requested
Yes, I agree. What I am saying is that the second sketch that I provided in post #21 looks slightly different than what you came up with to mimic it.
Yours:
long periods[][MAX_NUMBER_OF_PERIODS] = //on/off periods for each LED. -1 indicates end of sequence
{
{ 300, 300, -1 },
{ 0, 300, 300, 0, -1 },
{ 50, 50, 50, 50, 50, 50, 0, 300, -1 },
{ 0, 300, 50, 50, 50, 50, 50, 50, -1 }
};
The first two LEDs flash perfectly back and forth. The second set (the strobes) something is slightly off, hard to explain, which is why I posted the video comparing the two. As for timing diagram for the strobes, they are to flash 3 times with an equal on/off period of 50 milliseconds before changing to the other LED, which is how sketch 2 of post 21 is set up already.
It is difficult to see what is going on because the flashes happen so fast so here is a sketch that allows you to change the speed easily
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;
#define SHORT_PERIOD 500
long periods[][MAX_NUMBER_OF_PERIODS] = //on/off periods for each LED. -1 indicates end of sequence
{
{ SHORT_PERIOD * 6, SHORT_PERIOD * 6, -1 },
{ 0, SHORT_PERIOD * 6, SHORT_PERIOD * 6, 0, -1 },
{ SHORT_PERIOD, SHORT_PERIOD, SHORT_PERIOD, SHORT_PERIOD, SHORT_PERIOD, SHORT_PERIOD, 0, SHORT_PERIOD * 6, -1 },
{ 0, SHORT_PERIOD * 6, SHORT_PERIOD, SHORT_PERIOD, SHORT_PERIOD, SHORT_PERIOD, SHORT_PERIOD, SHORT_PERIOD, -1 }
};
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
}
You can change the #defined value of SHORT_PERIOD to change the speed of the sketch. With it set to 500 as above it seems to do what you describe, albeit slowly. Change the value to speed it up
Ok, using the code in your last post here is my video:
Compared to your video, you can see that on the third strobe flash in my video it stays steady as the opposite one goes to flash and vice versa. On your video it is just 3 blinks and off and vice versa. What gives?
I put them in now. On flash three of the strobe it still stays steady as the opposite one goes to flash and vice versa. In your video, it turns off before flashing to the other one.
You may have damaged the LEDs and/or the Arduino pins by not using current limiting resistors. Try using different pins on the Arduino and, if possible, different LEDs
Just to confirm, does my video show the effect that you are looking for, albeit slowed down ?