SOLVED::println() to Serial prints to many times

Goal:

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!

Try this library: Arduino Playground - MomentaryButton. It solved all my button problems!

YOU'RE AWESOME!! Thank you so much, I'll give it a quick try.

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?

Can you show your code?

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:

#include "WProgram.h"

to...

#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif

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'

I actually got it working. Man they seriously did a number on their libraries.

Original main.cpp:

#include <Arduino.h>

int main(void)
{
	init();

#if defined(USBCON)
	USBDevice.attach();
#endif
	
	setup();
    
	for (;;) {
		loop();
		if (serialEventRun) serialEventRun();
	}
        
	return 0;
}

Edited main.cpp

#include <Arduino.h>

int main(void)
{
	init();

#if defined(USBCON)
	USBDevice.attach();
#endif
	
	setup();
    
	for (;;) {
		void loop();//<---You have to add void.
		if (serialEventRun) serialEventRun();
	}
        
	return 0;
}

It seems that you left out the mandatory void loop in your code. But I can say for sure, 'cause I can't see it!

Note to other people reading my first sentence: I am not talking about using main().

Now you made the void loop not mandatory?

Apparently I did a number on my brain!

You were right I switched it back to loop()

Correct my mistake of using loops().

I greatly appreciate your help dkl65! And last by not least your patience!

So everything works now?
You are welcome. :slight_smile:

It sure does!

Just for the fun part of getting python to read the line it prints out. But as far as the program working to print to Serial Monitor, it works!