/*
LAB07 Part 2
Demonstrate External Interrupt, Audio:
Use the NewTone Library.
READ the syntax to learn how to use the NewTone() command.
Hookup an LED to a digital pin (don’t forget the resistor).
Hookup a passive piezo speaker (the one with NO white label) to a digital pin.
Hook up a pushbutton to an interrupt pin.
In the loop() function, blink the LED twice per second using the delay() function.
Using an interrupt, in the ISR make a 1kHz tone for two seconds (using the NewTone library)
whenever the pushbutton is pushed. The LED should keep blinking (which is the whole idea of using an interrupt)!
What you are learning from this:
Everything from #1 and how to hookup a pin to make an external interrupt (your pushbutton).
2. Use the NewTone library from here: https://bitbucket.org/teckel12/arduino-new-tone/wiki/Home
NewTone( pin, frequency [, length ] ) - Play a note on pin at frequency in Hz.
pin - Pin speaker is wired to (other wire to ground, be sure to add an inline 100 ohm resistor).
frequency - Play the specified frequency indefinitely, turn off with noNewTone().
length - [optional] Set the length to play in milliseconds. (default: 0 [forever], range: 0 to 2^32-1)
noNewTone(pin) - Stop playing note (pin is optional, will always stop playing on pin that was last used).
*/
#include <NewTone.h>
//constants to hold LED and BUZZER PINS
const byte LED_Pin = 13;
const byte BUZZER_PIN = 9;
int LED_delayDT = 500; //msec
// global variables to hold the LED and buzzer states
bool LED_State = LOW;
const byte DEBOUNCE_DELAY = 100;
volatile unsigned long lastPress = LOW;
volatile bool buzzer_State = LOW;
const byte PUSHBUTTON_PIN = 2; // INT0 interrupt pin on Uno
const byte PRESSED = LOW;
void setup() {
//Set the LED pin as an OUTPUT
pinMode(LED_Pin, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(PUSHBUTTON_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(PUSHBUTTON_PIN), PushButton, LOW);
//Set the baud rate in bits per second
Serial.begin(9600);
}
void loop() {
delay(LED_delayDT); // this delay seems more like 5 seconds in Wokwi
blinkLED();
delay(LED_delayDT);
blinkLED();
// Serial.print("Buzzer State = ");
// Serial.println(buzzer_State);
}
// INTERRUPT SERVICE ROUTINE BuzzBuzzer at 10khz
void BuzzBuzzer(void) {
tone(BUZZER_PIN, 1000, 2000); // 1kHz for 2 seconds
}
//function call using delay to blink LED. this just flips state and digitalWrite
void blinkLED() {
digitalWrite(LED_Pin, LED_State);
LED_State = !LED_State;
}
// INTERRUPT SERVICE ROUTINE
void PushButton () {
unsigned long milliSecond = millis();
if (((milliSecond - lastPress) > DEBOUNCE_DELAY) && (digitalRead(PUSHBUTTON_PIN) == PRESSED)) {
//buzzer_State = !buzzer_State; // Toggle buzzer_State
//if (buzzer_State) {
BuzzBuzzer(); // if buzzer_State is set by pressed
//}
}
lastPress = milliSecond;
}
//NOTE: the value returned by millis() will not increment!
//It’s frozen when it enters the ISR.
//But, since we are only reading it once, that’s the time we want
//- the time when it entered the interrupt.