I have an arduino code here and I am fairly new with the cod
I have an arduino code here and I am fairly new with the code. what is the 20ms delay before the digital read accomplishing?
void loop() {
// put your main code here, to run repeatedly:
if (digitalRead(sw) == LOW)
{
delay(20);
if(digitalRead(sw) == LOW)
{
if((counter >=3) && (counter <=6))
{
digitalWrite(counter, HIGH);
delay(500);
}
else if((counter >=7) && (counter <=10))
{
digitalWrite(counter-4, LOW);
delay(500);
}
counter++;
}
}
if(counter > 10)
{
counter = 3;
digitalWrite(3 ,LOW);
digitalWrite(4 ,LOW);
digitalWrite(5 ,LOW);
digitalWrite(6 ,LOW);
}
}
Solution
When you are reading digital input lets, suppose from a switch, so you pressed the switch and a phenomenon is happen that called contact bounce. That means when a button is pressed the signal that is going to input is very noisy for a few millisecond. We will not able to notice it because its is too fast for us to notice. But a microcontroller could able to see it happening because the loop() function will have run several times during this brief period of bouncing. A very best way to get away with it is to use a delay() to put the Arduino to sleep during the bouncing period and have it wake up when the button will stable. I think 20ms delay is enough for your buttons.

