Using a timer inside a void function

i have this sketch that uses lots of different led effects when a button is pushed. id like the leds to turn off after say 10 mins regardless of what state it was in then if the button is pushed again go to the next effect. ive tried using the millis and a universal timer library but everywhere i put it has no effect, the leds just continue what they were doing. any help or guidance would be great.

#include <DYPlayerArduino.h>


DY::Player player; // Initialise the player, it defaults to using Serial.

int pinsCount=5;                        // declaring the integer variable pinsCount
int pins[] = {5,6,9,10,11};          // declaring the array pins[]
 
const int led1 = 11;
const int led2 = 10;
const int led3 = 9;
const int led4 = 6;
const int led5 = 5;

int pin_switch=2;
int brightness=0;
int state=0;

int ledState = LOW;

const int blinkDuration = 5000; // number of millisecs that Led's are on - all leds use this
unsigned long currentMillis = 0;    // stores the value of millis() in each iteration of loop()
unsigned long previousOnBoardLedMillis = 0;


void setup() {

  for (int i=0; i<pinsCount; i=i+1){    // counting the variable i from 0 to 9
    pinMode(pins[i], OUTPUT);            // initialising the pin at index i of the array of pins as OUTPUT
  }
   player.begin();
  player.setVolume(30); // 100% Volume
Serial.begin(9600);
  pinMode(led1, OUTPUT); // Make Digital Pin 9 an OUTPUT
  pinMode(led2, OUTPUT); // Make Digital Pin 9 an OUTPUT
  pinMode(led3, OUTPUT); // Make Digital Pin 9 an OUTPUT
  pinMode(led4, OUTPUT); // Make Digital Pin 9 an OUTPUT
  pinMode(led5, OUTPUT); // Make Digital Pin 9 an OUTPUT
  attachInterrupt(0,interrupt_service_routine,RISING);
 
}
// change
int preState = 0;



void loop() {
 

// change
  if(preState != state){
    if(state == 1){
      player.playSpecified(7);
      timer();
    }else if(state == 2){
     player.playSpecified(1);
    }else if(state == 3){
     player.playSpecified(2);
    }else if(state == 4){
     player.playSpecified(5);
    }else if(state == 5){
    player.playSpecified(3);
    }else if(state == 6){
    player.playSpecified(6);
    }else if(state == 7){
    player.playSpecified(8);
    }else if(state == 8){
    player.playSpecified(4);
    }else if(state == 9){
   ;
    } 
    preState = state;
  }
  
  
  
  Serial.println(state);
if(state==0)
effect1();
else if(state==1)
effect2();
else if (state==2)
twinkle();
else if(state==3)
effect3();
else if(state==4)
blink();
else if(state==5)
chase();
else if(state==6)
{combo();
twinkle(); }
else if(state==7)
effect6();
else if(state==8)
all();
else if(state==9)
{ digitalWrite(led1, LOW);
                digitalWrite(led2, LOW);
                digitalWrite(led3, LOW);
                digitalWrite(led4, LOW);
                digitalWrite(led5, LOW);}
                
else if(state==10||state>10)
state=0;
  }

  void interrupt_service_routine(){
   
    state=state+1;
    }

 //----------------------------------FUNCTIONS-------------------------------------------------------------
    
 void  blink(){
            
  
  
                digitalWrite(led1, HIGH);
                digitalWrite(led2, HIGH);
                digitalWrite(led3, HIGH);
                digitalWrite(led4, HIGH);
                digitalWrite(led5, HIGH);
                 delay(2000);
                  digitalWrite(led1, LOW);
                digitalWrite(led2, LOW);
                digitalWrite(led3, LOW);
                digitalWrite(led4, LOW);
                digitalWrite(led5, LOW);
                delay(2000);
  }
 
  void twinkle() {
  
 
   // Set the brightness of the LED to a random value.
  analogWrite(led1, random(200)+150);
  analogWrite(led2, random(200)+150);
  analogWrite(led3, random(200)+150);
  analogWrite(led4, random(200)+150);
  analogWrite(led5, random(200)+150);

  // Randomly delay for a period of time between 0 and 0.3 seconds.
  delay(random(300));
}
 void fade()
{   
  for (brightness = 0; brightness <= 255; brightness += 10) {
    analogWrite(11, brightness);
    analogWrite(10, brightness);
    analogWrite(9, brightness);
    analogWrite(6, brightness);
    analogWrite(5, brightness);
    
    delay(30); // Wait for 30 millisecond(s)
  }
  for (brightness = 255; brightness >= 0; brightness -= 7) {
    analogWrite(11, brightness);
    analogWrite(10, brightness);
    analogWrite(9, brightness);
    analogWrite(6, brightness);
    analogWrite(5, brightness);
    delay(30); // Wait for 30 millisecond(s)
  }
}
  void combo(){ 
   
    for(int ii=0;ii<2;ii=ii+1)
    blink();
    for(int ii=0;ii<2;ii=ii+1)
  fade();
  for(int ii=0;ii<2;ii=ii+1)
  twinkle();
  chase();
    }
 

 void effect1()
 {   digitalWrite(led1, HIGH);
                digitalWrite(led2, HIGH);
                digitalWrite(led3, HIGH);
                digitalWrite(led4, HIGH);
                digitalWrite(led5, HIGH);

 }

 void effect2()
 {        timer();       
  digitalWrite(led1, LOW);
                digitalWrite(led2, LOW);
                digitalWrite(led3, LOW);
                digitalWrite(led4, LOW);
                digitalWrite(led5, LOW);
                fade();
               
 }

