i have two motors that i’m driving with PWM outputs. using optical sensors, i feedback sampled frequency information to my arduino in an attempt to synchronize the motors. this means i need both frequency and phase information. i had originally used an fft to get that information, but i decided that it might be much more efficient to use interrupts. i borrowed some code posted elsewhere to the forum ( http://arduino.cc/forum/index.php/topic,45295.0.html ) and it’s working pretty well as far as i can tell. i’m able to get both frequency and phase information this way, and it seems pretty reliable. sadly, i’m not very good at the programming side of things, and i’m having a hard time creating an array so that i can gather a few statistics by averaging some number of samples. it seems like it should be really easy, but i just can’t quite come up with the right code to do it. if i use a for loop in my my void loop() bit, i get repetitive entries because my loop is iterating faster than my interrupts are triggering. i also tried moving my array to my interrupt function, but i wasn’t able to get that to work either. (i get an entire array filled with the one value.) how do i tell it “hey, when you get your trigger, pass that value to an array?” am i making this way harder than it is? (i’m going to try to simplify the code below, so if something doesn’t make sense, it’s because i haven’t done a very good job simplifying it. forgive the fact that some of my variable names don’t make sense right now (i.e. “freqM1” is not an array of frequencies) - it’s because i’m simplifying things to try to get my array to work. )
int CH1_PIN = 2;
int CH3_PIN = 3;
unsigned long ch1_pulseLength;
unsigned long ch3_pulseLength;
unsigned long ch1_startTime;
unsigned long ch3_startTime;
const uint16_t samples = 9;
float freqM1[samples];
float freqM2[samples];
void setup() {
pinMode(CH1_PIN, INPUT);
pinMode(CH3_PIN, INPUT);
// when pin2 goes high, call function to set T0:
attachInterrupt(0, ch1_begin, RISING);
// when pin 3 goes high, call function to determine elapsed time from T0:
attachInterrupt(1, ch3_begin, RISING);
Serial.begin(9600);
}
//interrupt function to mark rising edge of first motor:
void ch1_begin() {
ch1_startTime = millis();
/* here's the second place i tried, where i get a full array of one value:
for(i=0; i < samples; i++) {
freqM1[i] = ch1_startTime;
Serial.println(i);
Serial.println(freqM1[i]);
}*/
detachInterrupt(0);
attachInterrupt(0, ch1_end, FALLING);
}
void ch1_end() {
ch1_pulseLength = millis() - ch1_startTime;
detachInterrupt(0);
attachInterrupt(0, ch1_begin, RISING);
}
void ch3_begin() {
ch3_startTime = millis();
detachInterrupt(1);
attachInterrupt(1, ch3_end, FALLING);
}
void ch3_end() {
ch3_pulseLength = millis() - ch3_startTime;
detachInterrupt(1);
attachInterrupt(1, ch3_begin, RISING);
}
void loop() {
/* here's where i first tried to make the array, where i get multiple entries of each value:
for (int i = 0; i < samples; i++)
freqM1[i] = ch1_startTime;
Serial.println(i);
Serial.println(freqM1[i]); */
}
so when i do this without the array, i get nice, meaningful numbers, and each occurs exactly once. when i try to put the array in the ch1_begin() function, i just get an entire array filled with the one ch1_startTime value. when i move it down to the void loop(), i get multiples. i’ve tried introducing another variable to save “ch1_startTime” to, and then pass it to the array, but that didn’t work either. please help me get this straightened out. it’s making me crazy.