Coding buzzer into Arduino Project 8

I am working on the projects from the arduino starter kit book. I am on project 8 the hour glass and at the end it suggests adding in a buzzer. The problem I am having is when i use a while loop within the given code, the reset code no longer works.

const int switchPin = 8;
unsigned long previousTime = 0;
int buzzer = 9;
int switchState = 0;
int prevSwitchState = 0;

int led = 2;

long interval = 1000;

void setup() {
  for(int x = 2;x<8;x++){
    pinMode(x, OUTPUT);
  }
  pinMode(buzzer, OUTPUT);
  pinMode(switchPin, INPUT);
}
void loop(){
  unsigned long currentTime = millis();
  if(currentTime - previousTime > interval) {
    previousTime = currentTime;
    digitalWrite(led, HIGH);
    led++;
    while(led == 8){
      tone(9, 1000, 260);
      delay(0);
      tone(9, 1000, 260);
      delay(3000);   
    }
  }
  switchState = digitalRead(switchPin);
  if(switchState != prevSwitchState){
    for(int x = 2;x<8;x++){
      digitalWrite(x, LOW);
      noTone(buzzer);
    }
    led = 2;
    previousTime = currentTime;
  }
  prevSwitchState = switchState;
}

My main goal is to have the buzzer go off every 3 seconds after the last led is on. If i change the while to an If(led == 8) then the reset works how it is supposed to but the buzzer does not continue like Id like it too.

Very new to using arduino and codding but Thank you!!

The value of led isn't changed in your while() loop so once you get into it you can never get back out. So it perfectly meets your stated main goal. The buzzer will continue for ever until you do a real hard reset or power off.

Basically your reset code, whatever that is, needs to be INSIDE the while() loop.

Steve

Note the smiley in your code ?

while(led == 8)

led is a PIN number, it's unlikely to be 8) (nor changing.... you need a digitalRead())

=> Please correct your post above and add code tags around your code:

[code]

[color=blue]// your code is here[/color]

[/code]

.

It should look like this:

// your code is here

(Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)

read forum rules please

Okay sorry about the code. I tried nesting my reset if statement within my while loop but it still does not seem to want to work. At random times it will reset itself but never when I push the button.

Here is my updated code with the digitalWrite to the buzzer and the if reset statement inside the while(led > 7) loop.

const int switchPin = 8;
unsigned long previousTime = 0;
int buzzer = 9;
int switchState = 0;
int prevSwitchState = 0;

int led = 2;

long interval = 1000;

void setup() {
  for(int x = 2;x<8;x++){
    pinMode(x, OUTPUT);
  }
  pinMode(buzzer, OUTPUT);
  pinMode(switchPin, INPUT);
}
void loop(){
  unsigned long currentTime = millis();
  if(currentTime - previousTime > interval) {
    previousTime = currentTime;
    digitalWrite(led, HIGH);
    led++;
    while(led > 7){
      digitalWrite(buzzer, HIGH);
      tone(9, 1000, 260);
      delay(0);
      digitalWrite(buzzer, LOW);
      tone(9, 1000, 260);
      delay(3000); 
      switchState = digitalRead(switchPin);
      if(switchState != prevSwitchState){
        for(int x = 2;x<8;x++){
          digitalWrite(x, LOW);
          noTone(buzzer);
        }
        led = 2;
        previousTime = currentTime;
      }
      prevSwitchState = switchState;
    }
  }
}

You won't hear that tone if you don't wait for 260ms after

 tone(9, 1000, 260);
      delay(0);

why is noTone in the for loop there

for(int x = 2;x<8;x++){
          digitalWrite(x, LOW);
          noTone(buzzer);
        }

