Pages: [1]   Go Down
Author Topic: Tasks with no delay  (Read 1067 times)
0 Members and 1 Guest are viewing this topic.
Colorado
Offline Offline
Edison Member
*
Karma: 47
Posts: 1562
Reviving dead brain cells with Arduinos.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm trying to figure out how to get this working.  What I'd like to create is a light display, where by pressing a button, the sequence changes to something else.  I realized that I can't use delay() calls within the code because reading the button will then be delayed while one of the routines is running.  So, I started small and doing things one step at a time, but now I'm stuck.  This is what I have so far:
Code:
int blinker = 12; // Phase indicator
int inPin = 13;   // Button (and builtin LED)

int previous = HIGH;
int reading;
int buttonCounter = 0;
int pause;

// Set led pins
int led1 = 2;
int led2 = 3;
int led3 = 4;
int led4 = 5;
int led5 = 6;
int led6 = 7;
int led7 = 8;
int led8 = 9;
int led9 = 10;
int led10 = 11;
int p;

long time = 0;
long debounce = 200;  // debounce time
long prevMillis = millis();
long currentMillis = 0;

void setup()
{
  // Set button pin to INPUT
  pinMode(inPin, INPUT);
  // Set all LED pins to OUTPUT
  for (p = 2; p <= 12; p++) {
    pinMode(p, OUTPUT);
  }
}

void blinkLed(int buttonCounter) {
  switch(buttonCounter) {
    case 1:
      // Phase one
      pause = 1000;
      break;
    case 2:
      // Phase two
      pause = 500;
      break;
    case 3:
      // Phase three
      pause = 333;
      break;
    case 4:
      // Phase four
      pause = 250;
      break;
    default:
      // Turn everything off
      pause = 0;
      digitalWrite(blinker, LOW);
      break;
  }
  if ((millis() - prevMillis > pause) && buttonCounter != 0) {
    digitalWrite(blinker, digitalRead(blinker) == HIGH ? LOW : HIGH);
    prevMillis = millis();
  }
}

void loop()
{
  currentMillis = millis();
  reading = digitalRead(inPin);

  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    time = millis();
    buttonCounter++;
    if (buttonCounter == 5) {
      buttonCounter = 0;
    }
  }
  blinkLed(buttonCounter);
  previous = reading;
}
This runs as expected.  When the script first runs, nothing happens.  Pressing the button once, and the blinker led blinks once a second.  Pressing the button again (which increases its counter), will have the LED blink twice a second.  Press the button again and we're at 3 times a second, and press it a fourth time and it's now blinking 4 times a sec.  One more press and the counter resets to 0, and everything turns off.

Great, but now ...  instead of having a blinker led, I want to have a bunch of LEDs doing something:

buttonCounter = 1: LEDs should chase each other, slowly, then pause (remain off) for a set amount of time before cycling.
buttonCounter = 2: LEDs should do a reverse chase ...
buttonCounter = 3: LEDs should do something else ...
etc., etc.

However I need to keep in mind that one might push the button while a sequence is running, so I can't use delay() calls within a sequence.

Can anyone give me some direction here?
Logged

Papendrecht, NL
Offline Offline
Newbie
*
Karma: 1
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Study the blink without delay tutorial: http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay.
Logged

Colorado
Offline Offline
Edison Member
*
Karma: 47
Posts: 1562
Reviving dead brain cells with Arduinos.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have, which is how I came up with the above code ... but I don't have the foggiest when it comes to implementing separate routines that run without delays.  For example, for phase one, I want this routine to run:
Code:
  for (p = 2; p <= 11; p++) {
    if (digitalRead(p) != HIGH) {
      // This is here so we don't set a pin HIGH when it already is
      digitalWrite(p, HIGH);
    }
    delay(50);
    if (p < 11) {
      digitalWrite(p+1, HIGH);
    }
    delay(50);
    if (p < 10) {
      digitalWrite(p+2, HIGH);
    }
    delay(50);
    digitalWrite(p, LOW);
  }
  delay(random(10000, 30000));
As you can see, there are several delay() calls in there.  I can't figure out how to make it so I don't have those.
Logged