void effect3()

 { for (brightness = 0; brightness <= 255; brightness += 15) {
    analogWrite(11, brightness);
    analogWrite(10, brightness);
    analogWrite(9, brightness);
    analogWrite(6, brightness);
    analogWrite(5, brightness);
    delay(100);
    digitalWrite(led1, LOW);
                digitalWrite(led2, LOW);
                digitalWrite(led3, LOW);
                digitalWrite(led4, LOW);
                digitalWrite(led5, LOW);
    
}
 }


 void effect4()
 {
 digitalWrite(led3,HIGH);
delay(1000);
digitalWrite(led3,LOW);
delay(1000);
 }

 void chase()
 {
for (int i=0; i<pinsCount; i=i+1){    // chasing right
    digitalWrite(pins[i], HIGH);         // switching the LED at index i on
    delay(100);                          // stopping the program for 100 milliseconds
    digitalWrite(pins[i], LOW);          // switching the LED at index i off
  }
  for (int i=pinsCount-1; i>0; i=i-1){   // chasing left (except the outer leds)
    digitalWrite(pins[i], HIGH);         // switching the LED at index i on
    delay(100);                          // stopping the program for 100 milliseconds
    digitalWrite(pins[i], LOW);          // switching the LED at index i off
 }
 }
   void effect6()
{                               digitalWrite(led1, HIGH);
                                delay (500);  
                                digitalWrite(led2,HIGH);
                                delay (500);  
                                digitalWrite(led3,HIGH);
                                delay (500);  
                                digitalWrite(led4,HIGH);
                                delay (500);  
                                digitalWrite(led5,HIGH);
                                delay (500);
                                digitalWrite (led5,LOW);
                               delay (500);
                               digitalWrite (led4,LOW);
                               delay (500);
                               digitalWrite (led3,LOW);
                               delay (500);
                               digitalWrite (led2,LOW);
                                delay (500);
                              digitalWrite (led1,LOW);
}

void all()
{ effect6();
combo();
chase();
effect2();
effect3();
effect4();
twinkle();
blink();}

 void timer(){
            currentMillis = millis();   // capture the latest value of millis()

          // if the Led is on, we must wait for the duration to expire before turning it off
    if (currentMillis - previousOnBoardLedMillis >= blinkDuration) 
          // time is up, so change the state to LOW
       ledState = LOW;
          // and save the time when we made the change
       previousOnBoardLedMillis += blinkDuration;
     

    // Set the LED state
    digitalWrite(led1, ledState);
    digitalWrite(led2, ledState);
    digitalWrite(led3, ledState);
    digitalWrite(led4, ledState);
    digitalWrite(led5, ledState);

  }
 

