LED and Siren at same time

So I am very new to this, but this is what I have done. I got some LEDs blinking the way I want, but then I add a loop for a siren and it slows everything down. Here is the code:
/*

*/

// the setup function runs once when you press reset or power the board

int i=0;

void setup() {

pinMode(13, OUTPUT); // Blue
pinMode(12, OUTPUT); // Blue
pinMode(11, OUTPUT); // Red
pinMode(10, OUTPUT); // Red
pinMode(9, OUTPUT); //White
pinMode(8, OUTPUT); //Buzzer
pinMode(7, OUTPUT); //White
}

// the loop function runs over and over again forever
void loop() {
digitalWrite(13, HIGH);
digitalWrite(12, HIGH);

delay(150);

digitalWrite(13, LOW);
digitalWrite(12, LOW);

delay(150);

digitalWrite(11, HIGH);
digitalWrite(10, HIGH);

delay(100);
digitalWrite(11, LOW);
digitalWrite(10, LOW);

delay(100);

digitalWrite(7, HIGH);
digitalWrite(9, HIGH);

delay(5);

digitalWrite(7, LOW);
digitalWrite(9, LOW);

delay(5);

for(i=700;i<800;i++){
tone(8,i);
delay(15);
}
for(i=800;i>700;i--){
tone(8,i);
delay(15);
}

}

I have also tried to change it to using more functions (I think thats what they are called) but doesn't work at all:

/*

*/

// the setup function runs once when you press reset or power the board

int i=0;

void setup() {

pinMode(13, OUTPUT); // Blue
pinMode(12, OUTPUT); // Blue
pinMode(11, OUTPUT); // Red
pinMode(10, OUTPUT); // Red
pinMode(9, OUTPUT); //White
pinMode(8, OUTPUT); //Buzzer
pinMode(7, OUTPUT); //White
}

void loop(){

void lightbar();
void buzz();
}

// the loop function runs over and over again forever
void lightbar() {
digitalWrite(13, HIGH);
digitalWrite(12, HIGH);

delay(150);

digitalWrite(13, LOW);
digitalWrite(12, LOW);

delay(150);

digitalWrite(11, HIGH);
digitalWrite(10, HIGH);

delay(100);
digitalWrite(11, LOW);
digitalWrite(10, LOW);

delay(100);

digitalWrite(7, HIGH);
digitalWrite(9, HIGH);

delay(5);

digitalWrite(7, LOW);
digitalWrite(9, LOW);

delay(5);

}

void buzz(){

for(i=700;i<800;i++){
tone(8,i);
delay(15);
}
for(i=800;i>700;i--){
tone(8,i);
delay(15);
}

}

I would like to have the lights blink and the buzzer to go off at the same time. Seems that the lights blink, then the siren goes off, then the lights blink etc. How can I get them to work at the same time,
any help would greatly be appreciated,

Take a look at the several things at time tutorial.

Hi,

You need to enable your buzzer during those delays when the LEDs change their state.

Try this code:

void loop() {


digitalWrite(13, HIGH);   
digitalWrite(12, HIGH);
 

for(i=700;i<750;i++){
  tone(8,i);
  delay(3);
  }
  
 
digitalWrite(13, LOW);
digitalWrite(12, LOW);

for(i=750;i<800;i++){
  tone(8,i);
  delay(3);
  }


digitalWrite(11, HIGH);
digitalWrite(10, HIGH);
   
for(i=800;i>750;i--){
  tone(8,i);
  delay(2);
  }

digitalWrite(11, LOW);
digitalWrite(10, LOW);
 
for(i=750;i>700;i--){
  tone(8,i);
  delay(2);
  }
 
digitalWrite(7,  HIGH);
digitalWrite(9,  HIGH);
 
delay(5);
 
digitalWrite(7, LOW);
digitalWrite(9, LOW);
 
delay(5);


}

Firstly, Since you are a newbie, I would like to give a recommendation about coding

Do NOT put the constant value directly to your code. It's easy to confuse, difficult to debug, difficult to read, difficult to change if needed. Instead, put it in a variable.

For example, DO NOT do

void setup() {
 
  pinMode(13, OUTPUT);  // Blue
  pinMode(10, OUTPUT);  // Red
}

Instead, DO:

const int pinBlue = 12;
const int pinRed = 10;


void setup() {
 
  pinMode(pinBlue , OUTPUT);  // Blue
  pinMode(pinRed , OUTPUT);  // Red
}

Secondly, To do "LED and Siren at same time", DO NOT use delay, use timestamp instead. See BlinkWithoutDelay example

1 Like