Go Down

Topic: How do I report an apparent error in an example sketch ? (Read 1 time) previous topic - next topic


I've just got to the example Sketch StateChangeDetection, and I believe there to be an error in the code, namely:

int lastButtonState = 0;

should be

int lastButtonState = 1;

As written, the serial output erroneously logs a first button press, when the button has not been pressed at all. This small modification corrects this.

I had a look on the "Contact us" section, but wasn't sure if this was "Software" or "Documentation", or something else.


Hi, and welcome to the forum.

Please assume these examples have been tried many, many times.

The code looks for a change from the last time it was checked, then whether that change was from LOW (0) to HIGH (1) before it tells you through the serial monitor that the button was pressed.

You read the instructions coming with that code, or didn't you ?

Quote from: Code snippet
The circuit:
 * pushbutton attached to pin 2 from +5V
 * 10K resistor attached to pin 2 from ground
 * LED attached from pin 13 to ground (or use the built-in LED on
   most Arduino boards)
Leaving out (forgetting) the 10K resistor from pin 2 to GND will result in unstable results from the test.
The resistor will also make sure that the level of that pin, if not connected to VCC (by the button), will be pulled down to zero.

The code is correct if you meet the specifications of the hardware it depends on.

Have a look at "blink without delay".
Did you connect the grounds ?
Je kunt hier ook in het Nederlands terecht: http://arduino.cc/forum/index.php/board,77.0.html


Thank you for checking - I did have a 10k ohm resistor. I'll check everything and get back to you, but from memory the logic of the sketch is faulty, as it assumes buttonState == HIGH means that the button has been pressed, which is not true when the sketch is run for the first time.


Contrary to what @MAS3 implies I would not be surprised if there are errors in some examples.

However I have no idea how one would go about notifying someone to have them fixed.

Two or three hours spent thinking and reading documentation solves most programming problems.


About assumptions:

I'm assuming that the code you have in your IDE is the same as in mine (not the latest IDE).
And buttonState as well as lastButtonState are set to 0 upon declaration, which can be seen as an assumption.

First thing that happens every iteration, is a read of buttonPin, and the result is stored in buttonState.
Next buttonState and lastButtonState are compared, and if NOT equal, another check is performed to see whether the new state was a pressed button and that is then handled.
If it wasn't pressed, it was a released button which is then communicated through serial monitor.

If the first compare was not true, then the iteration will be really fast (it is fast anyway).
This is ran many thousand times per second if not pressed (i'm not sure how long the read of a pin takes but that will take most of the time), and about 20 times per second when a button was pressed due to the delay in there.

So it'll be a bit difficult to know for sure it really is registering a button press in the first iteration (another assumption that you mean this when you say "when the sketch is run for the first time").
There is a way to find out, but that is beyond the scope of this example so i'll leave that for now.
As you have told us you see the assumption for a HIGH somewhere in the code, could you please show where you see that ?
So copy that part of the code to this thread and explain how you interpret that part ?

Seeing your other posts makes me believe you do have some experience and / or understanding of code.
And my participation in this thread is because i really endorse to use the examples to be understood by anyone.
Which means read them, try to understand them and then change them to see you really did understand before you move on to another example.
So thumbs up for asking on this forum if you run into something.

Have a look at "blink without delay".
Did you connect the grounds ?
Je kunt hier ook in het Nederlands terecht: http://arduino.cc/forum/index.php/board,77.0.html


However I have no idea how one would go about notifying someone to have them fixed.
With the examples, the best way is to submit a pull request on GitHub. The examples bundled with the IDE are all here:

All the examples bundled with Arduino's libraries are also on GitHub:

However, in this particular case of the StateChangeDetection example, it works correctly as intended, so there is no need to submit a pull request. I'm certain there is room for improvement in the example sketches though, including StateChangeDetection . One thing I think would improve this sketch is to not use magic numbers for the pin state variables initialization:
Code: [Select]
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

Much easier to understand this way:
Code: [Select]
int buttonState = LOW;         // current state of the button
int lastButtonState = LOW;     // previous state of the button

And of course it's silly to use the int type for these variables.


Thank you everyone for your patient explanation. I did indeed go through the explanations with the code as I typed them in, and I also remember getting the erroneous "number of button pushes: 1" message when no button had been pressed.

However, having read through everything carefully again, I realized the source of my confusion was an extremely elementary one, of confusing HIGH and LOW (ie voltage, logical 1 and 0) with the position of the button in the physical world where a 'high' (ie released position corresponds to LOW / approaching 0V), and where a 'low' (ie depressed position corresponds to HIGH / approaching 5V).

I also cannot explain now why my change to the code appeared to rectify things at the time, nor can I duplicate my original erroneous situation.

Still, the code does make an assumption that the sketch is going to start with the button released (ie LOW / 0). As someone suggested in a PM to me, it might be better after all to set it to start with according to the actual situation.

It's also good to have an answer to my original question, even if there's no need to report an error on this one.

Go Up