Lancashire, UK
Offline Offline
Edison Member
*
Karma: 9
Posts: 1991
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I have the time critical stuff in the main loop and have a unsigned long variable updated with millis() each time round (time in this example).  Then I keep an unsigned long variable for each thing I want to happen  after a set time.   Then its a series of if then statements of the form :

Code:
if (time >= qtrhrloop){
    qtrhrloop = qtrhrloop + 900000;
    save_eeprom();
    touchpadvaluemin = 25000;
  } // end qtrhrloop 

This one runs every 900000 milliseconds (15 minutes to you)  and runs a function to save stuff to eeprom and resets a value of a trigger variable.  I have a whole series of them that do stuff at different times, some of them are at variable time if the value added to the loop variable (qtrhrloop in this case) has differnt values added to it depending on other conditions.  The purists will throw up their hands in horror and whinge about the overrun of millis() every  50 days or so, in practice you won't notice.
Logged


Papendrecht, NL
Offline Offline
Newbie
*
Karma: 1
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Drop the for loop. Do something like this:

Code:
...
void loop() {
   long time = millis();

   if (time - prevTime >= duration) {
      ...
      prevTime = time;
   }

   if (time - ledTime >= ledDuration) {
      toggleLed(ledIndex++);

      if (ledIndex > 6) {
        ledIndex = 0;
      }

      ledTime = time;
   }
}

void toggleLed(int led) {
  ...
}

You can repeat this constuction as many times as needed.
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 70
Posts: 3726
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You can do it with some static data and a bit more state. Use your button detection as you did in the original sketch. Declare an array of bytes and initialize it with some numbers that represent successive states that you'd like your LEDs to cycle through. So, for example, if you have three LEDs, there are eight different combinations from off off off to on on on. Set your array up with say {1,2,3,8,0}. This could represent led 1 on, led 2 on, led 3 on, all on, all off as a sequence. Once a button press is detected, set an index variable to 0. Set the leds on according to the value in the array the index is pointing to. On every subsequent call to loop, if enough time has passed, increment the index, grab the next value from the array and set the leds to the new pattern it represents. If the value is zero, stop incrementing and wait for another button press.

You can use bitwise operations to extract the information you need from the values in the array to decide which LEDs to turn on, or code it explicitly in a switch statement.

Finally, using 0 as a terminator means the array can hold multiple sequences. You just need to decide where to set the index to start when you detect a button press.
Logged

Nova Scotia
Offline Offline
Full Member
***
Karma: 4
Posts: 204
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey KirAsh4,

Could you post the working code that you are happy with? I'm having the same problem with my project.
Logged

Manchester, New Hampshire
Offline Offline
Edison Member
*
Karma: 4
Posts: 1363
Propmaker
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here is the skeleton code I start my projects with:

Code:
const byte DEBUG = 0;

// Timing:
 
  unsigned long time;

// Functions:

  void setup();
  void loop();

 
  void setup() {
   
    time = millis();
   
    if (DEBUG == 1) { Serial.begin(115200); }
   
  }

 
  void loop() {

    // Variables:
   
      static unsigned long oldTime;
      int timeDelta;           
      float timeDeltaSec;   


    // Timing:
   
      oldTime = time;                            // Store start time of last update.
      time = millis();                           // Get current system time.
      timeDelta = time - oldTime;                // Calculate how long last update took, in milliseconds.
      timeDeltaSec = float(timeDelta) / 1000.0;  // Convert last update period into seconds.
   

   // Main loop here.


    if (DEBUG == 1) {
    }
   
  }


If you want an event to occur in 100 milliseconds, record the time it began and store it in a unique static or global variable.  Then compare that time with the current time.

TimeDelta and TimeDeltaSec tell you how long the last run through the main loop took in milliseconds and seconds.  If you want to move a servo at 90 degrees per second for example, you would have a float which contains the angle of the servo, and you would add 90*TimeDeltaSec to it each time through the loop.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

KirAsh4, in the code sample in your first post, you have a variable buttonCounter and a function that uses switch/case on that variable to set a different delay before changing the LEDs, and when the timeout expires, buttonCounter advances from 1, 2, 3, 4, back to 1, 2, 3, etc. This is very good, a basic example of a finite state machine that advances to a new state every time the button is pressed. Now, imagine a few changes to your code.

