Offline
Newbie
Karma: 0
Posts: 9
|
 |
« on: December 04, 2012, 09:07:45 am » |
3 tact switch, 1st pin connected to 23, 24 and 25 while 2nd pin is connected to ground. 8 ohm speaker, negative in ground while positive pin is connected to pin 8. all components are operational and fine. i just have problems with the code and i dont know where i got wrong,.. 
#include "pitches.h"
byte button[] = {23, 24, 25}; //create array of buttons connected at pin 23, 24 and 25 byte speakerpin = 8; //sets speaker pin to pin 8 byte buttonset ; //variable for reading button push
void setup() { // set all pins to output pinMode(speakerpin, OUTPUT); for(int x=23; x<26; x++) { pinMode(button[x] , INPUT); digitalWrite(button[x] , HIGH); // buttons are in the high position } }
void loop(){ for(int x=23; x<26; x++){ buttonset = digitalRead(button[x]); if(buttonset == LOW){ if(x == 23){ // if button on pin 2 is pressed // if (buttonset == LOW){ tone(8, NOTE_C4); //stores the note's wavelength to be played. // } } if(x == 24){ // if (buttonset == LOW){ tone(8, NOTE_D4); // } } if(x == 25){ // if (buttonset == LOW){ tone(8, NOTE_E4); // } } else noTone(8); } } }
[/size]
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 311
Posts: 35478
Seattle, WA USA
|
 |
« Reply #1 on: December 04, 2012, 09:14:48 am » |
for(int x=23; x<26; x++) { pinMode(button[x] , INPUT); You are referencing the 23rd, 24th, and 25th elements of an array that has 3 elements. Not a good thing.
|
|
|
|
|
Logged
|
|
|
|
|
Gosport, UK
Offline
Faraday Member
Karma: 19
Posts: 3118
|
 |
« Reply #2 on: December 04, 2012, 10:04:33 am » |
You risk damaging your Arduino connecting an 8 Ohm speaker to it without a series resistor.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 9
|
 |
« Reply #3 on: December 04, 2012, 10:16:00 am » |
You are referencing the 23rd, 24th, and 25th elements of an array that has 3 elements. Not a good thing. kind sir, can you suggest anything to correct this?
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 311
Posts: 35478
Seattle, WA USA
|
 |
« Reply #4 on: December 04, 2012, 10:27:35 am » |
can you suggest anything to correct this? Sure. Use the correct value in the for loop. You either want to set the pin whose number is x or the pin whose number is in the x position in the array. For the former, loose the array. For the latter, the proper range is x=0 to x<3.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 9
|
 |
« Reply #5 on: December 04, 2012, 10:29:36 am » |
You risk damaging your Arduino connecting an 8 Ohm speaker to it without a series resistor. thank you for the caution sir, i'm kind of new with arduino, my bad. anyway, my supply is just 5 volts, what resistor value should i use?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 9
|
 |
« Reply #6 on: December 04, 2012, 10:41:25 am » |
sir PaulS, i have my ckt up and running now, thanks for the advice. this proves my knowledge in C is still lacking, better study more. 
|
|
|
|
|
Logged
|
|
|
|
|
Left Coast, CA (USA)
Offline
Brattain Member
Karma: 279
Posts: 15314
Measurement changes behavior
|
 |
« Reply #7 on: December 04, 2012, 10:42:09 am » |
You risk damaging your Arduino connecting an 8 Ohm speaker to it without a series resistor. thank you for the caution sir, i'm kind of new with arduino, my bad. anyway, my supply is just 5 volts, what resistor value should i use? At least a 120 ohms or more to stay below the 40ma absolute maximum current draw limit from an output pin. But keep in mind that means the resistor will 'consume' about 95% of the 'audio power' leaving a wimpy amount of about 13 milliwatts of audio power to drive the the 8 ohm speaker. You should look for a speaker with a much higher voice coil resistance or do as most anyone wanting to drive a 8 ohm speaker, use a audio amplifier stage or module to drive the speaker. Lefty
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 9
|
 |
« Reply #8 on: December 04, 2012, 09:51:58 pm » |
At least a 120 ohms or more to stay below the 40ma absolute maximum current draw limit from an output pin. But keep in mind that means the resistor will 'consume' about 95% of the 'audio power' leaving a wimpy amount of about 13 milliwatts of audio power to drive the the 8 ohm speaker. You should look for a speaker with a much higher voice coil resistance or do as most anyone wanting to drive a 8 ohm speaker, use a audio amplifier stage or module to drive the speaker. thanks for the heads up sir,.. i will research and study about audio amplifier that you mentioned,..
|
|
|
|
|
Logged
|
|
|
|
|
|