esoteric terminology hindering ability to find answers on my own.

Hello,

To start I wanted to add that I have the utmost respect to technical forums, and I will do my best to not waste your time in any way. I, in reality have spent the better part of the last 48 hours searching for solutions on these forums. My issue stems from my inability to know what to search for. The most logical searches led to some better understanding of the programming, but have yet to yield the exact solution to my problem.

I am looking to start, and stop an LED array sequence being controlled by the Arduino via digital I/O’s acting as my PWM outputs. With the current (bad) program I am able to start the sequence with a SPST button. As you can see in the code, the code runs once at the button push, then returns to the “else” state of my crude, empty {}.

Though I am able to start the sequence via button push, I would like to essentially pause the sequence as is when the button is triggered again. Essentially keeping the LEDs frozen in the state they where in when the button was pushed.

Here is the current sketch I am playing with. I apologize if it is messy, or illogical.

int fadeout(int pin,float t)
{
  int i;
  for(i=255;i>=0;i-=1)
  {
    analogWrite(pin,i);
    delay(t);
  }
}
int fadein(int pin,float t)
{
  int i;
  for(i=0;i<=255;i++)
  {
    analogWrite(pin,i);
    delay(t);
  }
}
int Ppin1=3;
int Ppin2=5;
int Ppin3=6;
int Ppin4=9;
int Ppin5=10;
int Ppin6=11;

void setup()
{
  Serial.begin(9600);
}   

void loop()
   {
int i;
int buttonPin = 2;
pinMode(buttonPin, INPUT);

   if (digitalRead(buttonPin) == HIGH) 
  { float fadtime=15; 
  fadein(Ppin1,fadtime);  
  fadeout(Ppin3,fadtime);  
  fadein(Ppin2,fadtime);  
  fadeout(Ppin1,fadtime); 
  fadein(Ppin3,fadtime); 
  fadeout(Ppin2,fadtime); 
}
else 
{} // I would like to be able to use this area to stop the above loop, and the LEDs to be "paused" at the values they had during the moment the button was pushed. 
}

I do not wish to have any code written for me. I will say, If you are in the mood I am not going to stop you.
Though, more importantly, if you have a better way to phrase my question, and help me to help myself, I would really appreciate that.

Just to give as much info as I can, and avoid looking foolish for a simple oversight, here is my attempt at sudo-code.

Setup
int (everything I need to make this work)

End setup

(begin 9600)

End loop()

(PWM fade sequence)

If button is pressed,
{pause loop in current state}

Else
{Keep on looping (PWM fade sequence) }


I do realize I have the code is reversed in the actual code from what I want to do in my Sudo-code. I have yet to find how to write a pause, or stop loop command for a button push in the “IF” line.

If you have any insight on this matter, please let me know.

Thank you very much for your time.

-Mitsonga

First, a few things:

int Ppin1=3;
int Ppin2=5;
int Ppin3=6;
int Ppin4=9;
int Ppin5=10;
int Ppin6=11;

Consider using arrays for stuff like this. It allows you to condense your code down and plays right into what I'll talk about later.

int fadeout(int pin,float t)
{
  int i;
  for(i=255;i>=0;i-=1)
  {
    analogWrite(pin,i);
    delay(t);
  }
}

delay() takes an unsigned long variable type, not a float.

So now on to your issue...

There isn't a simple way to do this because of how delay() functions. When you put a delay() in your code, you're telling the processor to do nothing (except handle interrupts) until the delay finishes. That's a problem because we need to constantly check the button's state to determine if we need to "pause" or not. The first thing to consider is do you want the button to act as a toggle or a switch? Your wording tells me toggle, but your code tells me switch. If you want to push/release the button and have the sequence pause, then, at some later time, push/release the button to resume the action, you'll need a toggle. The StateChangeDetection example code helps teach you how to do that.

The problem then becomes re-implementing your code to not use delay(). The best advice I can give you is to study and fully understand the Blink Without Delay example. Don't just take a glance and try to copy it into your code. Really play with it and tweak things until you fully understand things. Only then can you try to embark on the great adventure of implementing it will non-trivial tasks (LED sequences).

Here is some psuedo-code for doing it without delay():

if button signal edge
  toggle pause flag

if not pause flag
    if it's been a certain interval since we last did something
        set the interval timer to now
        if we're going down
            decrease the analog value
        else
            increase the analog value
        it the analog value is at a certain level
            set the going down flag and pin to whatever is next in the sequence

As far as search terms go, the best I could recommend is button and led sequence.

This may help:

http://www.gammon.com.au/blink

Arrch is right, you have to lose the delay().

I sympathize, it's hard to search for concepts, especially when you aren't sure what it is. Just experimenting with stuff will help.