arduino - How to force interrupt to restart main loop instead of resuming? (timing issue!) -


for last 2 days wrote program in basic terms generates accurate user adjustable pulse signal (both frequency , duty cycle adjustable). uses micros() function keep track of time in order pull low or high 4 digital output channels.

these 4 channels need have phase difference of 90 degrees (think 4cyl engine) always. in order user change settings isr implemented returns flag main loop re-initialise program. flag defined boolean 'set4'. when false 'while' statement in main loop run outputs. when true 'if' statement perform necessary recalculations , reset flag 'while' statement resume.

the program works initial values. phase perfect. when isr called , comes main loop, how understand resumes program in 'while' statement interrupted, until finishes , re-checks flag 'set4' see true , should stop. then, though 'if' statement afterwards resets , re-calculates necessary variables phase between these 4 output channels lost. tested manually see depending on time isr called give different results, having 4 output channels synchronised together! happens though might don't change values (thus 'if' routine resets variables same ones when first power arduino!). however, if comment out routine , leave line resets flag 'set4' program continue nothing never happened!

i'm pretty sure somehow caused because of micros() timer because loop resumed isr called. i've tried differently checking , disabling interrupts using cli() , sei() couldn't work because freeze when arguments cli() true. solution can think of (i've tried everything, spend whole day searching , trying out stuff) force isr resume start of loop program may initialize properly. solution comes mind maybe reset micros() timer somehow..but mess isr believe.

to visualise going on here's snip of code (please don't mind 'millis" name in micros variables , missing curly brackets since not pure copy-paste :p):

    void loop()     {         while(!set4)         {         currentmillis = micros();         currentmillis2 = micros();         currentmillis3 = micros();         currentmillis4 = micros();            if(currentmillis - previousmillis >= interval) {             // save last time blinked led              previousmillis = currentmillis;                 // if led off turn on , vice-versa:             if (ledstate == low)             {               interval = ontime;               ledstate = high;             }             else             {              interval = offtime;               ledstate = low;             }              // set led ledstate of variable:             digitalwrite(ledpin, ledstate);             }     .     .     //similar code other 3 output channels     .     .     }     if (set4){     //recalculation routine - same when declaring variables         currentmillis = 0;       currentmillis2 = 0;       currentmillis3 = 0;       currentmillis4 = 0;        //output states of output channels, forced low seperately when isr called (without messing 'ledstate' variables)       ledstate = low;       ledstate2 = low;       ledstate3 = low;       ledstate4 = low;        previousmillis = 0;       previousmillis2 = 0;       previousmillis3 = 0;       previousmillis4 = 0;       //ontime high time interval of pulse wave (i.e. dwell time), offtime low time interval      //note calculated phase/timing offset @ each channel        interval = ontime+offtime;       interval2 = interval+interval/4;       interval3 = interval+interval/2;       interval4 = interval+interval*3/4;        set4=false;      } } 

any idea going wrong? kind regards, ken

the problem here:

  previousmillis = 0;   previousmillis2 = 0;   previousmillis3 = 0;   previousmillis4 = 0; 

all if statement true on next loop.

try with:

  previousmillis = micros();   previousmillis2 = micros();   previousmillis3 = micros();   previousmillis4 = micros(); 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -