Go Down

Topic: Stopping Code (Read 1 time) previous topic - next topic

accel

I have a quick question,

I am programming an RGB Led strip and I have already coded many effects. I am trying to integrate a switch into a project box to switch between effects.

I have included my code below but I cannot figure out for the life of me (coder's block), how to stop the execution of one of the effects based on the state of a switch.

Any ideas???


James C4S

while(digitalRead(pin) == HIGH);

That will cause the while() loop to effectively "stop" any other code from executing, while the "pin" is a HIGH.
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

accel

The issue is that most of the processing is done in while or for loops within each method. So each effect will take anywhere from <1 sec to 3 minutes to execute. Does anyone have any ideas how to stop these methods mid-execution. Do I have to recode the effects in order to do this?

Arrch


The issue is that most of the processing is done in while or for loops within each method. So each effect will take anywhere from <1 sec to 3 minutes to execute. Does anyone have any ideas how to stop these methods mid-execution. Do I have to recode the effects in order to do this?

Because there are delays in those while and for loops, you can't stop them. You'll need to rewrite the code to exists without them. Take a look at the Blink without delay example and try and build from those concepts.

accel

OK Something to look into. Basically, what I want to do is have to code periodically check to see if the switch is high (like every 200 ms), and if it is, stop the execution.

Is this possible? With interrupts or timers?

Arrch

Interrupts just temporarily interrupt the code, after it's done, execution is returned to the same spot, riddled with delays. You could put a bunch of reads throughout your loops and exit if it evaluates to true, but that could really much up the code and make it very convoluted. Getting a delayless pattern working is the better way to do it.

accel

I have looked at the code and I understand that I pretty much have to make a timer where (if (last value) + (delay) > (current value)) do this; The problem I still have is that the way that i wrote the code (and the only way that I can currently think of), it uses for loops and nested while loops within the code to write to the strip. How can I use this type of program with a delay-less architecture without having to wait until the end of the effect. I understand that I will probably have to rewrite the effects, I just am not quite sure how yet.

Thanks

Patrick

Arrch


I have looked at the code and I understand that I pretty much have to make a timer where (if (last value) + (delay) > (current value)) do this; The problem I still have is that the way that i wrote the code (and the only way that I can currently think of), it uses for loops and nested while loops within the code to write to the strip. How can I use this type of program with a delay-less architecture without having to wait until the end of the effect. I understand that I will probably have to rewrite the effects, I just am not quite sure how yet.

Thanks

Patrick

Here's an example:

Given a pattern (light 6 leds in sequence) that utilizes delays:
Code: [Select]
int pins[6] = {4,5,6,7,8,9};
...
void loop() {
  for (int i=0; i<6; i++) {
    digitalWrite( pins[(i==0?5:i-1)], LOW): // Previous LED off
    digitalWrite( pins[i], HIGH); // Current LED on
    delay(250);
  }
}


can be converted to...

Code: [Select]
int pins[3] = {4,5,6,7,8,9};
...
unsigned long change=0;
void loop() {
  static int i=0;
  unsigned long now = millis(); // current time
  if (now - change > 250) { // it's been over 250 ms
    change=now; // reset last time we changed
    i++; // next LED
    if (i>=6) i=0; // go back to zero if we reached the end of array
  }
  digitalWrite( pins[(i==0?5:i-1)], LOW): // Previous LED off
  digitalWrite( pins[i], HIGH); // Current LED on
}


Untested, but should give you an idea on how to start.

Udo Klein

Well, me being lazy I used the RESET switch for toggling effects: http://blog.blinkenlight.net/experiments/counting-resets/multiple-modes/. Reason: I already had used all other pins for driving LEDs and the reset switch is always in place anyway. As a sideeffect the computer can switch effects by triggering a reset through the serial connection.
Check out my experiments http://blog.blinkenlight.net

bperrybap


Well, me being lazy I used the RESET switch for toggling effects

Interesting. I have an RGB led bulb that does something similar.
They use power up/down for "programming" vs a reset or a pin input as there
is no interface to a light bulb.
The one additional thing that they did is add a timeout to save the desired sequence.
So you have to turn the device off within so many seconds of powering it up to alter the sequence
and then the new sequence is only saved after some longer amount of time.
This avoids quick power cycles from altering the programmed sequence.

--- bill

Udo Klein

Quote
The one additional thing that they did is add a timeout to save the desired sequence.


Not to hard to do in C but very hard if using the Arduino IDE + Bootloader.
Check out my experiments http://blog.blinkenlight.net

Go Up