Arduino Forum

Using Arduino => Programming Questions => Topic started by: dragonflex on Apr 17, 2019, 03:44 pm

Title: Push button on-off
Post by: dragonflex on Apr 17, 2019, 03:44 pm
Hallo everybody, could you help me a little? As long as I push a button I want the output pin1 to became HIGH and output pin2 LOW, else LOW and LOW, When I push the button for second time I want pin1 to became LOW and pin2 HIGH, else LOW and LOW. Third time begin cycle again. Any idea? Thanks a lot. 
Title: Re: Push button on-off
Post by: PaulS on Apr 17, 2019, 03:48 pm
Quote
Any idea?
Yeah. Look at the state change detection example. It describes exactly what you are asking about, except that it deals with one output pin. Trivial to make it deal with two pins instead.
Title: Re: Push button on-off
Post by: neiklot on Apr 17, 2019, 03:53 pm
That's a tad ambiguous, OP....

Do you mean while the button is pressed the first time the outputs are high/low and when released low/low, followed by another held press low/high and back to low/low on release?

In other words while the button unpressed the output is always low/low, and while it is pressed an odd time, outputs are high/low and low/high during even presses?

If that's right you need to do what PaulS just said to see a new press, and the example includes doing a count so it's easy to test for odd / even, BUT you also need to handle the unpressed case.

(Iff I understood you correctly, OP.)



Title: Re: Push button on-off
Post by: dragonflex on Apr 17, 2019, 04:04 pm
Thats right, neiklot. A piece of code please?
Title: Re: Push button on-off
Post by: PaulS on Apr 17, 2019, 04:09 pm
Thats right, neiklot. A piece of code please?
Quote
If that's right you need to do what PaulS just said
Title: Re: Push button on-off
Post by: evanmars on Apr 17, 2019, 04:10 pm
Here's a piece of code:
Code: [Select]
void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}


Fill in the rest with your best try.
Title: Re: Push button on-off
Post by: dragonflex on Apr 17, 2019, 04:14 pm
 if (buttonPushCounter % 1 ) {
    digitalWrite(ledPin3, HIGH);
    digitalWrite(ledPin4, LOW);
  } else {
    digitalWrite(ledPin3, LOW);
    digitalWrite(ledPin4, LOW);
  }
  if (buttonPushCounter % 2 ) {
    digitalWrite(ledPin3, LOW);
    digitalWrite(ledPin4, HIGH);
  } else {
    digitalWrite(ledPin3, LOW);
    digitalWrite(ledPin4, LOW);
  }

this do not work
Title: Re: Push button on-off
Post by: evanmars on Apr 17, 2019, 04:27 pm
this do not work
No big surprise.  There is no setup() and no loop().

Not to mention no digitalRead of the push button pin.
No assigning a value to the undeclared/undefined variable "buttonPushCounter"
Title: Re: Push button on-off
Post by: dragonflex on Apr 17, 2019, 04:36 pm
Code: [Select]

const int  buttonPin = 2;   
const int ledPin3 = 8;
const int ledPin4 = 9;         

// Variables will change:
int buttonPushCounter = 0; 
int buttonState = 0;         
int lastButtonState = 0;     
void setup() {
 
  pinMode(buttonPin, INPUT_PULLUP);

  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
 
  Serial.begin(9600);
}


void loop() {
 
  buttonState = digitalRead(buttonPin);


  if (buttonState != lastButtonState) {
   
    if (buttonState == LOW) {
     
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);
    } else {
   
      Serial.println("off");
    }
   
    delay(50);
  }

  lastButtonState = buttonState;

  if (buttonPushCounter % 1 ) {
    digitalWrite(ledPin3, HIGH);
    digitalWrite(ledPin4, LOW);
  } else {
   digitalWrite(ledPin3, LOW);
 digitalWrite(ledPin4, LOW);
  }
  if (buttonPushCounter % 2 ) {
  digitalWrite(ledPin3, LOW);
    digitalWrite(ledPin4, HIGH);
  } else {
   digitalWrite(ledPin3, LOW);
    digitalWrite(ledPin4, LOW);
  }
  }[code]
The code
Title: Re: Push button on-off
Post by: wildbill on Apr 17, 2019, 04:47 pm
This makes no sense:
Code: [Select]
buttonPushCounter % 1
It's always 0, so your code always sets your two outputs low unconditionally.

You want three states, so if you're going to use the % operator, use 3, not 1.

Then add a switch statement based on the result of that modulo calculation with a case for 0, 1  and 2.
Title: Re: Push button on-off
Post by: PaulS on Apr 17, 2019, 04:47 pm
Code: [Select]
 if (buttonPushCounter % 1 ) {
    digitalWrite(ledPin3, HIGH);
    digitalWrite(ledPin4, LOW);
  }

What will be the remainder of dividing an int by 1?
Title: Re: Push button on-off
Post by: neiklot on Apr 17, 2019, 04:54 pm
That logic counts the new presses and you only do the led switching once you come out of the main if. You need to do your odd/even thing inside the main if where it detects the button press since you said the leds are only on WHILE the button is pressed.

It's too late, where you do it.

And an easy way to test for odd or even is a bitwise and:


Code: [Select]
if (count & 1)
{
    //odd
}
  else
{
   //even
}