Go Down

Topic: analogRead messes up my frequency counter function (Read 1 time) previous topic - next topic

ElJefeUno

I'm trying to make a multi-function program that acts as both a frequency counter (512 Hz range) and a voltage level reader.  I've written the code for the frequency counter, and it functions fine on it's own, but as soon as I add an analogRead function to test the voltage it messes up my frequency count.  I've tried adding delays before and after calling the frequency function, but this caused greater error in my results.  I'm thinking that the delay created by the analogRead function is interfering with the frequency counter code, but shouldn't the for loop negate that?


Code: [Select]
bool cur_Val = false;
unsigned long cur_Time = 0;
unsigned long old_Time = 0;
bool old_Val= false;
unsigned long time_arr[200];
int counter = 0;
unsigned long freq = 0;

unsigned long test_Freq();

void setup()
{
 Serial.begin(9600);
 pinMode(5, INPUT);
 pinMode(13, INPUT);
}


void loop()
{
 Serial.println(analogRead(5));
 Serial.println(test_Freq());

}

unsigned long test_Freq()
{
 for(; counter < 199;)
 {
   cur_Val = digitalRead(12);
   
   if(old_Val == 0 && cur_Val == 1)
   {
     cur_Time = micros();
     int time_Delta = cur_Time - old_Time;
     time_arr[counter] = time_Delta;
     counter++;
     old_Time = cur_Time;
   }
   
   old_Val = cur_Val;
   
   if (counter == 199)
   {
     unsigned long sum = 0;
     for(int i = 0; i < 200; i++)
     {
       sum = sum + time_arr[i];
     }
     freq = 1 / (.000001*(sum / 200));
   }  
 }
 counter = 0;
 return(freq);
}

AWOL

#1
Nov 12, 2010, 10:48 pm Last Edit: Nov 12, 2010, 10:50 pm by AWOL Reason: 1
The delay caused by the analogRead is way less than the delay due to printing the value it returns.

I'm not sure I understand your loop nesting.

Code: [Select]
pinMode(5, INPUT);
You don't do anything with pin 5 - is this important?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

ElJefeUno

So do you think this delay is what's throwing off my frequency counter code?  I originally wrote the frequency counter code in the main loop, but I was encountering this issue so I thought I'd try a for loop.  I figured if I put the code into a for loop, and called it in a separate function it would run exclusively and avoid any time delays created by other code... but my results are identical.  My frequency count is thrown off by -8Hz.  I'm assuming some sort of delay occurs during the loop causing it to miss cycles of my test frequency when I add that function.

As for
Code: [Select]
pinMode(5, INPUT);


I'm using analog pin 5 for my voltage test.  Maybe the pin initialization isn't necessary, but I don't think this is related to my issue.

ElJefeUno

So I tried removing Serial.println(analogRead(5)); , and replaced it with just (analogRead(5)); and the problem went away.  This still begs the question of how I can return the results of the voltage test without messing up my frequency count.

ElJefeUno

Okay... well I tried increasing the serial baud rate to 57600 and now it seems to work fine!  I still find it strange though...

Go Up