Go Down

Topic: Revolutions Per Minute Calculating Error (Read 2 times) previous topic - next topic

marco_c

#5
Jun 19, 2012, 04:12 am Last Edit: Jun 19, 2012, 05:02 am by marco_c Reason: 1
I also don't see any of the logic that I would expect in this code.

There are 2 ways to do this:
1. Count how long it takes for one revolution (sec/rev) and then take the reciprocal to get rev/sec. You will probably count ms so a conversion will be required.
2. Count the number of 'clicks' in a second (or partial second) and then use the count directly.

What was your intention here?
Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

mykiscool

My intention was to measure the speed of a bike wheel as it turns in revolutions per second or minute and then calculate that to the actual speed using the wheel size. Also I tried using a debounce to make sure I wasn't getting the magnetic reed giving me 2 readings and I still have the same thing not the right reading. Any suggestions?
Proud Member of the Nighthawk Robotics Club Team 569 B

mykiscool

I sort of made it into a reciprocal when I divided 1000 by the milliseconds which would be the same as dividing 1 by the seconds. Ex: 5 seconds = 5/1 1 divided by 5 = 1/5 opposite of 5/1. I think the math is right as far as i can tell. Constructive criticism would be appreciated to help me find the right solution.
Proud Member of the Nighthawk Robotics Club Team 569 B

Nick Gammon


mykiscool

Here's my updated code to include a debounce:

Code: [Select]


//these are just some references for it to work properly
#include <StopWatch.h>
#include <LiquidCrystal.h>
//these are the microcontroller ports that an lcd screen is connected to, a status led, and the switch.
LiquidCrystal lcd(12, 11, 7, 6, 5, 4);
#define LED 13 //pin for the LED indicator (green)
#define Reed 3 //pin for the switch

//create a timer
StopWatch MySW;
//state of sensor 1 or 0
byte reedState=0;
//time between pass of magnet
float timeSincePass=0;
//revolutions per second
float RPS=0;
//revolutions per minute
float RPM=0;
//last state of reed switch on or off
byte lastReedState=LOW;
long lastDebounceTime = 0;
long debounceDelay = 50;

//just some setup stuff
void setup()
{
  pinMode(LED,OUTPUT);
  pinMode(Reed,INPUT);
  Serial.begin(9600);
  lcd.begin(16, 4);
  lcd.print("Your Revs/Sec");
}


void loop()
{
 
  byte reading = digitalRead(Reed); 
  //get the state of the sensor;
  //time since pass of magnet
  timeSincePass= MySW.elapsed();
  //set the lcd cursor to write on the second line
  lcd.setCursor(0, 1);

  if (reading != lastReedState)
  {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }
 
    if ((millis() - lastDebounceTime) > debounceDelay)
  {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:
    reedState = reading;
  }

//if the magnet engages the switch
  if (reedState==HIGH)
  {
    //turn on status led for visual purposes
    digitalWrite(LED, HIGH);
    //convert milliseconds to seconds
    RPS=(float)timeSincePass*1000;
    //reset the timer storage int   
    timeSincePass=0;
    //reset the timer and start it again
    MySW.reset();
    MySW.start();
  }

  else
  {
    //turn off led otherwise
    digitalWrite(LED, LOW);
  }
  //convert seconds to minutes
  RPM=(float)RPS*60;
 
  //print the results to a screen
  lcd.print(RPS);
  lcd.print("  ");
  lcd.print(timeSincePass);
  lastReedState = reading;
}
Proud Member of the Nighthawk Robotics Club Team 569 B

Go Up