Change #1: Rename the "buttonCounter" variable to "state" or "animationState" or something -- pretty soon it won't only be counting button presses.

Change #2: Right now your switch/case is running every single time blinkLeds is called. Right now that's a harmless waste, but after Change #3 and Change #4 it will be a real problem. Move the entire switch block inside of the if (millis() - pause > prevMillis) if block. That will make it so that after running through the switch block, it won't run again until after the next delay finishes.

Change #3: Move the code that does "digitalWrite(blinker, digitalRead(blinker) == HIGH ? LOW : HIGH)" into each state -- or move it into a function and then call that function from each state. This seems like you're repeating yourself, but later on you'll make it so that each state isn't just blinking a single LED -- instead, each state will set a pattern of LEDs according to whatever animation you design.

Change #4: Make it so that a button press isn't the only thing that changes the state variable -- at the end of each state, right before the "break;", each state can set a new next state.

If you don't add any next-state assignments right away in Change #4, you should have code that still acts just like your original sample, even though it will be in a very different shape. But now there's something new you can do: have more states than button presses. For example, maybe State 1 goes to State 2 goes to State 3 which goes back to State 1, and they turn on three LEDs in a cycle. But when you press the button it will jump to State 4, which goes to State 5, which goes back to State 4. And when you press the button again it will go to State 6, which does something different. So each "pattern" can be multiple states that will keep repeating until you press the button and jump into a different pattern.

By adding logic inside of the states you can also make them more complicated -- you don't need to have one state for every combination of LEDs you want to light up. For example you could have a row of 8 LEDs with the lit LED "bouncing" back and forth with only two states. State 1 would do

Code:
case 1: /* Move right */
    Turn off LED n;
    n = n + 1;
    Turn on LED n;
    if (n == 8) {
      state = 2; /* Start moving left instead */
    } /* Else keep moving right */
    pause = 250; /* Or whatever */
    break;

And state 2 would do
Code:

case 2:
    Turn off LED n;
    n = n - 1;
    Turn on LED n;
    if (n == 1) {
        state = 1; /* Start moving right */
    } /* Else keep moving left */
    pause = 250; /* Or whatever, again */
    break;

So instead of the states being 1, 2, 1, 2, 1, 2, 1, 2, they'd be 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, ... 1 every time it moves right, and 2 every time it moves left. And the logic inside of the states changes between 1 and 2 when it's needed, saving you a bunch of code.
Logged

Colorado
Offline Offline
Edison Member
*
Karma: 47
Posts: 1562
Reviving dead brain cells with Arduinos.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the suggestions hobbified.  I'm slowly making the changes you suggested, one step at a time.  I changed the variable name buttonCounter to stage, though to me that's just a name.  I moved the switch case inside of the time check and I had to make a small adjustment otherwise stage 0 would never be read.  I also removed stages 3 and 4 for now.  Eventually I will add them back in, but for the purpose of this exercise, they won't make any difference.  Now I have this code:
Code:
int blinker = 12; // Phase indicator
int inPin = 13;   // Button (and builtin LED)

int previous = HIGH;
int reading;
int stage = 0;
int pause;

int p;
int n = 1;

long time = 0;
long debounce = 200;  // debounce time
long prevMillis = millis();
long currentMillis = 0;

void setup()
{
  // Set button pin to INPUT
  pinMode(inPin, INPUT);
  // Set all LED pins to OUTPUT
  for (p = 2; p <= 12; p++) {
    pinMode(p, OUTPUT);
  }
}

void blinkLed(int buttonCounter) {
  if (millis() - prevMillis > pause) {
    if (stage != 0) {  // Having this in the above line causes stage 0 to never be read
      digitalWrite(blinker, digitalRead(blinker) == HIGH ? LOW : HIGH);
    }
    switch(stage) {
      case 1:
        // Phase one
        digitalWrite(n, LOW);
        n++;
        digitalWrite(n, HIGH);
        if (n == 12) {
          n = 1;
        }
        pause = 250;
        break;
      case 2:
        // Phase two
        digitalWrite(n, LOW);
        n--;
        digitalWrite(n, HIGH);
        if (n == 1) {
          n = 12;
        }
        pause = 250;
        break;
      default:
        // Turn everything off
        pause = 0;
        for (p = 2; p <= 12; p++) {
          digitalWrite(p, LOW);
        }
        break;
    }
    prevMillis = millis();
  }
}

