Loading...
Pages: [1]   Go Down
Author Topic: Get the push button sense once  (Read 138 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm using the push button with the built in pull-up resistor;

pinMode(b1, INPUT_PULLUP);

and have such a main loop. Whenever I press the push button and hold, the empty string called "pass" takes dozens of "1", as expected smiley
However I want to make it sense the push once until I release it and push again.
By that structure, it is not likely to implement I think.

What would your suggestions be ?

void loop()
{
  if(digitalRead(b1) == LOW)
  {
    pass += "1";
    counter += 1;
  }
}
Logged

Netherlands
Offline Offline
Tesla Member
***
Karma: 86
Posts: 9360
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


You must make a variable that holds the state of the button. Only if the state changes there is action

something like this
Code:

int pressed = 0;

void loop()
{
  if (digitalRead(b1) == LOW && pressed == 0)
  {
    pressed = 1;
    pass += "1";
    counter++;
  }
  if (digitalRead(b1) == HIGH && pressed == 1)
  {
    pressed = 0;
  }
}

give it a try
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Seems pretty logical but doesn't work.
I've implemented more or less a similar solution but it didn't work either. Strange


You must make a variable that holds the state of the button. Only if the state changes there is action

something like this
Code:

int pressed = 0;

void loop()
{
  if (digitalRead(b1) == LOW && pressed == 0)
  {
    pressed = 1;
    pass += "1";
    counter++;
  }
  if (digitalRead(b1) == HIGH && pressed == 1)
  {
    pressed = 0;
  }
}

give it a try

Logged

East Anglia (UK)
Offline Offline
Edison Member
*
Karma: 47
Posts: 1383
May all of your blinks be without delay
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This code does what I would expect
Code:
String pass;
int counter =0;
int pressed = 0;
int b1 = 3;

void setup()
{
  pinMode(b1, INPUT_PULLUP);
  Serial.begin(9600);
}
void loop()
{
  if (digitalRead(b1) == LOW && pressed == 0)
  {
    pressed = 1;
    pass += "1";
    counter++;
    Serial.println(pass);
  }
  if (digitalRead(b1) == HIGH && pressed == 1)
  {
    pressed = 0;
  }
}
Sample output of 4 button presses
Code:
1
11
111
1111
NOTE - no debounce implemented but the button action seems relatively snappy so not a problem
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

1
11
111

at 1 press. Very snappy :/
What can I do ?
Logged

East Anglia (UK)
Offline Offline
Edison Member
*
Karma: 47
Posts: 1383
May all of your blinks be without delay
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Debounce the button by the look of it.
Logged

Queens, New York
Offline Offline
Edison Member
*
Karma: 27
Posts: 1539
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

And maybe look for a button state change.
Logged

UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino

Arduino Tutorials, coming soon.

"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown

Pages: [1]   Go Up
Print
 
Jump to: