r/arduino • • 1d ago

Why would a print statement solves an input or timing problem?

I built a very simple system with a momentary switch that activates a linear actuator, which then goes through a sequence of motions (hence the Arduino instead of a simple switch).

My main loop has a variable that tracks the previous status of the switch and then I assign a digitalRead() to a second variable. By comparing the digitalRead to the previous status, the code only responds once to status changes (I don't know how long the user will hold the button down).

It worked, but had unexpected problems - the actuator would only complete part of its sequence and then branch into other parts or re-start its sequence. I figured the mechanical switch was bouncing or something like that, so I added in delays and so forth, but nothing solved the problem.

To try to figure it out, I added a print statement immediately after where I assign the digitalRead() status so I could see what status was being read. This was just a debug attempt, but it completely solved the problem and everything worked properly. I thought that was a coincidence, I must have made some other change somewhere so when everything was working properly, I commented out the print statement, and the timing problems came back.

I tried other variations like just doing a second digitalRead() after the first one (without printing anything) and assigning the second digitalRead() to a dummy variable, but none of those fixed the problem; the print statement seemed to clear some buffer or something.

Can anyone explain this, or is there a better way to fix the problem? Thanks.

void loop() {

buttonPush = digitalRead(buttonPin);

Serial.println(digitalRead(buttonPin)); <--- This is the debug line that fixed everything

if (buttonPush == HIGH && buttonState == LOW) { // Button released

// Go through button release sequence

}

3 Upvotes

3 comments sorted by

6

u/mikemontana1968 1d ago

The issue was solved because the Serial.Println introduced a delay. The delay masked your real issue: "debounce". Mechanical switches turn on and off several times in the microsecond it takes for the metal to fully click and stop vibrating. You could replace the serial.println with a small delay and get the same effect.

There are standard ways to programmatically deal with the "debounce" issue. The lazy, but perfectly fine & workable for your situation is to go with a delay every time you read the button. The better way is to only delay when the state of the button has changed:

const int buttonPin = 2;
const unsigned long debounceDelay = 50; // ms
int lastReading = HIGH;
int buttonState = HIGH;
unsigned long lastDebounceTime = 0;

void setup() {
pinMode(buttonPin, INPUT_PULLUP); // LOW when pressed, HIGH when released
}

void loop() {
int reading = digitalRead(buttonPin);
if (reading != lastReading) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) { //this determines if the new signal is really set
if (reading != buttonState) {
buttonState = reading;
if (buttonState == LOW) {
// put your "take action" code here
}
}
}
lastReading = reading;
}

3

u/triffid_hunter Director of EE@HAX 1d ago

Serial.print() can take a while if the TX buffer is full or the library for your target board doesn't use a TX buffer, especially at lower baud rates.

To get similar behaviour without a print, you could try just putting longer delay()s in there - 100ms would be a reasonably safe starting point.

Note that mechanical buttons can bounce for 20-50ms, so reading them faster than that isn't particularly helpful.

2

u/gm310509 400K , 500K , 600K , 640K , 750K 1d ago

It is unlikely that that one line changed much - but stranger things have been known to happen.

What most likely happened is that this was enough of an extra hurdle to tip the balance from it just not quite working to just working. It could also be that you are printing so much that the print statement is blocking (because the output buffer is full) and adding small delays to the execution of your program.

It would make it much easier to comment if you shared the full code (as it currently stands) along with a circuit diagram and some photos.

When sharing code, please use a formatted code block.
The guide explains how to do that. There is also a link to a video that describes the exact same thing if you prefer that format.