Pages: [1]   Go Down
Author Topic: Creating Simultaneous Loops  (Read 251 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am having some trouble trying to figure out how to increment a timer to be referenced in if and while statements in a second loop. I want to be able to get rid of the timer incrementation in the while statements so I can just have one timer to reference by. I would like to have the timer count down from a set value then keep counting down in 1 second increments until 0 while the buzzers go off. If I could have something like a completely separate loop for the timer increment counter and a completely separate one for the buzzers and whatever else I want to add later such as a timer display or something.

Code:
#include <Tone.h>

Tone tone1;
Tone tone2;

long countervalue = 1;
int RLED = 1;
int YLED = 2;
int GLED = 3;
int button = 0;
void setup(){
  tone1.begin(4);
  tone2.begin(4);
  pinMode(RLED,OUTPUT);
  pinMode(YLED, OUTPUT);
  pinMode(GLED, OUTPUT);
  pinMode(button,INPUT);
}


void loop()
{

  while(countervalue < 4){
    digitalWrite(GLED,HIGH);
    tone1.play(575 , 150);
    delay(3000);
    countervalue = (countervalue+1)%1000000;
    digitalWrite(GLED,LOW);
  }

  while(countervalue <= 8){
    digitalWrite(YLED,HIGH);
    tone1.play(100 , 1000);
    tone2.play(250 , 1000);
    delay(2000);
    countervalue = (countervalue+1)%1000000;
    digitalWrite(YLED,LOW);
  }

  if(countervalue == 9){
    digitalWrite(RLED,HIGH);
    tone1.play(350, 1000);
    delay(1000);
    tone1.play(200, 1000);
    delay(1000);
    countervalue = (countervalue+1)%1000000;

  }

}
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 114
Posts: 4267
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Read up on 'blink without delay' to see how you can time something without blocking execution of other code, which is what delay() does.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Pages: [1]   Go Up
Jump to: