Hi folks ! I've been making a kid of piano to control music, but with my code, arduino does not stop sending information when the button is pressed, and even when it's not pressed, it sends information... The result is that hen I hit a button, the note is triggered a lot of times making an horrible sound... In this code I need to send information via the monitor to retrieve the info in my musical app, like I show below. Does anyone has an idea to make that even if I press a long time the button, it only sends one 1 and not many, and if the button is low only one 0 is sent ??? So many thanks for your answers !!! Actually I've made somthing that look like this :
int button1 = 1;
int button2 = 2;
int button3 = 3;
int button4 = 4;
int button5 = 5;
int button6 = 6;
int button7 = 7;
int button8 = 8;
int button9 = 9;
int button10 = 10;
int button11 = 11;
int button12 = 12;
int buttonstate1 = 0;
int buttonstate2 = 0;
int buttonstate3 = 0;
int buttonstate4 = 0;
int buttonstate5 = 0;
int buttonstate6 = 0;
int buttonstate7 = 0;
int buttonstate8 = 0;
int buttonstate9 = 0;
int buttonstate10 = 0;
int buttonstate11 = 0;
int buttonstate12 = 0;
void setup() {
Serial.begin(9600);
pinMode (button1,INPUT);
pinMode (button2,INPUT);
pinMode (button3,INPUT);
pinMode (button4,INPUT);
pinMode (button5,INPUT);
pinMode (button6,INPUT);
pinMode (button7,INPUT);
pinMode (button8,INPUT);
pinMode (button9,INPUT);
pinMode (button10,INPUT);
pinMode (button11,INPUT);
pinMode (button12,INPUT);
}
void loop() {
int buttonstate1 = digitalRead( button1);
int buttonstate2 = digitalRead( button2);
int buttonstate3 = digitalRead( button3);
int buttonstate4 = digitalRead( button4);
int buttonstate5 = digitalRead( button5);
int buttonstate6 = digitalRead( button6);
int buttonstate7 = digitalRead( button7);
int buttonstate8 = digitalRead( button8);
int buttonstate9 = digitalRead( button9);
int buttonstate10 = digitalRead( button10);
int buttonstate11 = digitalRead( button11);
int buttonstate12 = digitalRead( button12);
Serial.print(buttonstate1);
Serial.print("\t");
Serial.print(buttonstate2);
Serial.print("\t");
Serial.print(buttonstate3);
Serial.print("\t");
Serial.print(buttonstate4);
Serial.print("\t");
Serial.print(buttonstate5);
Serial.print("\t");
Serial.print(buttonstate6);
Serial.print("\t");
Serial.print(buttonstate7);
Serial.print("\t");
Serial.print(buttonstate8);
Serial.print("\t");
Serial.print(buttonstate9);
Serial.print("\t");
Serial.print(buttonstate10);
Serial.print("\t");
Serial.print(buttonstate11);
Serial.print("\t");
Serial.println(buttonstate12);
delay(150);
}
Sound like you have some floating inputs. Do you have pull down or pull up resistors on the button inputs?
The more accepted way to wire switches is to wire one side to ground and the other to an input set to pinMode INPUT_PULLUP.
Often it is desirable to sense when a switch becomes pressed instead of is pressed. That is to sense the transition from off to on. The state change detection method is how to do that.
Please post a schematic. Written descriptions are always more ambiguous than a drawing. Hand drawn, photographed and posted is fine. Include all pin names/numbers, components, their part numbers and/or values and power supplies.
What Arduino board are you using? Pin 1 on many Arduino compatible boards is the TX pin for hardware serial and cannot be used for anything else if Serial is used.
Thanks Alto ! seems to be exactly what I need... but how can I manage that with several buttons ? could it be something like following ? Sorry if I'm wrong I'm a real newbie !!!