void loop()
{
  currentMillis = millis();
  reading = digitalRead(inPin);

  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    time = millis();
    stage++;
    if (stage == 3) {
      stage = 0;
    }
  }
  blinkLed(stage);
  previous = reading;
}
Great, this works as you described it would:
stage == 1: single LED chase from one end to the other
stage == 2: single LED chase going the opposite direction
stage == 0: everything turns off

However, here are my issues with this:
a) That n variable.  I had to start n = 1 because of the way your code is written.  You set a pin LOW first, increment n, then set it HIGH.  That means, if I start n = 2 (which would represent where my first LED is), it would never light up that first LED.  So I set it to 1.  However, that now means that I'm setting pin 1 LOW before moving on.  That doesn't seem right, especially not if I plan on using it later.

b) The second problem is again on that n variable.  It's being used in both stages without a reset.  So if the button is pushed while it's cycling the first stage, the next one will start where n left off.  It should reset so that stage 1 always starts at the first LED, and stage 2 always starts at the last LED.

c) It's a single LED chase.  In my second code sample, you'll see that I'm turning on n, n+1, n+2 then turn them off again.  This results in a 3 LEDs chase.  And they work per cycle, so if I were to write it out, I get a table that shows the relation between the value of n (if I start at 0) and which LEDs should be on
Code:
Value of n - LEDs to light up
    n =  0 -  2
    n =  1 -  2,  3
    n =  2 -  2,  3,  4
    n =  3 -  3,  4,  5
    n =  4 -  4,  5,  6
    n =  5 -  5,  6,  7
    n =  6 -  6,  7,  8
    n =  7 -  7,  8,  9
    n =  8 -  8,  9, 10
    n =  9 -  9, 10, 11
    n = 10 - 10, 11, 12
    n = 11 - 11, 12
    n = 12 - 12
This same thing would apply to stage == 2 as well, only in reverse.

d) And the last one (for now), there is no pause between a full cycle.  What I'd like it to do is when a stage starts, it cycles through all the LEDS, so a complete chase, then pause for anywhere between 10 to 30 seconds - it's at the bottom of my second example code as well, a random delay.  Then starts up again.  I don't want a continuous chase all the time.
Logged

Colorado
Offline Offline
Edison Member
*
Karma: 47
Posts: 1562
Reviving dead brain cells with Arduinos.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey KirAsh4,

Could you post the working code that you are happy with? I'm having the same problem with my project.
Here's a break down of what I ended up doing, which works great for what my daughter wants since this is going on a Lilypad, sewn onto a jacket. What started small quickly grew once she saw what the possibilities were. You will have to adjust to your liking of course.

Project goals:
- have a predefined number of LEDs repeat a specific blink pattern (with a random pause between each cycle)
- have several patterns available, selectable through a single push button which cycles through patterns
- have a pattern indicator (the LED display is out of line of sight, so the user needs an indicator near the button)
- Ability to add more patterns later

First I was only using digital pins but then she asked to add more LEDs so I decided to use the analog pins as well - anything after this and I would have to use a shift register (I think).

