Hi everybody,
I'm an amateur using arduino to create a toy for my kid.
In this project, I use an Arduino Mega and a switch led button (Link)
I wanted to control the led (make it blink before it's pushed on) on this switch to tell the user (my son) which button needs to be pushed on.
I wanted also to get the information when the button is pushed to go to next step of the game.
I decided to put the + of the button on my output 50 to control the led
and to attach the button to my input 51 to get the information "high" when the button is pushed on.
I had some troubleshooting in my project with an incoherent information after the button is pushed so I isolated it in a smaller project :
const int buttonPin1 = 51; // the number of the pushbutton pin
const int ledbuttonPin1 = 50; // the number of the pushbuttonLED pin
int buttonState1 = 0; // variable for reading the pushbutton status
void setup() {
Serial.begin(9600);
pinMode(buttonPin1, INPUT);
pinMode(ledbuttonPin1, OUTPUT);
}
void loop() {
buttonState1 = digitalRead(buttonPin1);
digitalWrite(ledbuttonPin1, buttonState1); // On the project, the ledbuttonPin1 is used to blik as a call to action.
Serial.print("buttonState1=");
Serial.println(buttonState1);
delay(30);
}
The problem I had is the following :
The serial returns "buttonState1=0" while the button isn't pushed.
As soon as it is pushed, "buttonState1=1"
Until there it works as expected
But, when I unlock the button, it still get the information "buttonState1=1" during a variable amount of time (15 seconds or 1 minutes).
Usually when the arduino is reseted after some time, it goes back to "buttonState1=0" until it's pushed again. I need to have a more precise information in order to get the information as soon the button is unlocked.
Maybe I should add a resistance, maybe this kind of button is not done to be used with a control of the led while getting the info from the button ...
Every help would be greatly appreciated.
To be more precise, I have 6 buttons in my project and they are linked like this (maybe this explain some troubleshootings because it's not the cleanest way to solder it ...)
Thank you all