making function run once when its called regardless if initial condition isn't.

Hello everyone, hope all of you are safe during this time. I've come across a dilemma, currently, when certain condition are met, here:

  if (x == 0 && b == 0 && y == 0 && a == 0) // All buttons pressed
      {
      for (int ixx=0; ixx<4; ixx++)
      strip.setPixelColor(ixx, 255, 255, 255);
      strip.show();
      timer1 = millis();
      }
         else if (b == 0 && x == 1 && y == 1 && a == 1 && (ay <= 200)) // B + down
         {
           if (k == 0) {
                    k = 255;   }
               
            Charge(100); 
          }

when the else if is met, it calls the "Charge" function I created:

void Charge(uint8_t wait)
  {
  if (millis() - ktimer > 10) 
   {
         if (k > 0) {
             k = k - 1;
           }
      ktimer = millis();
  }

  if (millis() - timer > wait) 
  {
    switch (i) {
      case 0:
        strip.setPixelColor(0, k , k , 0);
        strip.setPixelColor(1, 0 , k , k);
        strip.setPixelColor(2, k , k , 0);
        strip.setPixelColor(3, 0 , k , k);
        break;
      case 1:
        strip.setPixelColor(0, 0 , k , k);
        strip.setPixelColor(1, k , k , 0);
        strip.setPixelColor(2, 0 , k , k);
        strip.setPixelColor(3, k , k , 0);
        break;
      case 2:
        strip.setPixelColor(0, k , k , 0);
        strip.setPixelColor(1, 0 , k , k);
        strip.setPixelColor(2, k , k , 0);
        strip.setPixelColor(3, 0 , k , k);
        break;
      case 3:
        strip.setPixelColor(0, 0 , k , k);
        strip.setPixelColor(1, k , k , 0);
        strip.setPixelColor(2, 0 , k , k);
        strip.setPixelColor(3, k , k , 0);
        break;
    }
    strip.show();
    if (j == 255) 
      {
        j = 0;
      }
    j++;
    timer = millis();
    i = (i + 1) % 4;
    //Serial.println(k);
  }
}

What this does is simply alternate between Cyan and yellow between the 4 Neo pixel Leds i have attached, moving it back and forth. The user would have to input those button press's for a split second, however the issue is that those inputs usually wont stay like that, essentially the else if condition would be no longer met, ceasing the function to be continually run.

Is there a way to make so that as long as its pressed once, it will run the function until k == 0?

I was trying to make it so that when its called, it will set k to 255 and run it until k gets to 0, which then exits the function. Im trying to make it run all the way ( 255 too 0 ) even if the initial condition isn't continuously met, until k gets to 0.

Another thing is I can not use delay with this program, as the arduino has to be able to be interrupted at any point.

Thanks for any help at all

unfortunately I can't post the entire code as it exceeds maximum allowed of characters for the forum. If it is needed i can private message it to you.

Thanks again

CLOUD_NODELAY.ino (11.4 KB)

It is perfectly acceptable and possible to add a program to a post as an attachment if it is too big to post.

aarg:
It is perfectly acceptable and possible to add a program to a post as an attachment if it is too big to post.

Got you, attached the code to the original post

aimmet:
Is there a way to make so that as long as its pressed once, it will run the function until k == 0?

The usual way to do that is to use the button press to update a variable and then the value of the variable will stay until something else changes it (perhaps when the activity is complete). Then the IF test for the activity checks the variable, rather than the button. Something like this pseudo code

buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {  // assumes LOW when pressed
   jobMayRun = true;
}

if (jobMayRun == true) {
   do something
   if (finished) {
      jobMayRun = false;
}

Another thing is I can not use delay with this program, as the arduino has to be able to be interrupted at any point.

Have a look at how millis() is used to manage timing without blocking in Several Things at a Time.

And see Using millis() for timing. A beginners guide if you need more explanation.

...R

Ah! that did the trick! Thank you