So, using all available pins, I created an array of alternating pins, using the Lilypad's + and - pins as the divider:
Code:
int myPins[] = {5, 4, 6, 3, 7, 2, 8, A5, 9, A4, 10, A3, 11, A2, A0, A1};
If you look at a Lilypad layout (http://www.sparkfun.com/products/9266), imagine a dividing line that runs between pins 4 and 5 through to pins A0 and A1. So the first half, from left to right, would be pins 5, 6, 7, 8, 9, 10, 11, and A0, while the other half are pins 4, 3, 2, A5, A4, A3, A2, and A1.

Pins 12 and 13 are used for the pattern indicator and button respectively. I specifically wanted the button on pin 13 because I used that as the button indicator itself (the on board LED would light up when you depress the button.)

When I wired this up on my Uno, I put all green LEDs on one half, and all red LEDs on the other half so I can get a good visual of what's happening. I refer to these as the left and right sides.

The bare minimum for code became:
Code:
int blinker = 12; // Phase indicator
int inPin = 13;   // Button (and builtin LED)
// The sequence of pins is for the Lilypad, splitting in in half at the battery poles
// Bottom half are pins 5, 6, 7, 8, 9, 10, 11, A0
// Upper half are pins 4, 3, 2, A5, A4, A3, A2, A1
int myPins[] = {5, 4, 6, 3, 7, 2, 8, A5, 9, A4, 10, A3, 11, A2, A0, A1};

int previous = HIGH;
int reading;
int stage = 0;
int pause;
int p;
int resetDone = 0;
int blinkerPause = 300;
int counter = 0;

long time = 0;
long debounce = 200;  // debounce time
long prevMillis = millis();
long currentMillis = 0;
long blinkerMillis = 0;

void setup() {
  // Set button pin to INPUT
  pinMode(inPin, INPUT);
  // Set blinker LED
  pinMode(blinker, OUTPUT);
  // Set all LED pins to OUTPUT
  for (p = 0; p < 16; p++) {
    pinMode(myPins[p], OUTPUT);
  }
}

void resetAll() {
  pause = 0;
  for (p = 0; p < 16; p++) {
    digitalWrite(myPins[p], LOW);
  }
}

void blinkLed(int stage) {
  if (stage != 0) {
    if ((millis() - blinkerMillis > blinkerPause) && (digitalRead(blinker) == LOW)) {
      digitalWrite(blinker, HIGH);
      blinkerMillis = millis();
    }
    if ((millis() - blinkerMillis > 1) && (digitalRead(blinker) == HIGH)) {
      digitalWrite(blinker, LOW);
      blinkerMillis = millis();
      counter++;
      if (counter >= stage) {
        blinkerPause = 1000;
        counter = 0;
      } else {
        blinkerPause = 300;
      }
    }
  }
  if (millis() - prevMillis > pause) {
    switch(stage) {
      case 1:
        // Phase one
        break;
      case 2:
        // Phase two
        break;
      case n-th:
        // Phase n-th
        break;
      default:
        // Turn everything off
        pause = 0;
        resetAll();
        digitalWrite(blinker, LOW);
        break;
    }
    prevMillis = millis();
  }
}

void loop() {
  currentMillis = millis();
  reading = digitalRead(inPin);

  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    time = millis();
    stage++;
    switch(stage) {
      case 1:
        // Set variables pertaining to Phase one
        counter = 0;
        break;
      case 2:
        // Set variables pertaining to Phase two
        counter = 0;
        break;
      case n-th:
        // Set variables pertaining to Phase n-th
        counter = 0;
        break;
      default:
        // Turn everything off
        stage = 0;
        break;
    }
  }
  blinkLed(stage);
  previous = reading;
}
What that does is this:
a) sets array pins and blinker pin to OUTPUT, and pin 13 as INPUT for the button
b) sets the blinker rate outside of the switch/case routines so that it continues to blink at a set interval, regardless of the pause on the loop itself (more on this later)
c) allows me to set variables outside of the main switch/case inside of the loop() function
d) allows me to reset the full array and add global variables

So what happens is this, Lilypad (or Uno) comes on and does nothing. Depress the button once and you will get a visual indicator by form of the on board LED lighting up when you depress the button, and the blinker starts to blink once per second (set from stage) indicating it's in Phase one, or pattern one. What that pattern is gets defined in the blinkLed() switch/case. I'll list an example later.

Depress the button again and you increment the stage counter. This sets the stage for the switch/case which also determines how many times the blinker goes off and the blinkLed() function now only triggers whatever is in the 2nd switch/case, etc., etc.

I had to move some of the variables outside of the resetAll() function and into the main loop() function because otherwise the switch/case inside of blinkLed() would get confused when I call the resetAll() function multiple times within the same loop.