No problem. Present the flow chart of the code.
One question. If You've composed this code, what stops You from developing it further?

From the attachInterrupt manual page:

Syntax

attachInterrupt(digitalPinToInterrupt(pin), ISR, mode) (recommended)
attachInterrupt(interrupt, ISR, mode) (not recommended)
attachInterrupt(pin, ISR, mode) (Not recommended. Additionally, this syntax only works on Arduino SAMD Boards, Uno WiFi Rev2, Due, and 101.)

I wonder if your line should read

attachInterrupt(digitalPinToInterrupt(0), interrupt_service_routine, RISING); //also, pin 0? Not pin_switch?

I didnt write the code just the different effects. A paid a guy in fiver to help out but its already cost more than i hoped. Im not sure what u mean by a flowchart, super new to coding.

paid a guy in fiver to help out

Sadly, this person did not know what they are doing. We don't recommend that beginners work with interrupts, because attempting to use them usually causes more problems than are solved.

Which Arduino are you using?
Which pin is supposed to trigger the interrupt (see above)?

You need to declare variables that are shared with interrupt routines as "volatile", e.g.

volatile int state=0;

Furthermore in the main program you should access them only with interrupts off. Example for Uno and other AVR Arduinos:

cli(); //interrupts off
int state_copy=state; //copy the value
sei(); //interrupts back on
...
1 Like

I suspected he wasnt great because i asked him to debounce the button as well. So now im stuck with this code. I had another sketch that worked but for some reason the twinkle function wouldn’t twinkle.

If twinkle() was called from an interrupt routine, delay() would not have worked.

One of the other problems with using interrupts.

Consider this a learning experience.

No the sketch i started with had no interupts.

You forgot to tell us what all the code should do, and what it does instead.

We STRONGLY recommend to start simply and get one little bit working at a time. When each bit works to your satisfaction, start adding bits together and test to make sure the combination works, before adding another.

1 Like

Go back to that code, post it here and see what that than can bring.
Never trust miracle code makers. Learn coding and do it Yourself.

Delete posts...... What's next? Deleting the the entire question?

int pin_switch = 2;

int brightness = 0;

// variables to hold the new and old switch states
boolean oldSwitchState = LOW;
boolean newSwitchState1 = LOW;
boolean newSwitchState2 = LOW;
boolean newSwitchState3 = LOW;
boolean newSwitchState4 = LOW;
boolean newSwitchState5 = LOW;
 
byte state = 0;

void setup()
{
  Serial.begin(9600);
  pinMode(led1, OUTPUT); // Make Digital Pin 9 an OUTPUT
  pinMode(led2, OUTPUT); // Make Digital Pin 9 an OUTPUT
  pinMode(led3, OUTPUT); // Make Digital Pin 9 an OUTPUT
  pinMode(led4, OUTPUT); // Make Digital Pin 9 an OUTPUT
  pinMode(led5, OUTPUT); // Make Digital Pin 9 an OUTPUT
  
}

void fade()
{
  for (brightness = 0; brightness <= 255; brightness += 5) {
    analogWrite(11, brightness);
    analogWrite(10, brightness);
    analogWrite(9, brightness);
    analogWrite(6, brightness);
    analogWrite(5, brightness);
    
    delay(30); // Wait for 30 millisecond(s)
  }
  for (brightness = 255; brightness >= 0; brightness -= 5) {
    analogWrite(11, brightness);
    analogWrite(10, brightness);
    analogWrite(9, brightness);
    analogWrite(6, brightness);
    analogWrite(5, brightness);
    delay(30); // Wait for 30 millisecond(s)
  }
}

void twinkle() {
  
  
   // Set the brightness of the LED to a random value.
  analogWrite(led1, random(200)+150);
  analogWrite(led2, random(200)+150);
  analogWrite(led3, random(200)+150);
  analogWrite(led4, random(200)+150);
  analogWrite(led5, random(200)+150);

  // Randomly delay for a period of time between 0 and 0.3 seconds.
  delay(random(300));
}

