Running Multiple functions at once

I need some help figuring out how to run two functions at the same time. I am trying to have my arduino turn on a set of LEDs for x amount of time, while the arduino counts through some LEDs in order to act like a count down clock. I haven't set up all the countdown LEDs and I plan to use 8 for now. Here is my code:



const int clockledpin1 = 2;
const int clockledpin2 = 3;
const int clockledpin3 = 4;
const int clockledpin4 = 5;
const int clockledpin5 = 6;
const int clockledpin6 = 9;
const int clockledpin7 = 10;
const int clockledpin8 = 11;
const int ledPin = 8;
const int buttonPin = 7;
int buttonState = 0;


void flash() {
  digitalWrite(ledPin, HIGH);
  delay(900000);
  digitalWrite(ledPin, LOW);
}
void ledclock() {
  digitalWrite(clockledpin1, HIGH);
  delay(60000);
  digitalWrite(clockledpin1, LOW);
  digitalWrite(clockledpin2, HIGH);
  delay(60000);
  digitalWrite(clockledpin2, LOW);
  digitalWrite(clockledpin3, HIGH);
  delay(60000);
  digitalWrite(clockledpin3, LOW);

}
void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(clockledpin1, OUTPUT);
  pinMode(clockledpin2, OUTPUT);
  pinMode(clockledpin3, OUTPUT);
  pinMode(clockledpin4, OUTPUT);
  pinMode(clockledpin5, OUTPUT);
  pinMode(clockledpin6, OUTPUT);
  pinMode(clockledpin7, OUTPUT);
  pinMode(clockledpin8, OUTPUT);
  pinMode(buttonPin, INPUT);
}

void loop() {
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) {
 flash();
   }
while(ledPin==HIGH){
  ledclock();}
}

If someone could tell me what I need to do to make this work, that would be great.

See Using millis() for timing. A beginners guide, Several things at the same time and the BlinkWithoutDelay example in the IDE

Hello
It seems to be a school assigment, isn´t it?

That cannot be done if you use delay() in your sketch. Period.

Indeed, the rule of thumb is to never use delay() within loop() or in any function called from loop().

Carefully crafted exceptions apply…

As @UKHeliBob said, also stay away from using while( ).


Switches should be handled by looking for switch state changes rather than switch levels.


Also, wire switches as S3 below.

The one thing takes 3 minutes and seems like you want that to run for 15 minutes.

So instead of what won't work as you wrote it, why not just

Call ledclock() 5 times once you see the button get pressed?

Later we can hope you'll discover a better way to approach this general problem, we ready to help...

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.