2

As a first project I plan on making a teensyduino ambient light with different light modes, which are checked in a big switch statement - now I want to switch from one mode to another by pressing a button.

googling lead me to using interrupts, but there is one point that is not clear - if I press the button during an expensive function, which takes long time and has many variables in use, what happens if i call the main loop from the interrupt, does the remaining state of remain in the ram and leads to a stackoverflow if I do switch too many times or is it cleared.

Here some code:

const int speed = 30 //milliseconds
const int modes = 11; //maximum number of modes
const int red   = 15;
const int green = 14;
const int blue  = 12;

volatile int mode  = 0;

void setup() {
    pinMode(red   , OUTPUT);
    pinMode(green , OUTPUT);
    pinMode(blue  , OUTPUT);
    randomSeed(analogRead(0));
    Serial.begin(9600);
    attachInterrupt(0,incMode,CHANGE); // 0 -> digital pin 2
}
void loop()  {
    switch(mode){
        case 0:{
            Serial.println("powerdown");
            setAll(0);
            delay(1000);
            break;
        }
        \\...
        case modes:{
            \\ expensive long function
        }
    }
}
void blinkAll(int times){
    for(int i=1;i <= times;i++){
        setAll(255);
        delay(speed*17);
        setAll(0);
        delay(speed*17);
    }
}

void setAll(int bright){
        analogWrite(red   , bright);
        analogWrite(green , bright);
        analogWrite(blue  , bright);
}
void incMode(){
    delay(speed);
    blinkAll(2); //to indicate mode has changed
    mode = (mode+1) % (modes+1); //switch starts with 0 so use "% modes+1"!
    Serial.println("mode increased");
    //--> loop();
    //--> would resume the main loop but lead to a stackoverflow i presume
}

How would I break out of the running function without delay and stack pollution. I know I could just set the mode and wait until the function has ended, but if I have a mode that takes minutes to end I want to be able to switch from it immediately.

PS.: Though I am using a teensyduino, I will use the arduino tag, and as I don't know what language the arduinio uses the tags c/c++. Please change this if it is not appropriate.

4

1 回答 1

1

如果您从中断处理程序递归地多次重新进入 main,您最终会溢出堆栈。此外,由于就硬件而言,您仍将处于中断处理程序中,因此您将遇到各种奇怪的情况-特别是,当您已经处于中断状态时,中断被阻止,这意味着delay()将无法工作并且millis()不会计数,除非您找到某种手动重新启用中断的方法,否则其他各种事情也会被破坏。

解决这个问题的一个更好的方法是让你的“昂贵的长函数”变成一个状态机,由一个廉价的、经常调用的短函数驱动。然后,您的中断处理程序可以简单地设置一个标志,该标志在进入此函数时进行检查,此时当前模式(即当前状态机)被更改。

这种方法还可以更轻松地定义新的照明模式。例如,您可以定义如下内容:

struct phase {
  unsigned char r, g, b, delay;
};

unsigned long t_nextPhase;
volatile struct phase *forceMode = NULL;
struct phase *mode = blinkAll;
int nextPhase = 0;

struct phase blinkAll[] = {
  { 255, 255, 255, 17 },
  { 0, 0, 0, 17 },
  { 0, 0, 0, 255 } // loop sentinel
};

void lighting_kernel() {
    noInterrupts(); // ensure we don't race with interrupts
    if (forceMode) {
        mode = forceMode;
        forceMode = NULL;
        t_nextPhase = millis();
        nextPhase = 0;
    }
    interrupts();

    if (t_nextPhase > millis()) {
        return;
    }

    struct phase *cur_phase;
    do {
        cur_phase = mode[nextPhase++];
        if (cur_phase->delay == 255) {
            nextPhase = 0;
        }
    } while (cur_phase->delay == 255);

    analogWrite(red   , cur_phase->r);
    analogWrite(green , cur_phase->g);
    analogWrite(blue  , cur_phase->b);

    t_nextPhase = millis() + cur_phase->delay;    
}

现在要定义新的照明模式,您只需要一个新的颜色和时间数组,而不是编写新代码。添加诸如颜色渐变和其他此类效果之类的内容作为练习留给读者。

于 2012-10-21T11:26:58.193 回答