Arduino Sound Effect With Game Handle

by NinoZhang in Circuits > Arduino

1813 Views, 4 Favorites, 0 Comments

Arduino Sound Effect With Game Handle

CU Boulder | ATLS object | Project 1 | Nino

This project is for the class ATLS 3300 at CU Boulder.

Push buttons can make buzzers sound.

Five buttons represented the tone C, D, E, F, G.

There are two modes.

The switch determines the mode and LEDs can indicate which mode we are in.

In mode-1, the game handle can determine which buzzer sound.

In mode-2, the game handle can change the frequency of the sound.

Material:

wires

Button * 5

LED * 2

Switch * 1

Buzzer (low-level trigger) * 4

Resistor * 8

Arduino Uno * 1

Step 1: Schematic

未命名作品 2.jpg

Hardware 1:

IMG_0595.jpg
IMG_0597.jpg

Connect the ground and 5v on the breadboard to the Arduino.

Hardware 2:

IMG_0599.jpg

Add 5 buttons to the board.

Connect Them to the 5v power and ground them with resistors.

Parallel them with digital input on the Arduino (using the port without PWM).

I'm using the port 2,4, 7, 12, 13 for the button 1,2,3,4,5.

Hardware 3:

IMG_0600.jpg
IMG_0602.jpg

Connect buzzer to the board.

VCC to v5, GND to ground, and I/O to the PWM output on the Arduino.

I'm using 3~, 5~, 6~, 9~ on the Arduino for the buzzer 1,2,3,4.

Hardware 4:

IMG_0603.jpg

Add switch to the breadboard.

Power the middle needle with 5v and ground the left needle with a resistor.

Parallel the resistor with input on Arduino.

I'm using port 8 on the Arduino for the switch.

Hardware 5:

IMG_0604.jpg

Add two LEDs to the breadboard.

Ground them with a resistor.

Connect them with outputs on the Arduino.

I'm using ports 10 and 11 for LEDs 1 and 2.

Hardware 6:

IMG_0605.jpg
IMG_0606.jpg

Connect GND, + 5V, VRx, and VRy to the ground, 5v, A0, and A1 on the Arduino.

Step 7: Add Enclosure

20210521-IMGL8755.jpg
20210521-IMGL8756.jpg

Using cardboard making an enclosure for the project.

Code 1:

Screen Shot 2021-05-16 at 5.55.17 PM.png
Screen Shot 2021-05-16 at 6.00.27 PM.png

Test the button.

If everything looks good, change the testing code to the reading code.

Code 2:

Screen Shot 2021-05-16 at 6.03.51 PM.png
Screen Shot 2021-05-16 at 6.10.26 PM.png

Test the LEDs.

If the LEDs can light up separately, link the Leads to the mode indicator.

Code 3:

Screen Shot 2021-05-16 at 6.14.14 PM.png
Screen Shot 2021-05-16 at 6.14.18 PM.png
Screen Shot 2021-05-16 at 6.14.24 PM.png

Link the switch output to the mode.

If it works well and LEDs can change with the switch, nothing needs to change.

Code 4:

Screen Shot 2021-05-16 at 6.18.07 PM.png
Screen Shot 2021-05-16 at 6.18.11 PM.png
Screen Shot 2021-05-16 at 6.20.25 PM.png
Screen Shot 2021-05-16 at 6.20.49 PM.png
Screen Shot 2021-05-16 at 6.21.31 PM.png

Test the game handle.

The Serial should change while you are moving the handle.

If everything works well, delete the debug code and leave only the analogRead code.

Code 5:

Screen Shot 2021-05-16 at 7.01.56 PM.png
Screen Shot 2021-05-16 at 7.01.52 PM.png
Screen Shot 2021-05-16 at 7.01.40 PM.png

Test and see whether the busser works.

When pressing the button 1 to 4, the corresponding buzzer should make noise.

Code 6:

Screen Shot 2021-05-16 at 7.30.26 PM.png
Screen Shot 2021-05-16 at 7.30.23 PM.png
Screen Shot 2021-05-16 at 7.30.18 PM.png

