-1

In the first program after removing hand from the button it keeps on, means it keeps applying what is in the loop:

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

while in the second one it needs to press the button every time in order to apply what is in the loop,means to flash in this case,

int led = 13;
int 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);
  }
           }

Why different behavior although the same structure?

The problem is illustrated in this video : https://www.youtube.com/watch?v=u5WGvKwJYFY&t=2s

closed as unclear what you're asking by user31481, per1234, gre_gor, MatsK, jose can u c Jan 22 '18 at 15:30

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Looks confusing to me -- setup() should only run once per power-on cycle, and so since value is only set in setup() and not ever changed in loop() the behavior should never change after the initial powerup. Is your circuitry somehow resetting the arduino after each iteration? – Dave X Jan 21 '18 at 3:22
  • 3
    Both of your sketches are missing a "closing bracket", so they won't compile. They do not match the sketches in the video you've provided. Could you tell us what it is you are trying to accomplish? What should the LED do when you press the button? Flash the LED? What happens if you press the button again? Does it turn the flashing LED off? – VE7JRO Jan 21 '18 at 3:38
  • no the circuit works fine, i am not trying to accomplish something, it just happened and i want to know why different in applying the program, i want to understand how those programs are executed, i thought after pressing the button it should react the same way ,means either both prorams should apply only when press the button then keep applying or both should stop applying after i press the button, because the both have the same programming structure , – sumer fattoum Jan 21 '18 at 4:06
  • 1
    The code you posted does not compile, but it looks like if it did, it will completely ignore changes to button/value in loop() and not produce the effect you show in the video. – Dave X Jan 21 '18 at 5:20
  • 1
    @sumerfattoum, you do not understand ... the code that you posted is not the same as the code in the video – jsotola Jan 21 '18 at 7:44
2

You read value only once ... what do you expected?

In the first sketch, you put the LED ON and never turn it OFF, no matter what.

In the second sketch, you put the LED ON and one second later you turned it OFF. As the value doesn't change, it keeps repeating.

You mistake: you read the button in setup. You have to read in each loop iteration.

void loop() {
  value = digitalRead(button);
  if (value == HIGH)
  {
    digitalWrite(led, HIGH);
    delay(1000);
    digitalWrite(led, LOW);
    delay(1000);
  }
  • did you apply the codes you wrote or think ? i did that firstly , and now i reposted and reedited it so it will read the button in the loop , and still act the same as you see above – sumer fattoum Jan 21 '18 at 16:38
1

This iteration of your code is better. Now, the difference in behavior is that nothing in the first code ever executes digitalWrite(led, LOW);, while each iteration though the second code does. Therefore the LED remains on forever in code 1, and depends on the button press in code two.

If the behavior you desire is that the code remembers if the button has ever been pressed, latch that event in some state variable, and then in loop(), act on the value of the state variable:

int led = 13;
int button = 12;
int everHit = 0;
void setup() {
  pinMode(led, OUTPUT);
  pinMode(button, INPUT);
}

void loop() {
  if (digitalRead(button) == HIGH) everHit = 1;
  if (everHit) {
    digitalWrite(led, HIGH);
    delay(1000);
    digitalWrite(led, LOW);
    delay(1000);
  }
}

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