Hello everyone, I'm quite new here (forums and programming), so please forgive me some silly questions. Also I'm not english-speaking, so sorry for any mistakes.
So, here's my question. I've written a program, similiar to this one:
// set pin numbers:
const int buttonPin = 2;
const int ledPin = 13;
int ledState = LOW;
int buttonState = 0;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop()
{
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH)
{
if (ledState == HIGH)
{
digitalWrite(ledPin, LOW);
else
digitalWrite(ledPin, HIGH):
}
}
}
Let's say my button is perfect, so it does not bounce. What happens, then? Arduino starts void loop() and sees, that I pressed my perfect button. It changes the LED state. And instantly it begins the loop once again, once again sees, that button is pressed, and once again chagnes LED state! So, even with a perfect button I have only 50% chance to get what i want (changing LED state 0->1 or 1->0). Do I always have to write few lines, just to make sure, that Arduino reacts only when the button state has CHANGED?
I've seen few tutorials, but did not find an exact answer.
Edit: i did some debouncing on the other computer and my circut worked. What's important: my arduino ignored only buttonState changes faster than 5ms, and did not ignore situation, where it didn't change...