Click button "if statement"

Hello,
I'm don't post on forums much. I normally find answer on previous forums, yet I can't find my answer. I have some basic knowledge about coding Arduino. I'm not going to post my code but I need an example from you guys.

So in my setup I have push buttons that connect when pushed and disconnect when released.

My code will run the "if statement" if I hold down then button. But I want it to run the if statement all the way through just on a simple click of a button.

I also have another button that I want to be on a loop until another button is pressed. But I think I can figure that out.

I just need to know the right code example for if a click button is pushed to run the entire if statement.

I just have the "else" on return;

Welcome to the forum

It is not clear what you are asking for

If a test returns true then the whole code in the following code block will be executed whether that is a single line of code or many lines

So what constitutes a code block ?

In some programming languages a code block is defined by its lines of code being tabbed to the same level but not in C++, which is what the Arduino uses

The safest thing to do is to put the dependant code block in a pair of curly braces like this

if (this == true)
{
  //execute the code here
}

If that is not what you are asking then please post an example sketch illustrating your problem

I’d suggest you check the code for one of the numerous button library such as Button in easyRun or OneButton or Toggle or EasyButton or Bounce2, ...

You’ll see how buttons are handled or just pick one of those library and use it.

1 Like

The if statement is..

If (ButtonState == HIGH) {}

the code will run if I hold the button down and stop when I release.

But it is a click button I need an example to click the button once without holding it down. And run the entire "if statement"

That’s what button libraries do
Or study the state change detection examples from the IDE

you might benefit from studying state machines. Here is a small introduction to the topic: Yet another Finite State Machine introduction

1 Like

I'll check it out

No, it will complete all the code lines between the { and } even if you release the button during that time.

2 Likes

That's what I thought. But not sure what is happening then. I'll do some tests today and get back with you

As you said

We can’t help…

An example has been provided. If you still need help you must post your code

I figured it out you guys. The delay function I was using was causing it...

I want to thank J-M-L .

You provided links to help a lot.

I learned a lot the last couple days and I want to thank everyone that posted possible solutions and all the links. My brain :brain: is a lot smarter now.

Thank you everyone.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.