Avoid button repeating numbers

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.

My state change detection for active low inputs tutorial shows how to detect the changes on a switch wired to ground and how to wire the switches.

actually my buttons are wired as following... one pin in 5 and the other is both connected to data pin and minus through a resistor

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.

general help…
Use an array to hold your buttons, and possibly a struct to hold details of each button.
The code will be a lot smaller.

To fix the repeating, try testing for when the button changes ‘state’, rather than simply ‘when’ it’s held down

here is my wiring

That is what the state change detection method will do.

I am using a teensy 3 board

image

The bottom of the resistor needs to connect to Arduino GND.

so it is... it's only not represented in the drawing... sorry

Sorry... not so used with code... could you explain it with an example please ? :wink:

FYI

Look at the example in the IDE

 File / Examples / 02.Digital / StateChangeDetection

google

arduino state change detection

also.

a7

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 :wink: !!!

> const int button1Pin = 2;  
> const int button2Pin = 3;
> 
> // Variables will change:
> int button1PushCounter = 0;  
> int button2PushCounter = 0;  
> int button1State = 0;        
> int button2State = 0;
> int lastButton1State = 0;    
> int lastButton2State = 0;
> 
> void setup() {
> 
>   pinMode(button1Pin, INPUT);
>   pinMode(button2Pin, INPUT);
>   Serial.begin(9600);
> }
> 
> 
> void loop() {
>   button1State = digitalRead(button1Pin);
>   button2State = digitalRead(button2Pin);
> 
>   if (button1State != lastButton1State) {
>     if (button1State == HIGH) {
>       button1PushCounter++;
>      
>     }
>     delay(50);
>   }
>   if (button2State != lastButton2State) {
>     if (button2State == HIGH) {
>       button2PushCounter++;
>      
>     }
>     delay(50);
>   }
>   
>   lastButton1State = button1State;
>   lastButton2State = button2State;
>  
>   Serial.print(button1State);
>   Serial.print("\t");
>   Serial.print(lastButton1State);
>   Serial.print("\t");
>   Serial.print(button1PushCounter);
>   Serial.print("\t");
>   Serial.print(button2State);
>   Serial.print("\t");
>   Serial.print(lastButton2State);
>   Serial.print("\t");
>   Serial.println(button2PushCounter);
>   
> }
> 
> }

Groundfungus... could it be something like this ?

const int button1Pin = 2;  
const int button2Pin = 3;

// Variables will change:
int button1PushCounter = 0;  
int button2PushCounter = 0;  
int button1State = 0;        
int button2State = 0;
int lastButton1State = 0;    
int lastButton2State = 0;

void setup() {

  pinMode(button1Pin, INPUT);
  pinMode(button2Pin, INPUT);
  Serial.begin(9600);
}


void loop() {
  button1State = digitalRead(button1Pin);
  button2State = digitalRead(button2Pin);

  if (button1State != lastButton1State) {
    if (button1State == HIGH) {
      button1PushCounter++;
     
    }
    delay(50);
  }
  if (button2State != lastButton2State) {
    if (button2State == HIGH) {
      button2PushCounter++;
     
    }
    delay(50);
  }
  
  lastButton1State = button1State;
  lastButton2State = button2State;
 
  Serial.print(button1State);
  Serial.print("\t");
  Serial.print(lastButton1State);
  Serial.print("\t");
  Serial.print(button1PushCounter);
  Serial.print("\t");
  Serial.print(button2State);
  Serial.print("\t");
  Serial.print(lastButton2State);
  Serial.print("\t");
  Serial.println(button2PushCounter);
  
}

}

Confirm:

  • Every time a switch is pressed you want to print all the switch states ?

  • Every time a switch is released you want to print all the switch states ?

Larry,
What I need is that :

  • Every time a button is pressed, a "1" has to be printed only one time

But the instrument has to be polyphonic, so :

  • If several buttons are pressed at the same time, theirs states have to be printed once.

The notes that are played need to be set off when the buttons are released, so :

  • The low state of the button "0" has to be sent only once when button are released.
  • When nothing is touched there's no need to print anything...

That is the idea, yes. Though I would use active low switches.