6 independent output in different on off cycle.

Hi, I am working on the circuit which has 6 speakers. Each speaker line has a relay switch so my Arduino could on/off it. I want these speakers act like flickering LED with different on/off cycles. For instance,

SW1 : ---------------
SW2 : -----_-

SW3 : ------------------------------

SW4 : ----------------------_

Later I want to develop more so that I can modify cycles by changing values.
My question is now LED seems successfully turn on/off independently, but I can't shift it on a same cycle case.

SW1 : ----------------
SW2 : ----------____----

Below is my code :

const int testpin[]={8,9,10,11,12,13};
unsigned long present_T[] = {0UL,0UL,0UL,0UL,0UL,0UL}; //
unsigned long blank_T[] = {500UL,500UL,800UL,60UL,30UL,80UL}; //
unsigned long cycle_T[] = {1000UL,1000UL,2000UL,100UL,100UL,100UL}; //
int led_val[] = {1,1,1,1,1,1}; // 

void setup() {              
 for (int i=0 ; i < 6 ; i++){
   pinMode(testpin [i] , OUTPUT);
   present_T[i] = millis();

void loop() {
 set_led(testpin[0], &led_val[0], &present_T[0], &blank_T[0], cycle_T[0]); 
 set_led(testpin[1], &led_val[1], &present_T[1], &blank_T[1], cycle_T[1]); 
 set_led(testpin[2], &led_val[2], &present_T[2], &blank_T[2], cycle_T[2]); 
 set_led(testpin[3], &led_val[3], &present_T[3], &blank_T[3], cycle_T[3]); 
 set_led(testpin[4], &led_val[4], &present_T[4], &blank_T[4], cycle_T[4]); 
 set_led(testpin[5], &led_val[5], &present_T[5], &blank_T[5], cycle_T[5]);

void set_led(int ledPin, int *ledVal, unsigned long *myTimer, unsigned long *blkTimer, unsigned long timerVal ) {
 digitalWrite(ledPin, *ledVal);
 if(millis() - *myTimer >= *blkTimer) { // 
   *ledVal = LOW;    
 if((millis() - *myTimer) >= timerVal) { // 
   *myTimer = millis();  //restart cycle
   *ledVal = !*ledVal; //

My thought was if I set an each present_T[] differently in the beginning, signal should start on a different starting point. In the end, it would be shifted. But nothing really happens. I am quite new for this, any help would be appreciated. Thank you very much in advance!

I thought what I am doing is quite similar (if I understood correctly) with several PWM in different duty cycle. But what I want to do is different duty cycle in different frequency.

Please see How to sue the forum for posting code. don't think you make it italic on purpose.

But by using millis your on the right track. Calling it set, mm, bit confusing but okay. But what isn't working? (Hard to tell how it is formatted now.

Please see How to sue the forum for posting code. don't think you make it italic on purpose.

But by using millis your on the right track. Calling it set, mm, bit confusing but okay. But what isn't working? (Hard to tell how it is formatted now.

Hi, Sorry that I made mistake!

What isn't working is that I can't shift the signal line. For instance LED1,LED2 has same period of on/off time. (500ms ON in 1000ms cycle) I wish shift one of them while keeping same on/off period. I tried to set the present_T[1] differently as it can depend on present_T[0], but it doesn't give any effects.

LED1 : --------
LED2 : --------

set_led(testpin[1], &led_val[1], &present_T[0]+250, &blank_T[1], cycle_T[1]);

Ahh, yeah, then you need to change present indeed. But the biggest mistake is that you set the state all to on when you start while you're algoritm starts low. So make it more logical by first checking what state it needs to be and then set it. Or better, only set it when you want it to change.

And we can't delay it but we can advance it :slight_smile:

Couple of tips:

  • No need for the UL suffix. That's only useful when doing calculations and you want to force it as a UL. Putting a smaller variable into a bigger will just work fine. And when the constant is to small to fit the standard int it will be upgraded to L already :slight_smile:
  • Why make it that terribly complicated with pointers? Just pass the value :wink:
  • Like I said, ledUpdate is a more appropriate name.
  • The _T suffix can be confusing because -t is used to indicate types
  • Try to be more consistant with names. nowyoucombinethis, with_this etc
  • It's always nice to indicate an array by making it plural
  • spaces are free and make things more readable
  • Nice thing about arrays is that you don't have to repeat yourself
  • Nice thing about arrays is that you don't have to repeat yourself
  • aka, loop over all leds instead of calling all 6
const byte TestPins[]={8, 9, 10, 11, 12, 13};

const unsigned long BlankTimes[] = {500, 500, 800, 60, 30, 80};
const unsigned long CycleTimes[] = {1000, 1000, 2000, 100, 100, 100}; //

unsigned long startMillis[] = {-100, 0, 0, 0, 0, 0}; 

void setup() {              
  for (byte i = 0 ; i < sizeof(TestPins) ; i++){
    pinMode(TestPins[i] , OUTPUT);
    //present_T[i] = millis(); ///this messes up every start we want to give it....

void loop() {
  for(byte i = 0; i < sizeof(TestPins); i++){
    updateLed(TestPins[i], startMillis[i], BlankTimes[i], CycleTimes[i]);

void updateLed(const byte LedPin, unsigned long &timer, const unsigned long BTime, const unsigned long CTime) {
  const bool LedState = digitalRead(LedPin);
  //if off, we wait to end the blank time
  if(!LedState && (millis() - timer >= BTime)){
    digitalWrite(LedPin, HIGH);
  //if on we wait till the cycle is over
  else if(LedState && (millis() - timer) >= CTime){
    timer = millis();  //restart cycle
    digitalWrite(LedPin, LOW);

Dear septillion,

Thank you very much! :slight_smile: :slight_smile: :slight_smile: I really appreciate it. Yes I was struggling to change the 'present' value but noticed that somehow my present value(present_T[]) doesn't go into function properly. Thus it didn't receive the value, and all of beginning present value was always '0'.

I guess these combination of messed up with pointer & set the present time in setup(), were problem. But I couldn't realize about contradicted starting set, neither. To be honest, I am kinda feeling there is something wrong but having hard time to understand clearly. I have lack of understanding about pointer and data types, I've been always somehow messed up with them.

If it is okay, I would like to ask one more. Please correct me if I am wrong.
Though you made BlankTimes/CycleTimes as constant, my ultimate idea was that I make the module which makes LED varies flickering as you did. Then I wanted to play with the values of BlankTimes, CycleTimes, startMillis while LEDs are flickering.

I am trying to adding new lines to vary the values of BlankTimes/CycleTimes. I guess it would be easier. But I have no idea about changing shifting point (I know startMillis will be kept updated)? oh, maybe it would be same if I just force to change the startMillis regardless present value.(!)

First I am going to try anyway, then I will ask further. But any advice would be appreciated. thank you so much for the tips!

But if you want to flicker them all the time there is no shifting point anymore... Shifting is only applicable when you start. While running all that matters are on and off time (or like defined here, off and cycle time). And you can vary it in my program. Just don't make BlankTimes and CycleTimes a const.

The parameters in updateLed() may stay const because it only means they are const for the single call to the function. Which stays true because you don't change those values while running the function, only for the next run.