0

O holy simplicity. I've seen lots of solutions for this but none that will fit to my scheme. First of all, lets add simple code:

void setup() {
  pinMode(0, OUTPUT);
  pinMode(1, INPUT);
}

void loop() {
  int a=digitalRead(1);

  if(a==LOW) {
    delay(1000);
    digitalWrite(0, HIGH);
    delay(1000);
    digitalWrite(0, LOW);
  }
}

What the heck I need to add that the IF command runs only once while button is staying in low state and it will be reset eventually when button is set to high again. I've read five different solutions here and tried them out and yet not working as I intented. Rest of the solutions that I found in the net were complicated and I needed to add libraries or alter the code wayyyyyy too much. I'm trying to find the most simple solution for this simple code. And as it will be simple, for some reason I'm over thinking it and not finding solution myself.

Dave Tweed
  • 168,369
  • 17
  • 228
  • 393
  • 1
    Please show what you tried, and tell us in what way it didn't work. – The Photon Apr 03 '20 at 03:43
  • 1
    you might want to fix your indentation – Neil_UK Apr 03 '20 at 05:29
  • Possibly related: https://arduino.stackexchange.com/questions/408/why-does-my-sketch-report-too-many-button-presses – MarkU Apr 03 '20 at 06:02
  • Please explain why your function is called loop. Where do you call this function? How does the function loop? When is the code inside the if statement executed? – skvery Apr 03 '20 at 06:15
  • 5
    @skvery considering it's tagged Arduino, it's standard program structure. – tuskiomi Apr 03 '20 at 07:18
  • What are the solutions you've tried and in which way didn't they work? If you've already tried several solutions, chances are fairly high that we would propose any of these if we don't know **what** you've done. – Paul Kertscher Apr 03 '20 at 10:53
  • @skvery loop is called loop because it loops. It's essentially a while 1 loop. – Passerby Apr 03 '20 at 14:29

3 Answers3

3

After you have seen it low, you have to wait for it to go high again:

if(a==LOW)
{
  delay(1000);
  digitalWrite(0, HIGH);
  delay(1000);
  digitalWrite(0, LOW);
  while (digitalRead(1)==LOW)
    delay(10); // or more or less then 10 
}
Oldfart
  • 14,212
  • 2
  • 15
  • 41
1

I would usually do it like this:

void setup() {
  pinMode(0, OUTPUT);
  pinMode(1, INPUT);
}

void loop() {
  int a=digitalRead(1);

  if(a==LOW) {
    delay(1000);
    digitalWrite(0, HIGH);
    delay(1000);
    digitalWrite(0, LOW);
    while(digitalRead(1) == LOW);
  }
}

That will exit the "while" statement, and thus the "if" statement, only after the button has been released.

DerStrom8
  • 21,042
  • 8
  • 61
  • 95
1

The other examples are blocking, they will prevent anything else from being done while the button is pressed. Here is a simple change with a variable used as a state tracker. Code will continue to run, and your if will only trigger if both pin 1 is low AND the variable is false.

void setup() {
  pinMode(0, OUTPUT);
  pinMode(1, INPUT);
  int runonce=false;
}

void loop() {
  int a=digitalRead(1);

  if(a==HIGH) {
    runonce=false;
  }

  if(a==LOW && runonce==false) {
    delay(1000);
    digitalWrite(0, HIGH);
    delay(1000);
    digitalWrite(0, LOW);
    runonce=true;
  }
}
Passerby
  • 72,580
  • 7
  • 90
  • 202