Go Down

Topic: elapsed lib question [SOLVED] (Read 329 times) previous topic - next topic

yair

Jan 23, 2012, 10:59 am Last Edit: Jan 23, 2012, 11:45 am by yair Reason: 1
hey,
using the elapsed lib from gammon,  basicly trying a chase sequence with no delays.

im having a problem with scope, i think.

here is the code, (should compile if you have the elapsed lib).
what im seeing is the first (nested?) timer function works ok with the specified intervals, the second one doesn't - much shorter.  
i included the output from the serial in the code.

EDIT: i think i found the bug, seems i need to reset the next timer in the sequence before calling it (duh). added  time[pin+1].reset();
Code: [Select]

#include <Elapsed.h> //http://www.gammon.com.au/forum/?id=11094

static Elapsed time [3]; //an array of two timers (would be 24 in real code)
int Hstate[3]={99,99,99};  //mini state machine for inside the timer function H_pulseWave

// just some pins configured...
int hepticPin[2][3] = {
  {1,5,6},//[ number, pinFWD, pinBWD]
  {2,7,8},
};

int intrvl_kick = 1000;
int intrvlOFF = 50;
int intrvlON = 100;

void setup(){
  Serial.begin(19200);  // initialize serial communication:
  Hstate[1]=0;
}

void loop(){

/*
here is (was :))  the strange bit, the first H_pulseWave  will output the correct intervals , the second, much shorter pulses.
start
1 1001
1 1051
1 1151

2 1155
2 1159
2 1164
*/
    H_pulseWave(1);
    H_pulseWave(2);
}


void H_pulseWave(int pin){
  if(time[pin].intervalMs() > intrvl_kick && Hstate[pin]==0)
  {
    H_bwd(pin);
    Hstate[pin]=1;
    Serial.print(pin);     Serial.print(" ");     Serial.println(time[pin].intervalMs() ); 
  }
  if(time[pin].intervalMs() > intrvl_kick+intrvlOFF && Hstate[pin]==1) //50
  {
    H_fwd(pin);
    Hstate[pin]=2;
    Serial.print(pin);        Serial.print(" ");      Serial.println(time[pin].intervalMs() ); 
  }

  if(time[pin].intervalMs() > intrvl_kick+intrvlOFF+intrvlON && Hstate[pin]==2) //15
  {
    Serial.print(pin);      Serial.print(" ");     Serial.println(time[pin].intervalMs() ); 
   
    H_off(pin);
    Hstate[pin]=99; // make this offline , will check this on the main loop
    time[pin].reset();
   
    Hstate[pin+1]=0; // triger next one pin in wave loop
    time[pin+1].reset(); //added this! to fix bug
  }

}


void H_bwd(int pin){
  digitalWrite(hepticPin[pin][1], LOW);   
}
void H_fwd(int pin){
  digitalWrite(hepticPin[pin][1], HIGH);   
}

void H_off(int pin){
  digitalWrite(hepticPin[pin][1], LOW);   
}


Go Up