void loop() {
  Serial.println(state);

   newSwitchState1 = digitalRead(pin_switch);
    delay(1);
    newSwitchState2 = digitalRead(pin_switch);
    delay(1);
    newSwitchState3 = digitalRead(pin_switch);
    delay(1);
     newSwitchState4 = digitalRead(pin_switch);
    delay(1);
     newSwitchState5 = digitalRead(pin_switch);

     if (  (newSwitchState1==newSwitchState2) && (newSwitchState1==newSwitchState3) && (newSwitchState1==newSwitchState4) && (newSwitchState1==newSwitchState5))
 
     if ( newSwitchState1 != oldSwitchState ) 
        {
 
           // has the button switch been closed?
           if ( newSwitchState1 == HIGH )
           {
                // increase the value of state
                state++;
                if (state > 5) { state = 0; }
 
                // turn all LEDs off. Doing it this way means we do not need to care about the individual LEDs
                // simply turn them all off and then turn on the correct one. 
                digitalWrite(led1, LOW);
                digitalWrite(led2, LOW);
                digitalWrite(led3, LOW);
                digitalWrite(led4, LOW);
                digitalWrite(led5, LOW);
                
                // Turn on the next LED
                // Because the value of state does not change while we are testing it we don't need to use else if
                if (state==1) blinkx();
                if (state==2) twinkle();  
                if (state==3) fadey(); 
                if (state==4) combo();
                if (state==5) digitalWrite(led3, HIGH);
 
           }
           oldSwitchState = newSwitchState1;

        }
        
}

void blinks() {
   digitalWrite (11, HIGH);
   delay (500);
   digitalWrite (11, LOW);
   delay (100);
}
void blinkx() {
     for(int ii = 0; ii < 10; ii++){
      blinks();
     }


     }
void fadey() {
  for(int f = 0; f < 10; f++) {
    fade();
  } 
  }

void combo() {
  for(int ii = 0; ii < 5; ii++)
      blinks();
     for(int f = 0; f < 5; f++) 
    fade();  
}

//void effect1()
//{                               digitalWrite(led1, HIGH);
  //                              delay (500);  
    //                            digitalWrite(led2,HIGH);
      //                          delay (500);  
        //                        digitalWrite(led3,HIGH);
          //                      delay (500);  
            //                    digitalWrite(led4,HIGH);
              //                  delay (500);  
                //                digitalWrite(led5,HIGH);
                  //              delay (28000);
                    //            digitalWrite (led5,LOW);
                      //          delay (500);
                        //        digitalWrite (led4,LOW);
                          //      delay (500);
                            //    digitalWrite (led3,LOW);
                              //  delay (500);
                                //digitalWrite (led2,LOW);
//                                delay (500);
 //                               digitalWrite (led1,LOW); }

//void effect2()
//{    digitalWrite(led3, HIGH); }

void effect3()
{                              analogWrite(led1, random(200)+150);
                               analogWrite(led2, random(200)+150);
                               analogWrite(led3, random(200)+150);
                               analogWrite(led4, random(200)+150);
                               analogWrite(led5, random(200)+150);

  // Randomly delay for a period of time between 0 and 0.3 seconds.
                             delay(random(300));;  }
 

this was what i started with, hadnt put the sounds to it yet or the other effects, nor have i debounced the button yet

Here is the first "making order", the code autoformatted in the IDE.

int pin_switch = 2;

int brightness = 0;

// variables to hold the new and old switch states
boolean oldSwitchState = LOW;
boolean newSwitchState1 = LOW;
boolean newSwitchState2 = LOW;
boolean newSwitchState3 = LOW;
boolean newSwitchState4 = LOW;
boolean newSwitchState5 = LOW;

byte state = 0;

void setup()
{
  Serial.begin(9600);
  pinMode(led1, OUTPUT); // Make Digital Pin 9 an OUTPUT
  pinMode(led2, OUTPUT); // Make Digital Pin 9 an OUTPUT
  pinMode(led3, OUTPUT); // Make Digital Pin 9 an OUTPUT
  pinMode(led4, OUTPUT); // Make Digital Pin 9 an OUTPUT
  pinMode(led5, OUTPUT); // Make Digital Pin 9 an OUTPUT

}

