Go Down

Topic: Hall sensors, interrupts etc. (Read 675 times) previous topic - next topic

Orac

Jan 05, 2008, 07:46 am Last Edit: Jan 05, 2008, 07:53 am by Orac Reason: 1
I read the other thread but couldnt make much sense of it.

I tried modifying the Fan speed sensor code, and that didnt work either.

Im trying to make a speedometer that detects the speed of the output shaft of the diff, and then workout the speed of the vehicle. Using a hall sensor to detect axle/wheel speed.

Here is my code, it is not finished and I have some questions:

Code: [Select]
/* Speed sensor
* Detects diff ouput speed and works out vehicle speed.
*/

volatile byte rpmcount;
unsigned int rotations;
unsigned long timeold
int tyresize;
int speed;


void setup()
{
 Serial.begin(9600);
 attachInterrupt(0, rpm_fun, RISING);
 rpmcount = 0;
 rotations = 0;
 timeold = 0;
 tyresize = 2345; //Enter the circumfrence of your tyre size here, meausred in mm
}

void loop()
{
 if (rpmcount >= 20) {
   //Update RPM every 20 counts, increase this for better RPM resolution, decrese for faster update
   rotations = 30*1000/(millis() - timeold)*rpmcount;
   timeold = millis()
   rpmcount = 0;
   speed = (rotations * tyresize) //Calculation to work out speed.
   Serial.PrintIn(speed,DEC);
 }
}

void rpm_fun()
{
 rpmcount++;
}


But i get a error when I try and compile it:

error: expected intializer before 'int'

error: expected initializer before 'int' In function 'void setup()':
In function 'void loop()':'

:'(

I know that my calculation for speed is wrong.


I dont understand what this part of the code does

rotations = 30*1000/(millis() - timeold)*rpmcount; (remember its modifed code from the fan rpm code)

I need a way to work out rpm of the wheel so I can work out speed. since i am planning on using this calculation to work out vehicle speed:

meterspm = wheelrpm * wheelcircumfrence / 1000 //times wheelrpm and wheel circumfrence (in millimeters) to give millimeters a minute, devide by 1000 to give meters per minute
speed = (meterspm /1000) *60 //devide meterspm by 1000 to give kilometers per minute, times 60 to give kilometers per hour.

So basically, I need to read RPM of a spinning axle with a hall effect sensor (or maybe some other sensor?) So I can work out vehicle speed. I think once I can get RPM into a varible I will be able to work out vehicle speed easily.

Sorry if this is hard to understand, my head is spinning.

Also, I cant get the original Fan speed RPM code to compile, which is:

Code: [Select]
volatile byte rpmcount;

unsigned int rpm;

unsigned long timeold;

void setup()
{
  Serial.begin(9600);
  attachInterrupt(0, rpm_fun, RISING);

  rpmcount = 0;
  rpm = 0;
  timeold = 0;
}

void loop()
{
  if (rpmcount >= 20) {
    //Update RPM every 20 counts, increase this for better RPM resolution,
    //decrease for faster update
    rpm = 30*1000/(millis() - timeold)*rpmcount;
    timeold = millis();
    rpmcount = 0;
    Serial.println(rpm,DEC);
  }
}

void rpm_fun()
{
  rpmcount++;
  //Each rotation, this interrupt function is run twice
}

Orac

#1
Jan 05, 2008, 07:54 am Last Edit: Jan 05, 2008, 07:56 am by Orac Reason: 1
I got the Fan rpm code to compile by moving the rpm_fun function above void_setup.

Is this the correct way to do it? So it now looks like this,

Code: [Select]
volatile byte rpmcount;

unsigned int rpm;

unsigned long timeold;

void rpm_fun()
{
  rpmcount++;
  //Each rotation, this interrupt function is run twice
}

void setup()
{
  Serial.begin(9600);
  attachInterrupt(0, rpm_fun, RISING);

  rpmcount = 0;
  rpm = 0;
  timeold = 0;
}

void loop()
{
  if (rpmcount >= 20) {
    //Update RPM every 20 counts, increase this for better RPM resolution,
    //decrease for faster update
    rpm = 30*1000/(millis() - timeold)*rpmcount;
    timeold = millis();
    rpmcount = 0;
    Serial.println(rpm,DEC);
  }
}



Orac

Ok, I think I have maybe figured this out myself.

Code: [Select]
volatile byte rpmcount;

unsigned int rpm;

unsigned long timeold;

int tyresize = 4320; //Tyre circumfrence in millimeters

int speed;

void rpm_fun()
{
  rpmcount++;
  //Each rotation, this interrupt function is run twice
}

void setup()
{
  Serial.begin(9600);
  attachInterrupt(0, rpm_fun, RISING);

  rpmcount = 0;
  rpm = 0;
  timeold = 0;
}

void loop()
{
  if (rpmcount >= 20) {
    //Update RPM every 20 counts, increase this for better RPM resolution,
    //decrease for faster update
    rpm = 30*1000/(millis() - timeold)*rpmcount;
    timeold = millis();
    rpmcount = 0;
    speed = (rpm*tyresize)/1000/1000*60;
    Serial.println(speed,DEC);
  }
}




;D ;D

I dont have an Arduino to test it yet, but it compiled fine.

Although there are two things I am not sure about.

(rpm*tyresize)/1000/1000*60;

Do I need spaces between /1000/100*60 etc? I am trying to do this:

rpm TIMES tyresize DEVIDE by 1000 DEVIDE by 1000 TIMES 60.

Will my code work for that?

Also,
Serial.println(speed,DEC);

What does the DEC do?

Cheers.

boardboy

First, the Serial.Println command syntax can be found here:
http://www.arduino.cc/en/Serial/Println

As for the math; do you want to do:
((rpm*tyresize)/1000/1000)*60;

or

(rpm*tyresize)/1000/(1000*60);

I would assume it would be the second otherwise you could change your code to:
((rpm*tyresize)/1000000)*60;

And you don't need spaces between operators (multiply and divide).

Go Up