I have two buttons one of the buttons activates a speaker to play sound, I want the other button to deactivate the speaker, but my current code is doing that. Please help.
const int button1Pin = 2; // pushbutton 1 pin
const int button2Pin = 3; // pushbutton 2 pin
const int ledPin = 13; // LED pin
const int buzzerPin = 9;
const int songLength = 5;
char notes[] = "cdefg"; // a space represents a rest
int beats[] = {1, 1, 1, 1, 1};
// The tempo is how fast to play the song.
// To make the song play faster, decrease this value.
int tempo = 113;
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
char array1[] = "Alarm Activated "; //the string to print on the LCD
char array2[] = "Alarm Deactivated "; //the string to print on the LCD
int tim = 250; //the value of delay time
// initialize the library with the numbers of the interface pins
LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display
void setup()
{
pinMode(button1Pin, INPUT);
pinMode(button2Pin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
lcd.init(); //initialize the lcd
lcd.backlight(); //open the backlight
}
void loop()
{
int button1State, button2State; // variables to hold the pushbutton states
int i, duration;
button1State = digitalRead(button1Pin);
button2State = digitalRead(button2Pin);
if (button1State == LOW)
{
digitalWrite(ledPin, HIGH); // turn the LED on
lcd.setCursor(15, 0); // set the cursor to column 15, line 0
for (int positionCounter1 = 0; positionCounter1 < 26; positionCounter1++)
{
lcd.scrollDisplayLeft(); //Scrolls the contents of the display one space to the left.
lcd.print(array1[positionCounter1]); // Print a message to the LCD. //displays "Alarm Activated" on lcd
delay(tim); //wait for 250 microseconds
}
lcd.clear(); //Clears the LCD screen and positions the cursor in the upper-left corner.
while (button2State == HIGH)
{
for (i = 0; i < songLength; i++) // step through the song arrays
{
duration = beats * tempo; // length of note/rest in ms //loops buzzer sound forever
_ if (notes == ' ') // is this a rest?_
* {*
* delay(duration); // then pause for a moment*
* }*
* else // otherwise, play the note*
* {*
_ tone(buzzerPin, frequency(notes*), duration);
delay(duration); // wait for tone to finish*
* }
delay(tempo / 10); // brief pause between notes*
* }
}
noTone(buzzerPin); //turns the buzzer off*
* delay(duration);
digitalWrite(ledPin, LOW); // turn the LED off*
* lcd.setCursor(15, 1); // set the cursor to column 15, line 1*
* for (int positionCounter = 0; positionCounter < 26; positionCounter++)
{
lcd.scrollDisplayLeft(); //Scrolls the contents of the display one space to the left. //displays "Alarm Deactivated" on lcd*
* lcd.print(array2[positionCounter]); // Print a message to the LCD.
delay(tim); //wait for 250 microseconds*
* }
lcd.clear(); //Clears the LCD screen and positions the cursor in the upper-left corner.
}
else*
* {
digitalWrite(ledPin, LOW); // turn the LED off*
* noTone(buzzerPin); //turns the buzzer off*
* }
}
int frequency(char note)
{
// This function takes a note character (a-g), and returns the*
* // corresponding frequency in Hz for the tone() function.
int i;
const int numNotes = 8; // number of notes we're storing*
* // The following arrays hold the note characters and their*
* // corresponding frequencies. The last "C" note is uppercase*
* // to separate it from the first lowercase "c". If you want to*
* // add more notes, you'll need to use unique characters.
// For the "char" (character) type, we put single characters*
* // in single quotes.
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int frequencies[] = {262, 294, 330, 349, 392, 440, 494, 523};
// Now we'll search through the letters in the array, and if*
* // we find it, we'll return the frequency for that note.
for (i = 0; i < numNotes; i++) // Step through the notes*
* {
if (names == note) // Is this the one?
{
return (frequencies); // Yes! Return the frequency*
* }
}
return (0); // We looked through everything and didn't find it,
// but we still need to return a value, so return 0.
}*_