void fade()
{
  for (brightness = 0; brightness <= 255; brightness += 5) {
    analogWrite(11, brightness);
    analogWrite(10, brightness);
    analogWrite(9, brightness);
    analogWrite(6, brightness);
    analogWrite(5, brightness);

    delay(30); // Wait for 30 millisecond(s)
  }
  for (brightness = 255; brightness >= 0; brightness -= 5) {
    analogWrite(11, brightness);
    analogWrite(10, brightness);
    analogWrite(9, brightness);
    analogWrite(6, brightness);
    analogWrite(5, brightness);
    delay(30); // Wait for 30 millisecond(s)
  }
}

void twinkle() {


  // Set the brightness of the LED to a random value.
  analogWrite(led1, random(200) + 150);
  analogWrite(led2, random(200) + 150);
  analogWrite(led3, random(200) + 150);
  analogWrite(led4, random(200) + 150);
  analogWrite(led5, random(200) + 150);

  // Randomly delay for a period of time between 0 and 0.3 seconds.
  delay(random(300));
}

void loop() {
  Serial.println(state);

  newSwitchState1 = digitalRead(pin_switch);
  delay(1);
  newSwitchState2 = digitalRead(pin_switch);
  delay(1);
  newSwitchState3 = digitalRead(pin_switch);
  delay(1);
  newSwitchState4 = digitalRead(pin_switch);
  delay(1);
  newSwitchState5 = digitalRead(pin_switch);

  if (  (newSwitchState1 == newSwitchState2) && (newSwitchState1 == newSwitchState3) && (newSwitchState1 == newSwitchState4) && (newSwitchState1 == newSwitchState5))

    if ( newSwitchState1 != oldSwitchState )
    {

      // has the button switch been closed?
      if ( newSwitchState1 == HIGH )
      {
        // increase the value of state
        state++;
        if (state > 5) {
          state = 0;
        }

        // turn all LEDs off. Doing it this way means we do not need to care about the individual LEDs
        // simply turn them all off and then turn on the correct one.
        digitalWrite(led1, LOW);
        digitalWrite(led2, LOW);
        digitalWrite(led3, LOW);
        digitalWrite(led4, LOW);
        digitalWrite(led5, LOW);

        // Turn on the next LED
        // Because the value of state does not change while we are testing it we don't need to use else if
        if (state == 1) blinkx();
        if (state == 2) twinkle();
        if (state == 3) fadey();
        if (state == 4) combo();
        if (state == 5) digitalWrite(led3, HIGH);

      }
      oldSwitchState = newSwitchState1;

    }

}

void blinks() {
  digitalWrite (11, HIGH);
  delay (500);
  digitalWrite (11, LOW);
  delay (100);
}
void blinkx() {
  for (int ii = 0; ii < 10; ii++) {
    blinks();
  }


}
void fadey() {
  for (int f = 0; f < 10; f++) {
    fade();
  }
}

void combo() {
  for (int ii = 0; ii < 5; ii++)
    blinks();
  for (int f = 0; f < 5; f++)
    fade();
}

//void effect1()
//{                               digitalWrite(led1, HIGH);
//                              delay (500);
//                            digitalWrite(led2,HIGH);
//                          delay (500);
//                        digitalWrite(led3,HIGH);
//                      delay (500);
//                    digitalWrite(led4,HIGH);
//                  delay (500);
//                digitalWrite(led5,HIGH);
//              delay (28000);
//            digitalWrite (led5,LOW);
//          delay (500);
//        digitalWrite (led4,LOW);
//      delay (500);
//    digitalWrite (led3,LOW);
//  delay (500);
//digitalWrite (led2,LOW);
//                                delay (500);
//                               digitalWrite (led1,LOW); }

//void effect2()
//{    digitalWrite(led3, HIGH); }

void effect3()
{ analogWrite(led1, random(200) + 150);
  analogWrite(led2, random(200) + 150);
  analogWrite(led3, random(200) + 150);
  analogWrite(led4, random(200) + 150);
  analogWrite(led5, random(200) + 150);

  // Randomly delay for a period of time between 0 and 0.3 seconds.
  delay(random(300));;
}

