04 Arduino How? How to Add Sound Effect

Date:  May 19, 2017

In the last lesson, Jay has learned how to write a simple C program to make a ball aimlessly bouncing off the 4 walls.  It is not that hard to write that, takes only a few lines and you can do the job.

Now, in this lesson, we are back to Electronics, how do I add sound effect to my game.

It is simple actually.

First of all, in order to generate sound, you will need a buzzer.  A digital speaker.

As shown in the picture below, this is a buzzer.

IMG_0461.jpg

The buzzer has a connector.  One long and one short.  Long one is positive, and short one is negative.  So, to connect this, please the buzzer into the breadboard.  And then connect the following pin to the Arduino.

  •    long pin (positive) –> connect this to Digital input pin #8 (as in our example)
  •   Short pin (negative) –> connect this to the GND (Ground).

IMG_0462.jpg

That’s it!  Let’s look at the software programming.

I found a good header file for all the musical notes made by machine by sounding the tone.  You can use this for future.  It is inside “pitches.h” header files.

You can actually include a local header file such as pitches.h file.  click on the small downward arrow on the top right hand side to reveal the tab menu.

Select “New Tab”.

Screen Shot 2017-05-19 at 10.35.09 PM.png

Then, go down to the yellow status bar to key in the file name as “pitches.h” and press OK.

Screen Shot 2017-05-19 at 10.35.17 PM.png

Then, you cut and paste the following into this “pitches.h” tab.  This is the sound for every musical notes, the equivalent number for that tone.

/*************************************************
 * Public Constants
 *************************************************/

#define NOTE_B0 31
#define NOTE_C1 33
#define NOTE_CS1 35
#define NOTE_D1 37
#define NOTE_DS1 39
#define NOTE_E1 41
#define NOTE_F1 44
#define NOTE_FS1 46
#define NOTE_G1 49
#define NOTE_GS1 52
#define NOTE_A1 55
#define NOTE_AS1 58
#define NOTE_B1 62
#define NOTE_C2 65
#define NOTE_CS2 69
#define NOTE_D2 73
#define NOTE_DS2 78
#define NOTE_E2 82
#define NOTE_F2 87
#define NOTE_FS2 93
#define NOTE_G2 98
#define NOTE_GS2 104
#define NOTE_A2 110
#define NOTE_AS2 117
#define NOTE_B2 123
#define NOTE_C3 131
#define NOTE_CS3 139
#define NOTE_D3 147
#define NOTE_DS3 156
#define NOTE_E3 165
#define NOTE_F3 175
#define NOTE_FS3 185
#define NOTE_G3 196
#define NOTE_GS3 208
#define NOTE_A3 220
#define NOTE_AS3 233
#define NOTE_B3 247
#define NOTE_C4 262
#define NOTE_CS4 277
#define NOTE_D4 294
#define NOTE_DS4 311
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_FS4 370
#define NOTE_G4 392
#define NOTE_GS4 415
#define NOTE_A4 440
#define NOTE_AS4 466
#define NOTE_B4 494
#define NOTE_C5 523
#define NOTE_CS5 554
#define NOTE_D5 587
#define NOTE_DS5 622
#define NOTE_E5 659
#define NOTE_F5 698
#define NOTE_FS5 740
#define NOTE_G5 784
#define NOTE_GS5 831
#define NOTE_A5 880
#define NOTE_AS5 932
#define NOTE_B5 988
#define NOTE_C6 1047
#define NOTE_CS6 1109
#define NOTE_D6 1175
#define NOTE_DS6 1245
#define NOTE_E6 1319
#define NOTE_F6 1397
#define NOTE_FS6 1480
#define NOTE_G6 1568
#define NOTE_GS6 1661
#define NOTE_A6 1760
#define NOTE_AS6 1865
#define NOTE_B6 1976
#define NOTE_C7 2093
#define NOTE_CS7 2217
#define NOTE_D7 2349
#define NOTE_DS7 2489
#define NOTE_E7 2637
#define NOTE_F7 2794
#define NOTE_FS7 2960
#define NOTE_G7 3136
#define NOTE_GS7 3322
#define NOTE_A7 3520
#define NOTE_AS7 3729
#define NOTE_B7 3951
#define NOTE_C8 4186
#define NOTE_CS8 4435
#define NOTE_D8 4699
#define NOTE_DS8 4978

