1

I want the LED to flash when I press the switch once, but it keeps flashing. I tried the idea of just lighting the LED and keeping it lit with just one button press, so I wrote:

int led=13,button=12;
void setup() {
pinMode(led,OUTPUT);
pinMode(button,INPUT);
}
void loop() {
if (digitalRead(button)==HIGH)
  digitalWrite(led,HIGH);
}

and this worked; it stayed on after I pressed the button once, so for the flashing LED I wrote:

int led=13,button=12;
void setup() {
pinMode(led,OUTPUT);
pinMode(button,INPUT);
}
void loop() {
if (digitalRead(button)==HIGH)
{
  digitalWrite(led,HIGH);
delay(1000);
 digitalWrite(led,LOW);
 delay(1000);

} }

but this didn't work. It stops flashing after I remove my hand from the switch. Why is this happening?

migrated from electronics.stackexchange.com Dec 18 '17 at 1:29

This question came from our site for electronics and electrical engineering professionals, students, and enthusiasts.

  • 1
    Welcome to EE.SE. Please edit the question and use the {} tags to format your code properly. Make sure it's indented properly too. You also need to fix capitalisation in your title and in your post for legibility and credibility. Your compiler wouldn't let you away with it. Why should we? Note that your question has no electrical design element to it so it will probably be moved to the Arduino StackExchange site. – Transistor Dec 17 '17 at 20:03
  • 2
    How can it remember the switch has been operated? – JIm Dearden Dec 17 '17 at 20:43
  • exactly i dont know whats the difference between the two cases above, the first one just one press of the bush button and the led keeps on, the second case it flashes only when i keep pressing on the bush button, and stops when i remove my hand..i want it also in the second case to keep flashing after i remove my hand – sumer fattoum Dec 17 '17 at 20:52
  • That's what I am trying to do, i understand it should execute the same way what is inside if {}..but that is not happening – sumer fattoum Dec 24 '17 at 11:17
  • If you want the LED to stay on indefinitely, remove the line that sets the LED to LOW. – Nick Gammon Dec 27 '17 at 1:55
2

Because the digital writes only apply on if (digitalRead(button)==HIGH). If you don't press the button, then the code goes:

void loop() {
}

Essentially doing nothing. Your code needs a bit more to keep track of the button press. Pseudo Code:

if button == pressed {
    int tracker=1;
}
if tracker == 1 {
    led on;
    wait;
    led off;
    wait;
}
loop
  • Thnx, can you please explain more, i understand that it should execute what is in if {} in the same way, so why one time it kept executing after i pressed zhe button and in the another example it stopped executing what is in if {} after i pressed the button , i can send videos if someone gives me email – sumer fattoum Dec 23 '17 at 16:18
  • Try posting your new code if you have modified yours based on this answer. Don't change your original code or the question won't make any sense. Add a new part to your question (edit the question) saying you tried XYZ and showing what that was. – Nick Gammon Dec 27 '17 at 1:54
0

This is what you need. When you pressed the button then your LED turn ON. And it still ON until you pressed the button again.I hope it will help you.

const int kPinBtn = 2;
const int kPinLed = 13;

boolean kPress = false;

void setup()
{
  pinMode(kPinBtn, INPUT_PULLUP);
  pinMode(kPinLed, OUTPUT);
}

void loop()
{
  if(digitalRead(kPinBtn) == LOW && kPress == false)
  {
    digitalWrite(kPinLed, HIGH);
    kPress = true;
  }
  if(digitalRead(kPinBtn) == LOW && kPress == true)
  {
    digitalWrite(kPinLed, LOW);
    kPress = false;
  }  
}
  • ok,thnx, i will study this code, but still the question, why in the first code it kept executing what is inside if {} even after i removed my hand from the push button, and in the second codeit stopped executing what is in if {} after i removed my hand from frm the switch button,although the both codes have the same structure but the difference only in what inside if {} ,i can send vids if you didnt get what i mean – sumer fattoum Dec 27 '17 at 17:27
  • @sumerfattoum Becuase you can see that here I use one flag means boolean named kPress. By default it's value is false. When you pressed the button for the first time then code check the value of kPress. If it is false then and then your LED turns ON and that time we changed the value of kPress means now it's true. Now, your LED remains ON until not pressing the second time. This happens because of the value of kPress. Now when you pressed the second time then code check again the value of kPress. If it's true then and then it turns OFF the LED. – Hasan Dec 28 '17 at 6:59
  • My friend, they are two different codes, and two different cases for my codes above , i upload the code differently to Arduino , there is no consideration if the button is pressed for the first and second time – sumer fattoum Dec 28 '17 at 16:01
  • The case is when I upload the first code then press the push button ,it keeps applying what is in if {} ,it means it stays on although i removed my hand from the button, now when i upload the second code and press the button it applies what is in if {} (means it flashes ) only when my hand stills on the button, but it stops flashing when i remove my hand unlike the first case although they have the structure and the difference only what is inside if {} – sumer fattoum Dec 28 '17 at 16:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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