why do you have a while here    while(led > 7){you won't loop much since you set led to 2 as part of the execution of the associated statement. a if would do, wouldn't it ?

how is the switch wired do you have a pull-up or pull down set up?

So I need to change the delay? as of right now the tone does what i would like it too with going on and off.

The noTone was originally there for when I had an if statement for the buzzer to just go off until reset. I decided to try and change it to a repeating buzzer im assuming that noTone does not need to be there?

So my first thought was for the buzzer to go on and off it will need to be in a while loop. Then the code within the if statement below is what I was trying to use to reset the entire process. Once the button is pressed I wanted to set LED back to 2 which is what I have my first led of 6 wired too.

My switch is actually just a button which is wired to pin 8.

EDIT//

As of right now with that changed code I provided, the first cycle the lights will light up then the buzzer will on and off when it is need. I have to hold the button down and it will reset as I want. But then the very next cycle the buzzer will reset on its own after one buzz. then repeat the process of needing to hold the button down and so on with the reset on its own.

Unless I'm misreading your code buzzer is on pin 9. It's really confusing when you sometimes use the pin name 'buzzer' and sometimes use the pin number.

What sort of buzzer are you using? The digitalWrite(buzzer,HIGH/LOW) would be for an active buzzer but the tone(9,x,x) commands are what you use for a passive buzzer. You NEVER need both of them so that's just confusing things.

The reason you have to hold the switch is that delay(3000). That means do nothing not even look at the switch for a full 3 seconds. You really need to be timing using millis() if you want responsive code.

Steve

Okay the digitalWrite to the buzzer was a suggestion from a friend, I realize now that I only need one tone command to get the buzzer to repeat. The buzzer is in fact on pin 9. That again was suggestion when i originally only used "buzzer" as pin name.

I am going to try and figure out the millis() you mentioned as I have only followed the code from the starter kit book and am trying to incorporate the buzzing effect.

As for the second cycle resetting on its own, do you have any suggestions on why that might be happening? It was reset fine first cycle then the second it will reset after one buzz then go back to allowing for the button to reset.

first of all: if you connect your buzzer directly to GND and +5V do you hear a single "click" and that's all or doy hear your buzzer buzz all the time it is connected to power?

An active buzzer just needs to be connected to power and will buzz.

An passive buzzer needs to be switched ON/OFF as long as it should buzz.
passive buzzer needs the command tone

an active buzzer just need a the output set to HIGH for buzzing and set to LOW for beeing quiet.

I'm not sure if I understand what you want the buzzer to do.
Should the buzzer be on for 3 seconds and then stay quit forever?

Should the buzzer be on for 3 seconds then turn OFF (for how long?) and then turn on again
to create a signal

beep 3 seconds OFF 1 second beep 3 seconds OFF 1 second beep 3 seconds OFF 1 second beep 3 seconds OFF 1 second ...?

This Arduino-project-book introduces to Serial.print() to send information to the serial-monitor.
Serial.print() can be used and is used a lot for debugging.

To analyse what your code is doing serial debug-output is that thing to do.

here is a codeversion that is based on the code you have posted above.
I added serial-debugoutput to it.

So open the serial-monitor
adjust baudrate to 115200
activate show timestamp

then upload this codeversion and watch the serial-output
This will give you a detailed picture of what is going on in your code.

const int switchPin = 8;
const int buzzerPin = 13;

int switchState     = 0;
int prevSwitchState = 0;

int led = 2;

unsigned long previousTime = 0;

unsigned long interval = 1000;

void setup() {
  Serial.begin(115200);
  Serial.println("Setup Start");
  
  for(int x = 2; x < 8; x++){
    pinMode(x, OUTPUT);
  }
  
  pinMode(buzzerPin, OUTPUT);
  pinMode(switchPin, INPUT);
}

void loop(){
  unsigned long currentTime = millis();
  
  if(currentTime - previousTime > interval) {
	  Serial.println("if(currentTime - previousTime > interval) is TRUE");  
    previousTime = currentTime;
    digitalWrite(led, HIGH);
    led++;
	  Serial.print("value of variable led is ");  
	  Serial.println(led);
	
    while(led > 7){
	    Serial.println("inside and top of while(led > 7) ");  
      digitalWrite(buzzerPin, HIGH);
      tone(9, 1000, 260);
      delay(0);
      digitalWrite(buzzerPin, LOW);
      tone(9, 1000, 260);
      delay(3000);
	    Serial.println("inside and after delay(3000) of while(led > 7) ");  

      switchState = digitalRead(switchPin);

      if(switchState != prevSwitchState){
        Serial.println("switchState has changed");  

        for(int x = 2; x < 8; x++){
          digitalWrite(x, LOW);
          noTone(buzzerPin);
        }

        led = 2;
        Serial.println("led = 2");  
        previousTime = currentTime;
      }
      prevSwitchState = switchState;
	    Serial.println("inside and at bottom of while(led > 7) ");  
    }
  }
}

best regards Stefan

young_ty:
As for the second cycle resetting on its own, do you have any suggestions on why that might be happening? It was reset fine first cycle then the second it will reset after one buzz then go back to allowing for the button to reset.

I don't know because I can't really follow that awful mix of millis() and delay()s that you're using. It might be something to do with when you reset prevSwitchState or it might be that you're holding the button down for too long.

Steve

My switch is actually just a button which is wired to pin 8.

that does not tell us HOW it is wired.

I have seen people trying multiple ways...
Pin 8 —- button
Pin 8 —- button —— GND
Pin 8 —- button —— 5V
Pin 8 —- button —— Resistor —— GND
Pullup, pull down,....

So what’s yours ?