Pages: [1]   Go Down
Author Topic: Tachometer project stalled  (Read 587 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am trying to make a tachometer for my car.
The car ecu puts out at 5v square wave with each ignition pulse. (4 per revolution)
I thought I could use the tutorials for the IR computer fan tachometer, but I dont understand the code.
When I run the pulses too high, the terminal window gives me negative numbers. 
I am using Picaxe 28X2 shield to provide the pulses, so at this point they are free from ignition noise while I figure out how to program it.
Here is the current code, Can someone explain it to me and help me get it to not give negative numbers?
Thanks in advance
Cory

Code:
volatile float time = 0;
volatile float time_last = 0;
volatile int rpm_array[5] = {0,0,0,0,0};

void setup()
{
  Serial.begin(115200);
  //Digital Pin 2 Set As An Interrupt
 attachInterrupt(0, fan_interrupt, FALLING);

 
  Serial.println("Current RPM:");
}

//Main Loop To Calculate RPM and Update LCD Display
void loop()
{
 int rpm = 0;
 
  while(1){   

     //Slow Down The LCD Display Updates
  delay(400);
 
 
  //Update The Rpm Count
 
  Serial.println(rpm);   

  ////lcd.setCursor(4, 1);
  ////lcd.print(time);   

  //Update The RPM
  if(time > 0)
  {
    //5 Sample Moving Average To Smooth Out The Data
      rpm_array[0] = rpm_array[1];
      rpm_array[1] = rpm_array[2];
      rpm_array[2] = rpm_array[3];
      rpm_array[3] = rpm_array[4];
      rpm_array[4] = 60*(1000000/(time*7));   
    //Last 5 Average RPM Counts Eqauls....
      rpm = (rpm_array[0] + rpm_array[1] + rpm_array[2] + rpm_array[3] + rpm_array[4]) / 5;
  }
 
 }
}

//Capture The IR Break-Beam Interrupt
void fan_interrupt()
{
   time = (micros() - time_last);
   time_last = micros();
}
Code:
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 203
Posts: 8720
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

micros() returns an unsigned long int, not a float.  Try changing time and time_last to 'unsigned long'.

At 5000 RPM (20,000 pulses per second) you should get time = 50.

At 1000 RPM (4000 pulses per second) you should get time = 250.

Sounds like it should work well.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you!
  I'll give that a try and report back.
 
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That doesnt seem to solve the negative number problem either.  I think its just my lack of experience.  I've got my sights set on a pretty advanced project and only have a basic understanding of Arduino. I'll do some more reading and searching and see what I come up with.
Thanks for your help I appreciate it.
Logged

Saskatchewan
Offline Offline
Sr. Member
****
Karma: 19
Posts: 363
When the going gets weird, the weird turn pro. - Hunter S. Thompson
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

 rpm_array[4] = 60*(1000000/(time*7));   

I think it might be that formula which for small values of time won't fit an int type. Try rpm_array and rpm as unsigned long.
Logged

Pages: [1]   Go Up
Jump to: