How can I stop the buzzer?

int rgbGreen = A3;
int rgbBlue = A4;
int rgbRed = A5;

int a1 = 4; //Bit 0 Decoder 1
int a2 = 5; //Bit 1 Decoder 1
int a3 = 6; //Bit 2 Decoder 1
int a4 = 7; //Bit 3 Decoder 1

int b1 = 8; //Bit 0 Decoder 2
int b2 = 9; //Bit 1 Decoder 2
int b3 = 10; //Bit 2 Decoder 2
int b4 = 11; //Bit 3 Decoder 2 

int button = 12;
int buzzer = 13;

int d1; //To Store the first Digit
int d2; // To Store the Second Digit
int n=0; //Setting initial value of n to 0

int buttonState = LOW; // button starts low
int buttonPreesAmount = 0; // in the first time amount is zero.
                           // when people press button amount of press
                           // will increase 1 by 1.

int myNumber;

void setup()
{
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
pinMode(button,INPUT);
pinMode(13,OUTPUT);
  

Serial.begin(9600); // It makes serial communication ready.
while (! Serial); // Wait untilSerial is ready
delay(200);
Serial.println("The system was reset. It was started again.");
delay(200);
Serial.println("Welcome");
delay(200);
Serial.println("Enter any number between 0 - 99, please.");

}

 
 void loop()
{
   
 if (Serial.available() > 0)
  {
  myNumber = Serial.parseInt();
  Serial.print("Number is: ");
  Serial.println(myNumber);  
                            
  }  

 buttonState = digitalRead(button); 
   
 if (buttonState == HIGH) 
   {
   
   buttonPreesAmount = buttonPreesAmount+1;
   Serial.print("Amount of press: ");
   Serial.println(buttonPreesAmount);
   delay(100);
    
   }

 if(buttonPreesAmount == 1)
 {
   analogWrite(rgbRed,255);
   delay(1000);
   analogWrite(rgbRed,0);
   delay(1000);
   analogWrite(rgbBlue,255);
   delay(1000);
   analogWrite(rgbBlue,0);
   delay(1000);
   analogWrite(rgbGreen,255);
   delay(1000);
   analogWrite(rgbGreen,0);
   delay(1000);
   
 }
   
   
 
 else if(buttonPreesAmount == 2)
 {
  n++; 
  
       
        
   delay(250);
   
   if (n == myNumber+1)
   {
     tone(buzzer,1000,1000);                     
      analogWrite(rgbRed,255);                            
      delay(1000);                                  
      analogWrite(rgbRed,0);                            
      tone(buzzer,300,1000);                           
      analogWrite(rgbBlue,255);
      delay(1000);          
      analogWrite(rgbBlue,0); 
     tone(buzzer,1000,1000);                 
      analogWrite(rgbRed,255);                               
      delay(1000);                                     
      analogWrite(rgbRed,0);                              
      tone(buzzer,300,1000);                              
      analogWrite(rgbBlue,255);
      delay(1000);          
      analogWrite(rgbBlue,0);
     tone(buzzer,1000,1000);                    
      analogWrite(rgbRed,255);                            
      delay(1000);                                      
      analogWrite(rgbRed,0);                              
      tone(buzzer,300,1000);                              
      analogWrite(rgbBlue,255);
      delay(1000);             
      analogWrite(rgbBlue,0); 
     tone(buzzer,1000,1000);                    
      analogWrite(rgbRed,255);                             
      delay(1000);                                   
      analogWrite(rgbRed,0);                                
      tone(buzzer,300,1000);                              
      analogWrite(rgbBlue,255);
      delay(1000);           
      analogWrite(rgbBlue,0); 
     
     n=0; 
     
     buttonPreesAmount =0; 
    
   } 
  
 }
   
  
 else if(buttonPreesAmount == 3)
 {
   buttonPreesAmount =0;
   n = 0;
    
 }  
   
 
 if(n==100)
 {
   n=0;
 }
   
 d1=n%10; // This will divide n by 10 and the remainder will be stored in d1
 d2=n/10; // This will divide n by 10 and the value before decimal will be stored in d2
 disp1(d1);
 disp2(d2);
 delay(100);//100 ms delay to debounce
}

void disp1(int num){
  if(num == 0)//0000
  {
    digitalWrite(a1, LOW); 
    digitalWrite(a2, LOW);
    digitalWrite(a3, LOW);
    digitalWrite(a4, LOW);
  }
  if(num == 1)//0001
  {
    digitalWrite(a1, HIGH);
    digitalWrite(a2, LOW);
    digitalWrite(a3, LOW);
    digitalWrite(a4, LOW);
  }
  if(num == 2)//0010
  {
    digitalWrite(a1, LOW);
    digitalWrite(a2, HIGH);
    digitalWrite(a3, LOW);
    digitalWrite(a4, LOW);
  }
  if(num == 3)//0011
  {
    digitalWrite(a1, HIGH);
    digitalWrite(a2, HIGH);
    digitalWrite(a3, LOW);
    digitalWrite(a4, LOW);
  }
  if(num == 4)//0100
  {
    digitalWrite(a1, LOW);
    digitalWrite(a2, LOW);
    digitalWrite(a3, HIGH);
    digitalWrite(a4, LOW);
  }
  if(num == 5)//0101
  {
    digitalWrite(a1, HIGH);
    digitalWrite(a2, LOW);
    digitalWrite(a3, HIGH);
    digitalWrite(a4, LOW);
  }
  if(num == 6)//0110
  {
    digitalWrite(a1, LOW);
    digitalWrite(a2, HIGH);
    digitalWrite(a3, HIGH);
    digitalWrite(a4, LOW);
  }
  if(num == 7)//0111
  {
    digitalWrite(a1, HIGH);
    digitalWrite(a2, HIGH);
    digitalWrite(a3, HIGH);
    digitalWrite(a4, LOW);
  }
  if(num == 8)//1000 
  {
    digitalWrite(a1, LOW);
    digitalWrite(a2, LOW);
    digitalWrite(a3, LOW);
    digitalWrite(a4, HIGH);
  }
  if(num == 9)//1001
  {
    digitalWrite(a1, HIGH);
    digitalWrite(a2, LOW);
    digitalWrite(a3, LOW);
    digitalWrite(a4, HIGH);
  }
}

void disp2(int num){
  if(num == 0)//0000
  {
    digitalWrite(b1, LOW);
    digitalWrite(b2, LOW);
    digitalWrite(b3, LOW);
    digitalWrite(b4, LOW);
  }
  if(num == 1)//1000
  {
    digitalWrite(b1, HIGH);
    digitalWrite(b2, LOW);
    digitalWrite(b3, LOW);
    digitalWrite(b4, LOW);
  }
  if(num == 2)//0100
  {
    digitalWrite(b1, LOW);
    digitalWrite(b2, HIGH);
    digitalWrite(b3, LOW);
    digitalWrite(b4, LOW);
  }
  if(num == 3)//1100
  {
    digitalWrite(b1, HIGH);
    digitalWrite(b2, HIGH);
    digitalWrite(b3, LOW);
    digitalWrite(b4, LOW);
  }
  if(num == 4)//0010
  {
    digitalWrite(b1, LOW);
    digitalWrite(b2, LOW);
    digitalWrite(b3, HIGH);
    digitalWrite(b4, LOW);
  }
  if(num == 5)//1010
  {
    digitalWrite(b1, HIGH);
    digitalWrite(b2, LOW);
    digitalWrite(b3, HIGH);
    digitalWrite(b4, LOW);
  }
  if(num == 6)//0110 
  {
    digitalWrite(b1, LOW);
    digitalWrite(b2, HIGH);
    digitalWrite(b3, HIGH);
    digitalWrite(b4, LOW);
  }
  if(num == 7)//1110 
  {
    digitalWrite(b1, HIGH);
    digitalWrite(b2, HIGH);
    digitalWrite(b3, HIGH);
    digitalWrite(b4, LOW);
  }
  if(num == 8)//0001 
  {
    digitalWrite(b1, LOW);
    digitalWrite(b2, LOW);
    digitalWrite(b3, LOW);
    digitalWrite(b4, HIGH);
  }
  if(num == 9)//1001
  {
    digitalWrite(b1, HIGH);
    digitalWrite(b2, LOW);
    digitalWrite(b3, LOW);
    digitalWrite(b4, HIGH);
  }
}

void policesiren() 
{
      tone(buzzer,1000,1000);                   
      analogWrite(rgbRed,255);                     
      delay(1000);                                      
      analogWrite(rgbRed,0);                      
      tone(buzzer,300,1000);                         
      analogWrite(rgbBlue,255);
      delay(1000);             
      analogWrite(rgbBlue,0);  
}

void alarm()  
{
      tone(buzzer,500,2000);  
  
   	  analogWrite(rgbRed,255); 
      delay(1000);             
      analogWrite(rgbRed,0);
      analogWrite(rgbGreen,255);
      delay(1000);
      analogWrite(rgbGreen,0);
      analogWrite(rgbBlue,255);                               
      delay(1000);                                           
      analogWrite(rgbBlue,0); 
      
  	  tone(buzzer,500,2000); 
  
      analogWrite(rgbRed,255); 
      delay(1000);             
      analogWrite(rgbRed,0);
      analogWrite(rgbGreen,255);
      delay(1000);
      analogWrite(rgbGreen,0);
      analogWrite(rgbBlue,255);
      delay(1000);
      analogWrite(rgbBlue,0); 
void password()
{
  Serial.println("Please answer the password.");
  delay(200);
  int password = 30;
  noTone(13);
}
}

I am trying to to stop the buzzer after I write a value on serial monitor. I added void password funtion, but there is something missing I guess.

not sure what you're trying to do

  • a button switch pin is normally configured as INPUT_PULLUP which enables the internal pullup on the pin causing the pin to normally be HIGH. the switch is connected between the pin and ground and when pressed pulls the pin LOW. the code is testing for the pin being HIGH
  • don't see policesiren() or alarm() being used
  • looks like the buzzer is enabled 8 times for 1 sec with a 1sec pause between each when the # of button pressed is 2 and some combination of n and the serial input match
  • not sure what the purpose of the "n" variable is and the purpose of disp routine

The code, as posted, will not compile. The password function definition is inside the alarm function due to a misplaced }.

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