Issues combining 2 programs in the void loop

Hi all,

Im trying to combine 2 programs, flashing lights and a buzzer (while pressing a button) for my toddler son… he is obsessed with fire trucks lol.
The siren sound works well on its own from the buzzer but when I combine it with the flashing lights program the siren doesnt work properly anymore.

This is the program:

int buzzerPin = 9; // the number of the buzzer pin
int led1Pin = 10; // the number of the LED1 pin
int led2Pin = 11; // the number of the LED2 pin
float sinVal; // Define a variable to save sine value
int toneVal; // Define a variable to save sound frequency
int buttonPin = 7;

void buzzer(int buzzer1) {
digitalWrite(buzzerPin, buzzer1);
}
void setLed(int led1, int led2) {
digitalWrite(led1Pin, led1); // the state of LED1 is determined by variable led1.
digitalWrite(led2Pin, led2); // the state of LED2 is determined by variable led2.
delay(500); // wait for a second

}
void setup() {
// initialize the LED pin as an output:
pinMode(buzzerPin, OUTPUT);
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
pinMode(buttonPin, INPUT);
}

void loop() {

while (digitalRead(buttonPin) == HIGH);
delay(5);
setLed(LOW, HIGH); // set LED1 on, and LED2 off.
setLed(HIGH, LOW); // set LED1 off, and LED2 on.
setLed(LOW, HIGH); // set LED1 on, and LED2 off.
setLed(HIGH, LOW); // set LED1 off, and LED2 on.
setLed(LOW, HIGH); // set LED1 on, and LED2 off.
setLed(HIGH, LOW); // set LED1 off, and LED2 on.
setLed(LOW, HIGH); // set LED1 on, and LED2 off.
setLed(HIGH, LOW); // set LED1 off, and LED2 on.
buzzer(HIGH);
if (digitalRead(buttonPin) == LOW);
setLed(LOW, LOW); // set LED1 on, and LED2 off.
setLed(LOW, LOW); // set LED1 off, and LED2 on.
buzzer(LOW);

//buzzer program with sinus wave (works well on its own)
for (int x = 0; x < 360; x++) { // X from 0 degree->360 degree
sinVal = sin(x * (PI / 180)); // Calculate the sine of x
toneVal = 2000 + sinVal * 500; // Calculate sound frequency according to the sine of x
tone(buzzerPin, toneVal); // Output sound frequency to buzzerPin
delay(0);

}
}

Thanks in advance!

doesnt work properly anymore.

That conveys no information that we can use to help you. What does the code actually do? How is that different from what you want?

delay(500); // wait for a second

The delays called by setLed are blocking your code so that it will not respond.
Non-blocking timing tutorials:
Several things at a time.
Beginner's guide to millis().
Blink without delay().

Read the how to use this forum-please read sticky to see how to properly post code. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code.

In addition to what groundFungus pointed out the following line of code accomplishes nothing:

  if (digitalRead(buttonPin) == LOW);

And since your code was not formatted I cannot determine for certain what you intended to do. Also, what is the purpose of having a delay(0);?

Thanks for the reply.

The buzzer just make a straight beep, then goes high pitch for a little bit then goes back to straight beep.

I read the first two tutorials you linked briefly prior to posting, couldnt find in it how it will solve my issue, could just be more advanced then my understanding yet… so I figured ill try some more experienced set of eyes having a look at it in the forums…
I’ll have a look at the third one, could help with the blinking issue I’m having at least.

“Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code.” Cheers didnt know about it.

“And since your code was not formatted I cannot determine for certain what you intended to do. Also, what is the purpose of having a delay(0);?”

I was trying all sorts of different things changing the delay times and or removing it was one of those things, I just copied the code I have on it right now

Is this an active buzzer ?

Suggest you never use while() in your sketches, at least not until you son is 12 years ;).

Study the ‘ State Change Detection (Edge Detection)’ discussion.

Can you follow this sketch ?

//ToddlerLEDsBuzzer.ino
//
//https://forum.arduino.cc/index.php?topic=702449.msg4720514#msg4720514
//
// YY/MM/DD   Version
// 20/08/27   1.00          Working code
//

#define PUSHED              LOW //the switch is connected between GND and our pin

const byte buttonPin      = 7;
const byte buzzerPin      = 9;  //the number of the buzzer pin
const byte led1Pin        = 10; //the number of the LED1 pin
const byte led2Pin        = 11; //the number of the LED2 pin
const byte heartbeatLED   = 13; //toggles every 500ms if code is non-blocking

float sinVal;                   //Define a variable to save sine value
int toneVal;                    //Define a variable to save sound frequency
unsigned int buzzerNumber = 0;

byte lastButtonState;
byte pushNumber           = 0;

bool LEDflag              = false;
bool buzzerFlag           = false;

//timing stuff
unsigned long flashRate   = 250;
unsigned long buzzerTime  = 5;

unsigned long switchMillis;
unsigned long LEDmillis;
unsigned long buzzerMillis;
unsigned long heartbeatMillis;

//********************************************************************
void setup()
{
  pinMode(heartbeatLED, OUTPUT);
  pinMode(buzzerPin, OUTPUT);
  pinMode(led1Pin, OUTPUT);
  pinMode(led2Pin, OUTPUT);

  pinMode(buttonPin, INPUT_PULLUP);  //closed = LOW

  randomSeed(analogRead(A0));

} //END of setup()

//********************************************************************
void loop()
{
  //*********************************
  //time to toggle the heartbeat LED ?
  if (millis() - heartbeatMillis >= 500)
  {
    //restart the Timer
    heartbeatMillis = millis();

    digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));

  }

  //*********************************
  //check the switches
  checkSwitches();

  //*********************************
  //time to toggle the LEDs ?
  if (LEDflag == true && millis() - LEDmillis >= flashRate)
  {
    //restart the Timer
    LEDmillis = millis();

    digitalWrite(led1Pin, random(0, 2));
    digitalWrite(led2Pin, random(0, 2));

  }

  //*********************************
  //time for the next sound to be made ?
  if (buzzerFlag == true && millis() - buzzerMillis >= buzzerTime)
  {
    //restart the Timer
    buzzerMillis = millis();

    //buzzer program with sine wave
    sinVal = sin(buzzerNumber * (PI / 180)); // Calculate the sine of x
    toneVal = 3300 + sinVal * 500; // Calculate sound frequency according to the sine of x

    tone(buzzerPin, toneVal); // Output sound frequency to buzzerPin

    //buzzerNumber++;

    buzzerNumber = buzzerNumber + 20;

    if (buzzerNumber > 360)
    {
      buzzerNumber = 0;
    }

  }

  //*********************************
  //Other non-blocking code goes here
  //*********************************

} //END of loop()

//********************************************************************
void checkSwitches()
{
  //*********************************
  //time to proceed with switch checking ?
  if (millis() - switchMillis < 50)
  {
    return;
  }

  //restart the Timer
  switchMillis = millis();

  byte buttonState = digitalRead(buttonPin);

  //*********************************
  //has the switch changed state ?
  if (lastButtonState == buttonState)
  {
    return;
  }

  //update to the new state
  lastButtonState = buttonState;

  //*********************************
  //was the switch closed ?
  if (buttonState == PUSHED)
  {
    //odd/even number of pushes
    pushNumber = 1 - pushNumber;

    //*********************************
    //should we enable events ?
    if (pushNumber == 1)
    {
      //enable the buzzer
      buzzerFlag = true;

      //restart the Timer
      buzzerMillis = millis();
      buzzerNumber = 0;

      //enable the LEDs
      LEDflag = true;

      //restart the Timer
      LEDmillis = millis();
    }

    //*********************************
    //should we disable events ?
    else if (pushNumber == 0)
    {
      //disable the buzzer
      buzzerFlag = false;
      noTone(buzzerPin);

      //disable the LEDs
      LEDflag = false;
      digitalWrite(led1Pin, LOW);
      digitalWrite(led2Pin, LOW);

    }

  } //END of  if (buttonState == PUSHED)


  //*********************************
  //Other switch code goes here
  //*********************************

} //END of checkSwitches()

//********************************************************************