loop while button is pressed and stop on release

hi everyone,

this i my very first sketch and hopefully you can help me to get further.

i am using an arduino leonardo, a led (D11), a push button(D2) and some wires.
everything works fine except that the for-loop wont’t stop on releasing the push button. after the push button got pressed the for-loop just runs to the end. so, i want the loop go through while the push button is pressed. and i want the loop to stop as soon as the push button will be released.

do you have any clever solution? i guess you have ;D

greetings
0 8 15 :confused:

    #include <Mouse.h>
     
        int led_pin = 11;
        int button_pin = 2;
       
        int button_state = LOW;
       
        int arr[] = {0,1,2,3,4,5};
     
        void setup(){
          pinMode(led_pin, OUTPUT);
          pinMode(button_pin, INPUT_PULLUP);
        }
     
     
        void loop(){
          button_state = digitalRead(button_pin);
         
          if (button_state == LOW) {
              digitalWrite(led_pin, HIGH); // LED on
             
              Mouse.begin();
                  for (int i = 0; i < 6; i++) {
                      Mouse.move(arr[i], arr[i], 0);
                      Serial.println(arr[i], arr[i]);
                      delay(200);
                      }
                      Mouse.end();
              }
         
          else {
              digitalWrite(led_pin, LOW); // LED off
          }
        }
for (int i = 0; i < 6 && button_state == LOW; i++) {

A FOR loop (and also WHILE) is a blocking construct - it runs to completion before anything else can happen.

In your FOR you are not checking the state of the button so it will not know that the button has been released. You could read the button within the FOR and then use break to exit from the FOR prematurely.

However my preference would be to replace the FOR with an IF and allow loop() to do the repetition.

...R

PaulRB:

for (int i = 0; i < 6 && button_state == LOW; i++) {

unfortunately that did not work ;( the for-loop still loops while the push button is not pressed.

Robin2:
A FOR loop (and also WHILE) is a blocking construct - it runs to completion before anything else can happen.

In your FOR you are not checking the state of the button so it will not know that the button has been released. You could read the button within the FOR and then use break to exit from the FOR prematurely.

However my preference would be to replace the FOR with an IF and allow loop() to do the repetition.

…R

okay, i understand the part regarding the for-loop. but could you explain the part with the if-statement more detailed please. that would be great - many thanks in advance.

greetings
0 8 15 :confused:

0815:
unfortunately that did not work ;( the for-loop still loops while the push button is not pressed

Sorry, my bad.

for (int i = 0; i < 6 && digitalRead(button_pin) == LOW; i++) {

PaulRB:
Sorry, my bad.

for (int i = 0; i < 6 && digitalRead(button_pin) == LOW; i++) {

great! many thanks!

0815:
okay, i understand the part regarding the for-loop. but could you explain the part with the if-statement more detailed please. that would be great - many thanks in advance.

Have a look at how the code is organized in Several Things at a Time and in Planning and Implementing a Program

Note that none of the functions takes very long to do a single run-through but they may need to be called many times before their task is complete.

…R

is there any possibility to stop the for-loop after it looped once? but while the button is still pressed.

many thanks in advance.

greetings
0 8 15 :confused:

0815:
is there any possibility to stop the for-loop after it looped once? but while the button is still pressed.

Since a for loop is a blocking construct, that would be "throwing good money after bad" as the saying goes.

Mouse.end();
while (digitalRead(button_pin) == LOW);

As Robin has pointed out, making “quick fix” code changes like these could cause problems when you want to make more changes later.

0815:
is there any possibility to stop the for-loop after it looped once? but while the button is still pressed.

Only use a FOR loop when you do not need to interrupt it.

If you need to be able to intervene then do not use FOR. Just allow loop() to do the iterations and use IF to make decisions.

...R

many thanks for your answers. i really try to understand...

so, even if the loop already went through the whole array? i just want the for-loop to loop once while the button is pressed. afterwards the for-loop should stop... even if the button is still pressed. and there should be a new button press to start the for-loop again.

how to get the same result using if-statements? imagine i would have a very huge array to run through :o isn't it a shitload work? or am i wrong?

greetings
0 8 15 :confused:

0815:
how to get the same result using if-statements? imagine i would have a very huge array to run through :o isn't it a shitload work? or am i wrong?

I don't understand why you think there would be more lines of code?

The loop() function will do the repetition so you just need a line of code to increment a counter and a line of code to test when it gets to a certain value.

Post the code that you think is necessary and then I should be better able to understand what you are thinking.

...R