Skip to main content
added 181 characters in body
Source Link

The two adjacent lines

  buttonPush = 1;  
  if (buttonPush == 1){ ...

are logically grouped together when indented like this at make it clearer that the "set then test" arrangement can never fail so the subsequent "else" where the LED is turned off is never implemented. So the LED must stay on.

enter image description here

buttonPush = 0; is bolded for unrelated reasons.
I had wondered if it was being set and never cleared and affecting the logical test, but as it is not tested outside the loop this is OK.

enter image description here

buttonPush = 0; is bolded for unrelated reasons.
I had wondered if it was being set and never cleared and affecting the logical test, but as it is not tested outside the loop this is OK.

The two adjacent lines

  buttonPush = 1;  
  if (buttonPush == 1){ ...

are logically grouped together when indented like this at make it clearer that the "set then test" arrangement can never fail so the subsequent "else" where the LED is turned off is never implemented. So the LED must stay on.

enter image description here

buttonPush = 0; is bolded for unrelated reasons.

added 297 characters in body
Source Link

This alone isSummary:

A fatal problem is caused by the two lines of code below.
A variable is set and then the same variable is immediately tested to see if it is set. This means that the IF test always passes and the ELSE never happens. As the LED is cleared only when the else occurs, the LED stays lit.

Code:

   buttonPush = 1;
   if (buttonPush == 1){ ...
  buttonPush = 1;  
  if (buttonPush == 1){ ...

When the button is pressed you set buttonPush and then immediately test to see if it is set.
As

As it always is set, the LED never gets turned off as the "else" condition is never met.

This alone is fatal:

   buttonPush = 1;
   if (buttonPush == 1){ ...

When the button is pressed you set buttonPush and then immediately test to see if it is set.
As it always is set, the LED never gets turned off as the "else" condition is never met.

Summary:

A fatal problem is caused by the two lines of code below.
A variable is set and then the same variable is immediately tested to see if it is set. This means that the IF test always passes and the ELSE never happens. As the LED is cleared only when the else occurs, the LED stays lit.

Code:

  buttonPush = 1;  
  if (buttonPush == 1){ ...

When the button is pressed you set buttonPush and then immediately test to see if it is set.

As it always is set, the LED never gets turned off as the "else" condition is never met.

added 80 characters in body
Source Link

This alone is fatal:

   buttonPush = 1;
   if (buttonPush == 1){ ...

When the button is pressed you set buttonPush and then immediately test to see if it is set.
As it always is set, the LED never gets turned off as the "else" condition is never met.

Even when that is fixed you have other issues.
I suggest that "expanding" the code along the lines I have shown in (3) below will help you immensely with visualising what your code does.

There are a large number of editors which handle this task in various ways, but just plain tabbing and white space with a basic editor helps muchly.


It seems almost certain that your code will NOT do what you intend when it does work "properly", but there are several issues below that need addressing (one fatal, others less so) - see "ISSUES" below.

At present it will not do any of these.
This alone is fatal:

   buttonPush = 1;
   if (buttonPush == 1){ ...

When the button is first pressed you set buttonPush and then immediately test to see if it is set. As it always is the LED never gets turned off as the "else" condition is never met.

Even when that is fixed you have other issues.
I suggest that "expanding" the code along the lines I have shown in (3) below will help you immensely with visualising what your code does.

There are a large number of editors which handle this task in various ways, but just plain tabbing and white space with a basic editor helps muchly.

Initially I thought that yourThis has been edited to match my main conclusion which was already covered above {} nesting(the problem was wrong,not nesting but on further examination found it was OKa test that can never fail - as covered above) but the formatting comments are relevant.
Your

Your code is written in a standard (or usual) manner. Some are able to handle such without problem, but I (and many others) find such layout immensely hard to read and, importantly, easy to bury errors in.
The The following is overdone - I used existing tabs and expanded it in Word :-) - but to me the following makes it immensely more readable. Layout and herestyle are a matter of personal preference, very usefullyand there are many opinions re what is "best", shows that the basicbut something which allows ease of visualisation of program decision structure is OK - SO the fundamental error must lie outside loop(). A mismatched data type then became an obvious thingliable to look for, although I did go looking for millis() related unsigned long assignments first as this is a common errorbe useful.

It seems almost certain that your code will NOT do what you intend when it does work "properly", but there are several issues below that need addressing (one fatal, others less so) - see "ISSUES" below.

At present it will not do any of these.
This alone is fatal:

   buttonPush = 1;
   if (buttonPush == 1){ ...

When the button is first pressed you set buttonPush and then immediately test to see if it is set. As it always is the LED never gets turned off as the "else" condition is never met.

Even when that is fixed you have other issues.
I suggest that "expanding" the code along the lines I have shown in (3) below will help you immensely with visualising what your code does.

There are a large number of editors which handle this task in various ways, but just plain tabbing and white space with a basic editor helps muchly.

Initially I thought that your {} nesting was wrong, but on further examination found it was OK.
Your code is written in a standard (or usual) manner, but I find such layout immensely hard to read and, importantly, easy to bury errors in.
The following is overdone - I used existing tabs and expanded it in Word :-) - but to me the following makes it immensely more readable and here, very usefully, shows that the basic program decision structure is OK - SO the fundamental error must lie outside loop(). A mismatched data type then became an obvious thing to look for, although I did go looking for millis() related unsigned long assignments first as this is a common error.

This alone is fatal:

   buttonPush = 1;
   if (buttonPush == 1){ ...

When the button is pressed you set buttonPush and then immediately test to see if it is set.
As it always is set, the LED never gets turned off as the "else" condition is never met.

Even when that is fixed you have other issues.
I suggest that "expanding" the code along the lines I have shown in (3) below will help you immensely with visualising what your code does.

There are a large number of editors which handle this task in various ways, but just plain tabbing and white space with a basic editor helps muchly.


It seems almost certain that your code will NOT do what you intend when it does work "properly", but there are several issues below that need addressing (one fatal, others less so) - see "ISSUES" below.

At present it will not do any of these.

This has been edited to match my main conclusion which was already covered above (the problem was not nesting but a test that can never fail - as covered above) but the formatting comments are relevant.

Your code is written in a standard (or usual) manner. Some are able to handle such without problem, but I (and many others) find such layout hard to read and, importantly, easy to bury errors in. The following is overdone - I used existing tabs and expanded it in Word :-) - but to me the following makes it immensely more readable. Layout and style are a matter of personal preference, and there are many opinions re what is "best", but something which allows ease of visualisation of program structure is liable to be useful.

added 1259 characters in body
Source Link
Loading
added 193 characters in body
Source Link
Loading
added 1038 characters in body
Source Link
Loading
added 1038 characters in body
Source Link
Loading
Source Link
Loading