Pages: [1]   Go Down
Author Topic: Stopping Code  (Read 829 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 24
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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???


* effects1.zip (2.53 KB - downloaded 13 times.)
Logged

Austin, TX
Offline Offline
Faraday Member
**
Karma: 64
Posts: 6055
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

while(digitalRead(pin) == HIGH);

That will cause the while() loop to effectively "stop" any other code from executing, while the "pin" is a HIGH.
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

0
Offline Offline
Newbie
*
Karma: 0
Posts: 24
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 24
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 24
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
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.
Logged

0
Offline Offline
Faraday Member
**
Karma: 19
Posts: 3420
20 LEDs are enough
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Check out my experiments http://blog.blinkenlight.net

Dallas, TX USA
Offline Offline
Edison Member
*
Karma: 47
Posts: 2347
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

0
Offline Offline
Faraday Member
**
Karma: 19
Posts: 3420
20 LEDs are enough
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Check out my experiments http://blog.blinkenlight.net

Pages: [1]   Go Up
Jump to: