loading

This tutorial will show you have to use the DIY Gamer Kit's Piezo Buzzer in your programs. The Buzzer is what we use to make all sorts of bleeps and squeaks, using clever functions already created in the Gamer Library!

The program we will make in this tutorial will be a simple 'press and play' instrument, using the 4 buttons as controls for different notes.

Step 1:

To start your program, we need to make sure our Gamer library is installed on your Arduino software.

We can check this by importing it into our sketch. Open a new Arduino sketch and go to

Sketch -> Import Library -> Gamer. If this does not show up, follow instructions here on how to import the library.

Once you have done this, let's create the basic structure for our code, by adding the void setup and void loop functions. These are needed in every Arduino sketch and are where all our code will be written.

#include <gamer.h>

Gamer gamer;

void setup(){
   gamer.begin();
   }
 
 void loop(){        
   }

Step 2: Create Your Images

To create images for each button event, we need to create 4 byte arrays. Each one will be assigned to each button so we can see the button is pressed.

These images are arrows for each direction, you can make your own images if you wish, aslong as these are placed inbetween <#include Gamer.h> and Gamer gamer;

// Images displayed when the buttons are pressed.
byte up[8] = {
  B00011000,
  B00111100,
  B01111110,
  B11111111,
  B00011000,
  B00011000,
  B00011000,
  B00011000};

byte right[8] = {
  B00001000,
  B00001100,
  B00001110,
  B11111111,
  B11111111,
  B00001110,
  B00001100,
  B00001000};

byte down[8] = {
  B00011000,
  B00011000,
  B00011000,
  B00011000,
  B11111111,
  B01111110,
  B00111100,
  B00011000};

byte left[8] = {
  B00010000,
  B00110000,
  B01110000,
  B11111111,
  B11111111,
  B01110000,
  B00110000,
  B00010000};

Step 3: Control the Images

To animate these images, we need to assign them to button events. We can do this using the gamer.isHeld() function. We can refer to each button on the Gamer as a direction.

  if(gamer.isHeld(UP)) {
    gamer.printImage(up);
    gamer.playTone(200);
  }
  else if(gamer.isHeld(RIGHT)) {
    gamer.printImage(right);
    gamer.playTone(220);
  }
  else if(gamer.isHeld(DOWN)) {
    gamer.printImage(down);
    gamer.playTone(240);
  }
  else if(gamer.isHeld(LEFT)) {
    gamer.printImage(left);
    gamer.playTone(180);
  }
  // Otherwise, silence!
  else gamer.stopTone();

This should be posted in for loop section.

The code essentially checks if any of the buttons have been pressed. If so, it triggers an image on the 8x8 matrix and a note with gamer.playTone(note) . A value specified between 10 - 240 will play a different tone.

to stop the noise when nothing is pressed, we call gamer.stopTone() . This stops the note from being played.

Step 4: Full Code

Here is the full code:

/*
This example shows you how to play tones on the
Gamer's buzzer. Press one of the buttons, and you'll
hear a sound. 
*/

// Include Gamer library.
#include <Gamer.h>

// Images displayed when the buttons are pressed.
byte up[8] = {
  B00011000,
  B00111100,
  B01111110,
  B11111111,
  B00011000,
  B00011000,
  B00011000,
  B00011000};

byte right[8] = {
  B00001000,
  B00001100,
  B00001110,
  B11111111,
  B11111111,
  B00001110,
  B00001100,
  B00001000};

byte down[8] = {
  B00011000,
  B00011000,
  B00011000,
  B00011000,
  B11111111,
  B01111110,
  B00111100,
  B00011000};

byte left[8] = {
  B00010000,
  B00110000,
  B01110000,
  B11111111,
  B11111111,
  B01110000,
  B00110000,
  B00010000};

// Create a copy of the Gamer library.
Gamer gamer;

void setup() {
  // Start up Gamer.
  gamer.begin();
}

void loop() {
  /* 
  Depending on the button that's held down,
  play a note!
  */
  if(gamer.isHeld(UP)) {
    gamer.printImage(up);
    gamer.playTone(200);
  }
  else if(gamer.isHeld(RIGHT)) {
    gamer.printImage(right);
    gamer.playTone(220);
  }
  else if(gamer.isHeld(DOWN)) {
    gamer.printImage(down);
    gamer.playTone(240);
  }
  else if(gamer.isHeld(LEFT)) {
    gamer.printImage(left);
    gamer.playTone(180);
  }
  // Otherwise, silence!
  else gamer.stopTone();
}


It can also be found in your examples folder at Examples -> Gamer -> BuzzerExample

About This Instructable

338views

7favorites

License:

Bio: Technology Will Save Us is a haberdashery for making technology in everyday life. We design, manufacture and sell DIY technology Kits and run workshops to ... More »
More by techwillsaveus:DIY Speaker Pieizo Plate Reverb DIY Speaker Talk Box DIY Speaker Kit Spring Reverb 
Add instructable to: