i apologize if this question has been asked, but I can't find an answer that suits my needs. i am simply attempting to save the state of a button and only have values printed to serial monitor when it is pressed. (no scrolling of zeros or ones, just one value).
for example, once it is pressed and released, then it prints a '1' and it remembers the state. then, when I press the button again, it prints a '0' to the monitor.
This should be straightforward. I posted about doing this with an analog potentiometer and tried to transpose that code to this functionality, but it seems I am missing something. Thank you again.
//variables
const int buttonPin = 2;
boolean lastButtonState = 0;
boolean currentButtonState = 0;
char keys[] = {'1', '2'};
void setup() {
Serial.begin(115200);
pinMode(buttonPin, INPUT); //declare pushbutton as input
}
void loop() {
if (lastButtonState != currentButtonState) {
Serial.println(keys[0]);
}
else {
Serial.println(keys[1]);
}
lastButtonState = currentButtonState;
}