Hey everyone, I am currently trying to make a piano using my arduino, 100ohm "step up" resistors, and tac switch (buttons). I currently use this code here, and pretty much copy and paste it but change the button name and pin number for each button. The code is:
buttonState7 = digitalRead(buttonPin7);
Serial.println(buttonState7);
if (buttonState7 == HIGH) {
// turn LED on:
tone(piezoPin, 440);
But every time I add a copy of that code with the names and pins changed for the next button, the buzzer seems to be bogged down, and the note is very distorted.
I have gone ahead and found an 8 ohm, 0.3 Watt speaker, but am not sure how to hook it up (like do I need capacitors, transformers, diodes, etc). If the problem is the buzzer and I should use the speaker instead, how should I go about this? It's a p57n02-1
You can drive the 8 ohm speaker from an output pin, put a 120 ohm or higher resistor in series with it so the output pin drives less than 40mA. It will be very quiet.
Better suggestion is to use a powered computer speaker, with a 1uF or 10uF cap between the pin and the speaker input. You may also need a resistor divider to bring the 0-5V output down to 0-2V or so.
Computer speakers, look up on e-bay. May need to buy a USB power adapter for them as well. I have a few sets of older desktop speakers, the kind that used a wall adapter for power (Dell and Gateway 2000, that's how old they are!)
100 ohms is too low for pullup resistors. If you're using a 5V Arduino, that would try to draw 50mA when the button is pushed. That is higher than the specified maximum of 40mA per pin. You should either use the builtin pullups, or use maybe 10kohm resistors.
Thank you @CrossRoads for the information regarding the computer speakers, I may go ahead and try those as I would prefer a higher tone. I'm looking for something along the lines of a phone on full volume.
@el_supremo I will try to use it with 10K ohm resistors and see what happens, but usually the speaker gets bogged down only when I add the extra line of code, regardless of what I have hooked up. I will still try anyways as I'm sure I'm missing something.
Please read the first post in any forum entitled how to use this forum. http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.
Post the code you are having problems with.
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
"100ohm "step up" resistors, and tac switch (buttons)."
I would ditch those. Use the internal pullup resistors, and wire the tactile switch to connect the pin to Gnd when pressed
pinMode (buttonX, INPUT_PULLUP);
if (digitalRead (buttonX) == LOW){
//button was pressed, play tone
...
}
As you add more buttons, you may see a lot more 'lag' as all the if (digitalReads() == LOW) get executed.
You may want to group them, with diodes into an interrupt pin (cathodes to each input pin, common anodes to the interrupt pin with its pullup resistor enabled), when you get a LOW interrupt then scan that group of pins and see which was pressed.
I have tried your method and perhaps I am doing something wrong but now the buzzer continues to buzz at the frequency of the second tone, even though neither button is being pressed. I have pasted my entire code just incase it is the problem.
int piezoPin = 8;
const int buttonPin7 = 7;
int buttonState7 = 0;
const int buttonPin2 = 2;
int buttonState2 = 0;
void setup() {
Serial.begin(9600);
pinMode(piezoPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin7, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
}//close setup
void loop() {
buttonState7 = digitalRead(buttonPin7);
buttonState2 = digitalRead(buttonPin2);
Serial.println(buttonState7);
if (buttonState7 == HIGH) {
// turn LED on:
tone(piezoPin, 440);
}
if (buttonState2 == HIGH) {
// turn LED on:
tone(piezoPin, 261.63);
}
else if (buttonState7 == LOW) {
tone(piezoPin, 0);
}
else if (buttonState2 == LOW) {
tone(piezoPin, 0);
}
}
But I do admit at least now it's not all sounding bogged down, and is at least sounding like a tone. Thank you again for taking the time to review my code and offer help of your own time.
Your logic may need work also.
If button 7 is active, tone will start.
If button 2 is not active the, tone will then stop.
Not sure that's the logic you want.
I have taken your suggestion and it still continued to buzz at a constant tone until I set one to HIGH and one to LOW as such:
int piezoPin = 8;
const int buttonPin7 = 7;
int buttonState7 = 0;
const int buttonPin2 = 2;
int buttonState2 = 0;
void setup() {
Serial.begin(9600);
pinMode(piezoPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin7, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
}//close setup
void loop() {
buttonState7 = digitalRead(buttonPin7);
buttonState2 = digitalRead(buttonPin2);
Serial.println(buttonState7);
if (buttonState7 == HIGH) {
// turn LED on:
tone(piezoPin, 440);
}
if (buttonState2 == LOW) {
// turn LED on:
tone(piezoPin, 260);
}
else {
tone(piezoPin, 0, 880);
}
}
However now, when the first button at A7 is pressed, the tone comes out very distorted, gravely, and low even though I know it is the correct tone. Is this when I need another piece of circuitry like a capacitor or something? This part I am not too learned about. The ton for the second button, A2, sounds fine however.
Thank you @wvmarle This has most definitely cleaned up my code, unfortunately whenever I press the first button, button A7, it sounds weaker than the second button A2, and not to mention when I change the tone of A7, the actual tone played doesn't change. When I change the tone for A2, the tone actually changes. Might you have any ideas why this is happening, and what I can do to fix it? If not that's okay, I appreciate your help.
Looking back at your original code you check for button7 HIGH and button2 LOW. I missed that the first time and quite odd, usually all buttons work the same way.
The code I just typed tests for both buttons active HIGH, so that's where you may have to make changes. Actually it's most common to wire buttons active LOW and indeed you do have your pins set to INPUT_PULLUP which means they should be active LOW.
So correct code should be:
if (buttonState7 == LOW) {
tone (piezoPin, 440);
}
else if (buttonState2 == LOW) {
tone (piezoPin, 262);
else {
noTone(piezoPin);
}
But that presumes your buttons are wired correctly of course
Thank you @wvmarvle I have changed the code as you suggested so that the button works when pushed on LOW, I did have it wired incorrectly.
But it has not changed the effect that pushing the button has on my buzzer. When I push the button, the buzzer plays a tone that does not match the tone I have programmed, and the tone is very low and gravely. This is for the first Button, on A7. The second buttonfor A2 works well and changes tone when the tone I’ve programmed changes.
It's probably about time for you to repost your code because it's obviously changed a lot.
Also what exactly is the buzzer you're using? One way to get the result you're describing is to try to drive an ACTIVE buzzer (which generates its own tone) with the tone() command.