#include "pitches.h"byte button[] = {23, 24, 25}; //create array of buttons connected at pin 23, 24 and 25byte speakerpin = 8; //sets speaker pin to pin 8byte buttonset ; //variable for reading button pushvoid 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); } } }
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.
can you suggest anything to correct this?
You risk damaging your Arduino connecting an 8 Ohm speaker to it without a series resistor.
Code: [Select]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.