i dont understand, thats almost exactly what i posted minus the led declaration. is my code not formatted properly?

You need to rewrite the code a little regarding the strategy. Delay must be wiped out.

Take some time and explore the topics "blink without delay" and "do many several things a the same time". They are the keys to work this out.
This work is a valuable investment for Your coding skill and a very, very useful tool.

Where ever there is a delay You need to use time stamps.
When an activity is started set a "turn-off-time" for that activity and check for that. Loop() must run at full speed all the time. This makes things possible....

Then, in loop, You will check for the button supposed to kill, change, whatever action going on.

It's much more easy to read when formatted in the IDE. I did some more checking of the code after that.

im starting from scratch now

Yes and no...

Start exploring the technics told. Then apply them to Your project. Once You manage to run one effect, the other ones will be easy to get working.
I know it's not a quick fix but it will pay off, be sure.
Being not experienced, starting a project, it's easy to face a dead end or a mess that have no miracle fixes.



#include <Noiasca_button.h>            // download library from http://werner.rothschopf.net/microcontroller/202202_tools_led_en.htm

Button buttonA {2, HIGH};                  // active high: button connects pin with GND
unsigned long count = 0;
const int led1 = 11;
const int led2 = 10;
const int led3 = 9;
const int led4 = 6;
const int led5 = 5;


void setup() {
  Serial.begin(9600);
  pinMode(led1, OUTPUT); // Make Digital Pin 9 an OUTPUT
  pinMode(led2, OUTPUT); // Make Digital Pin 9 an OUTPUT
  pinMode(led3, OUTPUT); // Make Digital Pin 9 an OUTPUT
  pinMode(led4, OUTPUT); // Make Digital Pin 9 an OUTPUT
  pinMode(led5, OUTPUT); // Make Digital Pin 9 an OUTPUT
  buttonA.begin();                     // you have to call the .begin() method
  
}

void loop() {
  if (buttonA.wasPressed())  // fire if button was pressed

  { Serial.println(count);
    (count++);
    if (count >= 10) {
      count = 0;
    }
    if (count == 0) {  digitalWrite(led1, LOW);
        digitalWrite(led2, LOW);
        digitalWrite(led3, LOW);
        digitalWrite(led4, LOW);
        digitalWrite(led5, LOW);
    }
    if (count == 1) { digitalWrite(led1, HIGH);
    }
    if (count == 2) {digitalWrite(led2, HIGH);
    }
    if (count == 3) {digitalWrite(led3, HIGH);
    }
    if (count == 4) {digitalWrite(led4, HIGH);
    }
    if (count == 5) {digitalWrite(led5, HIGH);
    }
    if (count == 6) {effect1();
    }
    if (count == 7) {twinkle();
    }
    if (count == 8) {
    }
    if (count == 9) {
    }
    if (count == 10) {
    }

  }
}

void effect1()
{
        digitalWrite(led1, LOW);
        digitalWrite(led2, HIGH);
        digitalWrite(led3, LOW);
        digitalWrite(led4, HIGH);
        digitalWrite(led5, HIGH);
}

void twinkle() {


  // Set the brightness of the LED to a random value.
  analogWrite(led1, random(200) + 150);
  analogWrite(led2, random(200) + 150);
  analogWrite(led3, random(200) + 150);
  analogWrite(led4, random(200) + 150);
  analogWrite(led5, random(200) + 150);

  // Randomly delay for a period of time between 0 and 0.3 seconds.
  delay(random(300));
}

this is started from scratch, again the twinkle or flicker function just has the leds stay on. is there a problem with the analogWrite?

Have You investigated and tried the topics told in reply #16? Your reply tells me "No".
I don't find the flicker function......
Look at twinkle. You need a boolean variable telling whether twinkle is running or stopped.
If twinkle is called and it's in the stop state, set twinkleActive = true as well as an eventual twinkleStopTime. Next time twinkle is called the code know that and check if twinkleStopTime is reached.