So now an example of what would go inside of the blinkLed() function, let's say case 1:
Code:
       // Ascending chase, both sides
        if (resetDone != 1) {
          resetAll();
          resetDone = 1;
        }
        if (millis() - startMillis > longPause) {
          pause = 50;
          if (n < 16) {
            digitalWrite(myPins[n], HIGH);
          }
          if (n > 2) {
            digitalWrite(myPins[n - 3], LOW);
          }
          n++;
          if (n == 19) {
            n = 0;
            startMillis = millis();
            longPause = random(shortPauseInt, longPauseInt);
          }
        }
This does a 3 LED chase from one end to the other end of the array. It starts off resetting the whole array in case the button was depressed while it was finishing a previous pattern. Then it does a time check against 'longPause'. That 'longPause' is set at the end of a chase and is a random number between whatever you want it set to. In my case, I choose 10 to 30 seconds so I have the following defined:
Code:
int shortPauseInt = 10000;
int longPauseInt = 30000;
It also gets reset to 0 whenever the stage changes, so the next stage doesn't sit there and wait for 'longPause' to timeout.

Then the routine just runs through the array to turn on or off the LEDs, taking into account that I'm leaving up to 3 of them on at any given time, except for the first and last 2 cycles where there are only one and two LED lit up (reversed at the end), so I had to check for what the value of 'n' is so that I don't try to set a non-existing pin HIGH or LOW.

Now, copy and paste that same piece of code inside of the case 2 loop and you get the same thing, except your blinker now blinks twice then pauses for a second. What you put in each switch/case is up to you.  I have different patterns defined:
phase 1: ascending three led chase (both sides)
phase 2: descending three led chase (both sides)
phase 3: ascending followed by descending chase (both sides)
phase 4: sequentially turn all LEDs on then off again (go up then down, both sides)
phase 5: random blinking (both sides)
phase 6: chase going up one side and down the other
phase 7: opposite of phase 6
default: everything off

Depressing the button will simply cycle through those patterns, so you can set it to one of them and leave it there till you either change it, or the Arduino loses power and/or gets reset.

Now, with phase 5, I realized I had to pull some variables out of the resetAll() function and into a separate switch/case in the main loop(). Reason being I was blinking for a specific amount of blinks (50 blinks) and when it reaches that number, it resets the whole array back to off and goes into its longPause. If I left the counter inside of the resetAll() function, it will cause problems when resetAll() is called in the middle of the blinker doing its thing - remember, it's doing it regardless of what the pattern loop is doing. Adding additional patterns means extending the switch/case in both the main loop() and the blinkLed() function.

I'm sure there are plenty of folks on here that will find short comings with my code. Nothing helps me better than receiving input from others, especially if it makes it better. As long as people can remember, this works for us and does what we want. Sorry hobbified, this throws your Change #4 right out the window. smiley-mr-green

0miker0, I hope this helps you out.
Logged

Colorado
Offline Offline
Edison Member
*
Karma: 47
Posts: 1562
Reviving dead brain cells with Arduinos.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

By the way folks, I noticed 9500 characters in preview mode isn't the same as 9500 characters per post.  In preview mode I had reached the 9500 limit and editing to bring it down (and it stopped giving me the warning.)  When i went to post, it told me I was over.  I had to remove another 100 characters (roughly, wasn't exactly counting them.)  So it seems the posting limit is actually lower than the preview limit.

One thing I had to remove was the following bit: I realized after the fact that I can move the counter reset outside of the switch/case inside of the main loop(). I was doing this in small steps and adding bits and pieces as I went along. Once done, I will go back over the code to optimize it. Stuff that gets repeated a lot will get consolidated.

One thing I keep wondering is moving the patterns into their own functions, then perhaps I can implement hobbified suggestion, but in a slightly different way:
Code:
switch(stage) {
  case 1:
    // run pattern 1 followed by pattern 2
    pattern_one();
    pattern_two();
    break;
  case 2:
    // run pattern 4 followed by pattern 1
    pattern_four();
    pattern_one();
    break;
  default:
    resetAll();
    break;
}
But, again, at this point in the game, it does what we want it to do, and the kid is happy. It might sit like this for a week, or 6 months, before we decide to change it again, who knows.
Logged

Pages: [1]   Go Up
Jump to: