state machine question

11 Dec 2010

Hi. I have stumbed upon something which is really confusing to me. (unless it's really basic)

I tried to implement a basic state machine. So there are three task and two of them have a single loop. I observe that when I use a loop the event occurs only once but once I unroll the loop it behaves ok. I tried to use a global variable with no effect.

 

int main() {
	
	InitializeSystem();

    while(1) {
        state_table[curr_state]();
    }
}

void Task1(){
	for(int j=0;j<4;j++){  //blink only once
	led1 = 1;
	wait(1);
	led1 = 0;
	
	}
  curr_state =  BlinkTwoLed;//next state

}
Any ideas appreciated.

11 Dec 2010

Hi,..

can you give us the whole program ?

 

11 Dec 2010

Hi Dimiter,

I'd predict not only does it only flash once instead of four times, it *also* flashes for 4 seconds rather than 1 second (which should give a clue).

I think your problem is just a slip in your led flash code. In:

for(int j=0;j<4;j++) {  //blink only once
	led1 = 1;
	wait(1);
	led1 = 0;
}

you only have a delay after turning the led on. So on all but the last iteration of the loop, it'll turn off the led and then immediately turn it on again as it re-enters the loop; so you wont see it. So change it to:

for(int j=0;j<4;j++) { 
	led1 = 1;
	wait(1);
	led1 = 0;
	wait(1);
}

and all should be fine!

Simon

11 Dec 2010

Thanks once againg Simon, that fixed it. For a moment I thought I could not embed loops inside a task. Probably I shouldn't be coding late.

@David here it is simpl-fsm