Need my code tweaked, will PayPal $25 to person who helps

Can somebody help me? I want the loop to run one time when the button is pushed one time. I have got to be doing something wrong. E-mail directly if interested. mark@laughingcamera.com

this_works_with_button.ino (1.33 KB)

If you had All warnings turned on in the IDE you would get this informative message:

C:\Users\Peter\AppData\Local\Temp\arduino_modified_sketch_591412\sketch_jun24b.ino: In function 'void loop()':

C:\Users\Peter\AppData\Local\Temp\arduino_modified_sketch_591412\sketch_jun24b.ino:22:27: warning: suggest braces around empty body in an 'if' statement [-Wempty-body]

   if (buttonState == HIGH); {   //run the loop

                           ^

The semicolon shouldn't be there.

Pete

Sorry but I do not understand what you mean by "If you had All warnings turned on in the IDE you would get this informative message:"

I tried it without the ; and still does not run when I press the button.

I presume you are using the IDE and not the online Create thingy.
In the File menu, select Preferences. In the dialog which pops up in "Show verbose output during" select "compilation" and just under that select "Compiler Warnings" All.
Then you may get extra warning messages during compilation. These warnings are often just as important as a compiler message. In the case of the warning message I posted, that semicolon definitely shouldn't be there.

If the code still isn't working, at least removing the semicolon is progress :slight_smile:

You would usually use INPUT_PULLUP instead of INPUT when setting the pinMode of a button. This will hold the pin HIGH until the button is pushed, in which case you need to look for LOW instead of HIGH.

In setup do this:

  pinMode(buttonPin, INPUT_PULLUP);

and in loop do this:

  if (buttonState == LOW) {   //run the loop

Pete

I am using the Mac desktop version and that choice is not in the preferences. Will try your suggestion. Thank you.

Just got the answer from a forum member. Thanks for the help folks and pointing me in the correct direction.

So, what was the solution?

What a shame - I was looking forward to that $25.

I bet he had the sense of his button the wrong way around.