Lesson 10: Using Buttons to Control Buzzer

 

Written by Jonathan Sim

You can find this lesson and more in the Arduino IDE (File -> Examples -> Andee). If you are unable to find them, you will need to install the Andee Library for Arduino IDE.

Works on iOS and Android
In this lesson, I'll show you how to create two buttons that will control the buzzer in different ways.

The first way will be to start/stop an annoying beep. The second way will be to start an annoying melody.

Contents

Top of the Code

#include <SPI.h>
#include <Andee.h>

// We'll create two buttons. One to summon Darth Vader
// and the other to release an annoying beep
AndeeHelper btnPlayMelody;
AndeeHelper btnBeep;

const int pinBuzzer = 3; // Buzzer is connected to Pin 3

// Each item in this array contains the frequency of each note.
int melody[] = { 440, 440, 440, 349, 523, 440, 349, 523, 440, 659, 
                659, 659, 698, 523, 415, 349, 523, 440 };
// Curious to know what this melody is? Upload the code to find out!
                
// Duration is specified in ms
int duration[] = { 500, 500, 500, 350, 150, 500, 350, 150, 1000, 
                  500, 500, 500, 350, 150, 500, 350, 150, 1000 }; 
int numNotes = 18; // Number of notes in the array

int state; // Variable to check whether the state of the toggle button

If you noticed, I mentioned in the comments above that one button will be used to summon Darth Vader.

Here's a look at the user interface that we'll be creating:

Lesson10.png


setup()

void setup()
{
  Andee.begin();  // Setup communication between Annikken Andee and Arduino
  Andee.clear();  // Clear the screen of any previous displays
  setInitialData(); // Define object types and their appearance
  
  pinMode(pinBuzzer, OUTPUT); // Set pin to output mode
  state = 0;
}

setInitialData()

void setInitialData()
{
  btnPlayMelody.setId(0);
  btnPlayMelody.setType(BUTTON_IN);
  btnPlayMelody.setLocation(0,0,FULL);
  btnPlayMelody.setTitle("Lord Vader... RISE!");
  
  btnBeep.setId(1);
  btnBeep.setType(BUTTON_IN);
  btnBeep.setLocation(1,0,FULL);
  btnBeep.setTitle("Annoy your friends");
}

loop()

void loop()
{
  // You can use buttons to start or stop a buzzer.
  // We're using a single-press button to prevent accidental
  // multiple presses.
  if( btnBeep.isPressed() )
  {
    btnBeep.ack();
    
    if(state == 0) // If ON
    {
      tone(pinBuzzer, 440); // Play buzzer
      state = 1;
      btnBeep.setTitle("Stop annoying people!");
      btnBeep.update();
    }
    else // If OFF
    {
      noTone(pinBuzzer); // stop buzzer
      state = 0;
      btnBeep.setTitle("Annoy your friends");
      btnBeep.update();      
    }
  }
  
  // Or you can use buttons to initiate a melody
  if( btnPlayMelody.isPressed() )
  {
    btnPlayMelody.ack();
    btnPlayMelody.setTitle("Playing...");
    btnPlayMelody.update();
    
    for(int i = 0; i < numNotes; i++)
    {
      tone(pinBuzzer, melody[i]); // Play note
      delay(duration[i]); // Hold for a specified duration
      noTone(pinBuzzer); // Stop buzzer
      delay(20); // Short pause in between notes so that the notes can be clearly heard.
    }
    btnPlayMelody.setTitle("Lord Vader... RISE!");
    btnPlayMelody.update();
  }
  
  btnPlayMelody.update();
  btnBeep.update();  
  delay(500); // Always leave a short delay for Bluetooth communication
}
comments powered by Disqus