Go Down

Topic: Get the push button sense once (Read 567 times) previous topic - next topic

HazardsMind

And maybe look for a button state change.
Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

UKHeliBob

Debounce the button by the look of it.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

zoom

1
11
111

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

UKHeliBob

This code does what I would expect
Code: [Select]
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: [Select]
1
11
111
1111

NOTE - no debounce implemented but the button action seems relatively snappy so not a problem
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

zoom

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: [Select]


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


Go Up