1

I have this simple push button schematic: enter image description here

The idea is that the LED is on as long as the button stays pushed, with the switch closed.

The code is below:

int ledPin=10;
int switchPin=5;

void setup()

{
  Serial.begin(9600);
  pinMode(switchPin, INPUT);
  pinMode(ledPin,OUTPUT);

}
void loop()

{
  digitalWrite(ledPin,LOW);//DBG
  if (digitalRead(switchPin)==HIGH)
{
 digitalWrite(ledPin,HIGH);//DBG
 delay(500);
}
 if (digitalRead(switchPin)==LOW);
{
digitalWrite(ledPin,LOW);//DBG 
delay(200);
}
}

As I keep the button pressed, the LED blinks regularly, instead of staying on. Why? I have tried using a capacitor between the switch and switchPin, with no change.

1

The beginning of your code in the loop:

void loop()  {
  digitalWrite(ledPin,LOW); //remove this line here
  if (digitalRead(switchPin)==HIGH)
{
     digitalWrite(ledPin,HIGH);//DBG
     delay(500);
}
 if (digitalRead(switchPin)==LOW);
 {
      digitalWrite(ledPin,LOW);//DBG 
       delay(200);
     }
}

Before your first if(digital read(switch pin) == HIGH) you turn the LED off, and then you have a delay, which gives the blinking effect, remove that and it will not blink.

  • @GiuseppeBarbesino not stupid just the formating of the code makes it unreadable, and will cause one to miss read code, happens to many ;) – RSM Apr 10 '16 at 15:55

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.