Test and see whether the buzzer can make a sound in specific Hz.

If it works, delete the debug code.

Code 7:

Screen Shot 2021-05-16 at 8.31.23 PM.png
Screen Shot 2021-05-16 at 8.31.39 PM.png
Screen Shot 2021-05-16 at 8.31.56 PM.png
Screen Shot 2021-05-16 at 8.32.02 PM.png
Screen Shot 2021-05-16 at 8.32.07 PM.png
Screen Shot 2021-05-16 at 8.32.12 PM.png

Copy and paste everything in loop(), into a helper function called start().

Add the code in start() to define Hz from the button.

Add two helper functions called mode_1() and mode_2().

Relate buzzer input with game handle in mode_1().

Relate Hz to the game handle in mode_2().

Add all the helper functions to the loop().

Step 16: Final Code

Change the input of the buzzer to A5 and cut the power supply when the buzzer needs to quiet.

Final code:

int b[] = {2, 4, 7, 12, 13};

int b_rd[] = {0,0,0,0,0};

int s_rd = 0; int led [] = {10,11};

int s = 8; int hdx = A0; int hdy = A1;

int hdx_rd = 0; int hdy_rd = 0;

int buzzer[] = {3,5,6,9}; int hz = 0;

int hz_array[] = {262,294,330,349,392}; int pw = A5;

int mode = 1;

//int debug = 0;

void setup() {

Serial.begin(9600);

for (int i = 0; i<5; i=i+1){

pinMode(b[i],INPUT);

if(i<2){

pinMode(led[i],OUTPUT);

}

if(i<4){

pinMode(buzzer[i],OUTPUT);

}

}

pinMode(s, INPUT);

pinMode(hdx,INPUT);

pinMode(hdy, INPUT);

pinMode(pw, OUTPUT);

}

void start(){

// readings

for (int i =0; i<5; i=i+1){

b_rd[i]=digitalRead(b[i]);

if(i<2){

s_rd = digitalRead(s);

}

}

//switch reading

if(s_rd == 1){

mode = 1;

}else{

mode = 2;

}

// LED display

if(mode == 1){

digitalWrite(led[0], HIGH);

digitalWrite(led[1], LOW);

} else {

digitalWrite(led[0], LOW);

digitalWrite(led[1], HIGH);

}

//game handle reading

hdx_rd = analogRead(hdx);

hdy_rd = analogRead(hdy);

// button to hz

hz = 0;

for(int i=0;i<5;i=i+1){

if(b_rd[i] == 1){

hz = hz_array[i];

}

}

}

void mode_1(){

if(hz != 0){

bool speak[] = {false,false,false,false};

if( (hdx_rd < 100) && (hdy_rd > 900)){

speak[0] = true;

}

if( (hdx_rd > 900) && (hdy_rd > 900)){

speak[1] = true;

}

if( (hdx_rd > 900) && (hdy_rd < 100)){

speak[2] = true;

}

if( (hdx_rd < 100) && (hdy_rd < 100)){

speak[3] = true;

}

for( int i = 0; i<4; i=i+1){

if(speak[i] == true){

digitalWrite(pw,HIGH);

tone(buzzer[i], hz);

}else{

digitalWrite(pw,LOW);

noTone(buzzer[i]);

analogWrite(buzzer[i],255);

}

}

}else{

for( int i = 0; i<4; i=i+1){ digitalWrite(pw,LOW); noTone(buzzer[i]); analogWrite(buzzer[i],255);

}

}

}

void mode_2(){

if(hz != 0){

digitalWrite(pw,HIGH);

int gap = hdy_rd;

hz = hz - map(hdx_rd, 0, 1023, -15,15);

hz = hz - map(hdy_rd, 0, 1023, -15,15);

tone(buzzer[1],hz);

}else{

for( int i = 0; i<4; i=i+1){

digitalWrite(pw,LOW); noTone(buzzer[i]); analogWrite(buzzer[i],255);

}

}

}

void loop() {

start();

if(mode == 1){

mode_1();

} else{

mode_2();

}

}

Step 17: Final Result

20210521-IMGL8752.jpg