Buzzer Button Sample
This application demonstrates button reading and tone playing functions of the module.
To run the example:
- 1.Open the IoT Node Buzzer Button sketch from the examples menu.
- 2.Select Turta IoT Node from Tools > Board menu.
- 3.Select your device's COM port from Tools > Port menu.
- 4.Open Serial Monitor from Tools > Serial Monitor.
- 5.Select 115.200 baud from the Serial Monitor's status bar.
- 6.Upload the code to your device.
IoT_Node_Buzzer_Button.ino
#include <Turta_BuzzerButton_Module.h>
#define LED 13
// Global variables.
bool buttonPressed = false;
bool oldButtonState = false;
// Create Buzzer Button instance.
Turta_BuzzerButton_Module bb;
void setup() {
// Initialize onboard LED.
pinMode(LED, OUTPUT);
// Initialize Buzzer Button.
bb.begin();
// Configure serial port.
Serial.begin(115200);
delay(200);
}
void loop() {
// Read button state.
buttonPressed = bb.readButton();
// On button pressed / released.
if (oldButtonState != buttonPressed) {
// Set LED state.
digitalWrite(LED, buttonPressed ? HIGH : LOW);
// Write button state to the serial monitor.
Serial.println(buttonPressed ? "Button pressed." : "Button released.");
// Play tone if button is pressed.
if (buttonPressed) {
// Buzzer tone alternative 1:
// Play 5000Hz tone in 192/255 duty cycle for 100ms.
bb.buzzerTonePeriod(5000, 192, 100);
}
// Play tone if button is released.
else {
// Buzzer tone alternative 2:
// Play 2000Hz tone in 64/255 duty cycle until stopped.
bb.buzzerTone(2000, 64);
// Delay 100ms.
delay(100);
// Stop the buzzer.
bb.buzzerStop();
}
}
// Store last button state.
oldButtonState = buttonPressed;
// Delay 10ms.
delay(10);
}
After the application is uploaded to the device, it generates different tones when the button is pressed and released. The application also writes the following output to the serial monitor:
Button pressed.
Button released.
The application runs forever until you clear it from memory.
Last modified 3yr ago