Code explained in laymans terms???

Can you help by telling if my interpretations of the following code is correct in laymans terms?

void loop() {
int reading = digitalRead(buttonPin);
reading will equal what is read by the buttonPin

if (reading != lastButtonState) {
If the reading does not equal the buttonstate

lastDebounceTime = millis();
don't understand what is happening here, but is says lastDebounceTime equals millis
}

if ((millis() - lastDebounceTime) > debounceDelay) {
If (millis minus lastDebounceTime) is greater than debounceDelay

if (reading != buttonState) {
If reading does not equal buttonstate
buttonState = reading;
then buttonState equals reading

if (buttonState == HIGH) {
If buttonState is equal to high (pressed)
ledState1 = !ledState1;
Then ledState1 is not equal to ledState1

Some parts of the above code I don't fully understand and I hope somebody helpful on here might be able to make it clearer.

lastDebounceTime = millis();

millis() function returns a value and lastDebounceTime is set to that value

ledState1 = !ledState1;

ledState1 is changed to its opposite state

Think of a single = as "value of right hand side expression is assigned to left hand side variable"

Maybe a step back will help. When you press a button, you would think that it goes from off to on. Real buttons are not like that, they literally bounce and cause multiple on/off transitions computers are so fast they can respond to each of the transitions making it look like you pressed multiple times. By putting in a small delay, you can wait past the bounces and just see one button press.

The other thing going on is the switch debouncing. When you open or close a switch. it may generate several pluses (bouncing). This happens on the transition of the switch. Google debouncing for more details.

The code ensures that the action only takes place once when the button is pressed.

ieee488:
lastDebounceTime = millis();

millis() function returns a value and lastDebounceTime is set to that value

ledState1 = !ledState1;

ledState1 is changed to its opposite state

Many thanks for the reply :slight_smile:

So essentially the time recorded by millis becomes the value for lastDebounceTime?

Regarding the ledState. What exactly does it mean to change the ledState to its opposite state?
I don't understand what that means. Or does it mean that if the LED state is already HIGH then change it to LOW and vice versa?

KeithRB:
Maybe a step back will help. When you press a button, you would think that it goes from off to on. Real buttons are not like that, they literally bounce and cause multiple on/off transitions computers are so fast they can respond to each of the transitions making it look like you pressed multiple times. By putting in a small delay, you can wait past the bounces and just see one button press.

nathancamp:
The other thing going on is the switch debouncing. When you open or close a switch. it may generate several pluses (bouncing). This happens on the transition of the switch. Google debouncing for more details.

The code ensures that the action only takes place once when the button is pressed.

Thanks to both of you for the replies.
I do understand the concept of Debounce but I am struggling to understand some of the coding.

Thanks for this too, that's a nice simple way to think about it.

AWOL:
Think of a single = as "value of right hand side expression is assigned to left hand side variable"

TableLeg:
Regarding the ledState. What exactly does it mean to change the ledState to its opposite state?
I don't understand what that means. Or does it mean that if the LED state is already HIGH then change it to LOW and vice versa?

Yes, HIGH is the opposite of LOW.

In the case of

X =!X

!X is true (1) if X evaluates to false (0), and viseversa.

LOW is 0, HIGH is 1

Also means digitalWrite(pin,1) is the same as digitalWrite(pin,HIGH). You'll see that a lot in code around the web.

TableLeg:
int reading = digitalRead(buttonPin);
reading will equal what is read by the buttonPin

No. It says "read the pin, and then store that value in the variable named reading

TableLeg:
lastDebounceTime = millis();
don't understand what is happening here, but is says lastDebounceTime equals millis

No. It says "find out what time it is now, and then store that value in the variable named lastDebounceTime

There was a langugae named Pascal that used := rather than a plain equals so as to drive home this point.

Many thanks to all for the replies :slight_smile:

So to follow the below code would be:

int reading = digitalRead(buttonPin);

if (reading != lastButtonState) {

A press on buttonpin changes it's state from LOW to HIGH.
Therefore variable 'reading' will be set to the value at digital read (HIGH).

Then if the value of 'reading' is the opposite to the value at lastbuttonstate it knows the button has been pressed.

lastDebounceTime = millis();
Then record the length of time the button is pressed in lastdebouncetime.

Am I getting close to understanding this?

lastDebounceTime = millis();
Then record the length of time the button is pressed in lastdebouncetime.

No, this records the time the button is first pressed. this is actually like starting a timer.

the next line is the timer:
if ((millis() - lastDebounceTime) > debounceDelay) {

deBounceDelay is how long we want the timer to run.

So if Time now(millis()) minus the time it was when we first pushed the button(lastDeBounceTime) is greater than deBounceDelay. then time is up.

The tricky part to remember is that the loop() function continuously loops. and in a small sketch like this probably thousands of times a second.

It will have looped many times before the timer expires.
So in this line:
if (reading != buttonState) {

"reading" is a different reading than the first time around. and it is saying if the reading is still the same(and is still different than buttonState)
then the button is not bouncing and we have confirmed a good button press.

It is a very common mistake to confuse = (which is assignment, not comparison, and involves a variable and an expression) with == (which compares two expressions and is nothing to do with variables or changing their value)

Some programming language have a better symbol for assignment, such as 'var := value' (read as 'set equal to')
or 'var <- value' (which shows the value is copied form right to left and is more intuitive).

C style languages are forever stuck with the poor choice of '=' for assignment: you just have to learn these
things by doing and making the common mistakes a few times!

TableLeg:
Then if the value of 'reading' is the opposite to the value at lastbuttonstate it knows the button has been pressed.

Pressed or released.

Part of the problem is the english language. "reading will equal what is read by the buttonPin" it may mean "the reading is already equal to what is read by the buttonPin" or it may mean "reading is caused to become equal to what is read by the buttonPin".

Had all sorts of problems getting specs from a client because of this. He'd say 'the frob activates 3 seconds after the gazebo", and I didn't know if he was telling me that this is what the gazebo does, or if he was telling me that he wanted me to make the arduino do that in response to the gazebo doing its thing. What was missing was who was doing what, and the modal particles (should, must, will).

Notice how in the various RFCs, they explicitly use these modal words:

An application that sends a request or response message that includes HTTP-Version of "HTTP/1.1" MUST be at least
conditionally compliant with this specification.

Avoid passive voice and use modal verbs to indicate what is doing what.

Many thanks to all of you for the detailed replies :slight_smile:

I really appreciate it.

It might be a good idea for me to look at some simple Sketches again and try and be sure I fully understand them before moving on.

I thought I was getting a good grip of the simple stuff and had started on a small project, but have hit the wall so to speak trying to go from a counterPush which is counting up to 3 before resetting to zero (which I have working fine), to adding a long press of the button to complete a separate action.
I looked at the sketches which contained long press codes and couldn't quite understand how it was working hence my questions.
I tried to apply what I thought I had grasped but it didn't work.

I definitely have a better idea now so I will go and see if I can implement it again and get it working.

Thanks again.

Just to confirm in laymans terms,

I currently have:

Start at zero and have no output,
Press the momentary button and release,
Counter moves on 1 place and outputs to an LED (No.1),
Press the momentary button and release,
Counter moves on 1 place and outputs to an LED (No.2),
Press the momentary button and release,
Counter moves on 1 place and outputs to an LED (No.3),
Press the momentary button and release,
Counter returns back to zero.

But in order to be able to use short and long button presses I would need something like:

Start at zero and have no output,
IF I Press the momentary button and release, use millis code to record the time the button is pressed,
IF equals to set short time,
Counter moves on 1 place and outputs to an LED (No.1),
IF equals to set long time,
Output to LED No.4,
Continue to wait for the next short button press,

IF I Press the momentary button and release, use millis code to record the time the button is pressed,
IF equals to set short time,
Counter moves on 1 place and outputs to an LED (No.2),
IF equals to set long time,
Output to LED No.4,

IF I Press the momentary button and release, use millis code to record the time the button is pressed,
IF equals to set short time,
Counter moves on 1 place and outputs to an LED (No.3),
IF equals to set long time,
Output to LED No.4,

IF I Press the momentary button and release, use millis code to record the time the button is pressed,
IF equals to set short time,
Counter returns back to zero.

Is this the right way to think about it?

Are you ever going to turn LED #4 off and if so when ?

UKHeliBob:
Are you ever going to turn LED #4 off and if so when ?

Thanks for the reply.

That's a good question.

In essence the project requires that a 5v signal be read for 2 seconds. The LED's are currently only to replicate the outputs, therefore I am using the 4th LED so I can test the code is working.

I would therefore say that turning off the 4th LED after 3 seconds should suffice.

IF equals to set short time,
Counter moves on 1 place and outputs to an LED (No.1),
IF equals to set long time,
Output to LED No.4,

That's a good plan, but instead of "equals to set short time"

try - less than set time equals a short press
and - greater than set time equals a long press

as it would be impossible to press a button for an exact amount of time