I want to print a button press to the Serial Monitor. Whenever a button is pressed it should show only once. Depending on which button I press will change what the Serial Monitor receives.
Currently:
The program allows it to print the message to the Serial Monitor, however it prints multiple times.
Serial Monitor Output: NOTE It prints a lot more then I show here. A LOT MORE!
0
0
0
0
0
0
1
1
1
1
1
1
Code:
/*
DigitalReadSerial
Reads a digital input on pin 2, prints the result to the serial monitor
This example code is in the public domain.
*/
//Constant Pin locations
int pushButtonR = 2;
int pushButtonL = 4;
int ledPinR = 8;
int ledPinL = 9;
//Variables will change
int buttonStateR;
int buttonStateL;
int lastButtonStateR = LOW;
int lastButtonStateL = LOW;
//Debounce variables
long lastDebounceTime = 0;
long debounceDelay = 50;
void setup(){
pinMode(pushButtonR, INPUT);
pinMode(pushButtonL, INPUT);
pinMode(ledPinR, OUTPUT);
pinMode(ledPinL, OUTPUT);
Serial.begin(9600);
}
void loop(){
//Read the state if the button into a local variable
int readingR = digitalRead(pushButtonR);
int readingL = digitalRead(pushButtonL);
if (readingR != lastButtonStateR || readingL != lastButtonStateL){
lastDebounceTime = millis();
}
if((millis() - lastDebounceTime) > debounceDelay) {
buttonStateR = readingR;
buttonStateL = readingL;
}
int pressOutR = digitalRead(pushButtonR);
int pressOutL = digitalRead(pushButtonL);
lastButtonStateR = readingR;
lastButtonStateL = readingL;
if (buttonStateR == HIGH){
Serial.println(0);
buttonStateR = LOW;
digitalWrite(ledPinR, HIGH);
delay(1);
digitalWrite(ledPinR, LOW);
}
else if (buttonStateL == HIGH){
Serial.println(1);
digitalWrite(ledPinL, HIGH);
delay(1);
digitalWrite(ledPinL, LOW);
}
}
What I Need Help With:
If possible I would like help with printing only one line per button press.
I appreciate any and all help. Thank you in advance!
Hey I tried the example they have there, and added a println. For some reason the compiler keeps saying "class MomentaryButton' has no member named 'wasClicked'. It does the same with was held.
I did look at the files after importing them. The only thing I noticed that seemed strange was they have 'boolean' and not 'bool' which is C++ standard. However when I try and edit that and rerun the code it says ,"Error Compiling". So I changed it back. Any ideas?
The Arduino libary declared "boolean". I still don't get why they did that, and how it differs from bool. I don't think there is a difference.
Maybe you need to change:
lol you're going to love this now I got the strange 'boolean' to work , and now it gives me:
core.a(main.cpp.o): In function main': /Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduin o/main.cpp:14: undefined reference to loop'