OK, in order to make use of this pitches.h definitions, you will need to include this file into your C program.  you do this by adding this following line

#include "pitches.h"

Now, why do we use double quotes here?  Because this file is not in the system library, it only exists locally, as you just created it.  So instead of the arrow brackets, you uses the double quotes to include this file.  Arduino.h and U8g2lib.h both exists in the system library, so, you simply use a <…> will do.

Screen Shot 2017-05-19 at 11.03.41 PM.png

OK, since we plug the active pin (positive) of the buzzer into digital input pin #8, so, we need to define that pin here.  Let’s call it BEEPER.

#define BEEPER 8

Screen Shot 2017-05-19 at 11.06.23 PM.png

Let’s write a new sound function, soundBounce().  What this function does is to use the Arduino system library to send a tone of “494” and play it for 50ms (milliseconds).

Screen Shot 2017-05-19 at 11.07.43 PM.png

When your pong ball hit the wall and bounce back, you call the function soundBounce(); and play the tone “494” digitally.

That’s it.

Screen Shot 2017-05-19 at 11.09.38 PM.png

And there you go.  The ball sound the tone “494”.

OK, if you cannot remember it, you look up at the “pitches.h”, you will find that this tone is actually the NOTE_B4 (which is equivalent to digit 494 tone).

Screen Shot 2017-05-19 at 11.11.45 PM.png

So, you modify your program this way.  To reference it as NOTE_B4, instead of 494.

Screen Shot 2017-05-19 at 11.12.32 PM.png

Mission accomplished.

Now, your small little game got sound.

Next lesson, we will need to find a way to redraw the wall.  Except that this time, we draw only the top, left and right wall.  We removed the bottom wall, and replace it with a paddle.  We also need a potentiometer so that you can move the paddle left and right.

Here is the complete code for this lesson.  Remember, just let them copy and paste and explain to them what each function does.

#include <Arduino.h>
#include <U8g2lib.h>
#include "pitches.h"

#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif

U8G2_SSD1306_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);

#define BALL_SIZE 4
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define BEEPER 8 // connect the beeper to digital input pin #8

#define BALL_MOVEX 4 // when you move your pong ball, how far x-axis it should move from last position
#define BALL_MOVEY 2 // when you move your ping ball, how far y-axis it should move from last position

int ballX = SCREEN_WIDTH/2; // start to "drop" the pong ball from center of x-axis
int ballY = BALL_SIZE/2+1; // start to "drop" the pong ball from top
int ballMoveX = BALL_MOVEX;
int ballMoveY = BALL_MOVEY;

void draw(void) {

 // draw playing area
 u8g2.drawFrame(0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
 
 // draw ball
 u8g2.drawDisc(ballX,ballY,BALL_SIZE/2);
 
}

void Movement(){

 // the pong ball move from last position in terms of x-axis and y-axis with these differnce
 ballX = ballX + ballMoveX;
 ballY = ballY + ballMoveY;

 // ball bouncing from top or bottom
 if (ballY >= SCREEN_HEIGHT - 1 - BALL_SIZE || ballY <=1 +BALL_SIZE/2) {
 ballMoveY *= -1; // instead of adding, reverse the polarity, start to minus
 soundBounce();
 }

 // ball bouncing from left wall or right wall
 if (ballX >= SCREEN_WIDTH - 1 - BALL_SIZE || ballX <=1+BALL_SIZE/2) {
 ballMoveX *= -1; // instead of adding, revrse the polarity, start to minus
 soundBounce();
 }
 
}

void soundBounce() 
{
 tone(BEEPER, NOTE_B4, 50);
}


void setup(void) {
 u8g2.begin(); // initialise the display

}

void loop(void) {
 Movement();
 u8g2.firstPage();
 do {
 draw();
 } while (u8g2.nextPage());
}

Yeah, got sound.

IMG_0463.jpg

 

 

 

 

 

 

Leave a Reply