Go Down

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

mykiscool

#20
Jul 15, 2012, 02:15 am Last Edit: Jul 16, 2012, 10:37 pm by Nick Gammon Reason: 1
Thanks for all your help, I fixed it.

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
#define Reed 3 //pin for the switch

//create a timer
StopWatch MySW (StopWatch::MICROS);
//state of sensor 1 or 0
byte reedState=0;
//time between pass of magnet
unsigned long timeSincePass=0.0;
//revolutions per second
float RPS=0.0;
//revolutions per minute
float RPM=0.0;
//last state
byte lastreedState=0;

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


void loop()
{  
 reedState=digitalRead(3);
 //get the state of the sensor;
 //time since pass of magnet
 timeSincePass= MySW.elapsed();
 //set the lcd cursor to write on the second line
 

 //if the magnet engages the switch
 if (reedState != lastreedState && reedState==HIGH)
 {
   //turn on status led for visual purposes
   //convert milliseconds to seconds
   RPS=(float)1000000.0/timeSincePass;
   //reset the timer storage int    
   timeSincePass=0;
   //reset the timer and start it again
   MySW.reset();
   MySW.start();

 }

 if (reedState==HIGH)
 {
   digitalWrite(LED, HIGH); // gives a visual aid to tell when it is being magnetized
 }

 else
 {
   digitalWrite(LED, LOW); //Turns off when no magnet is detected
 }

 //convert seconds to minutes
 RPM=(float)RPS*60.0;
 

 //print the results to a screen
 lcd.print(RPM);
 lcd.setCursor(0, 1);
 lcd.print("Time  ");
 lcd.print(timeSincePass);  
 lcd.print(" MPH ");
 lcd.print();
 lastreedState = reedState;

 Serial.begin(9600);
 Serial.println(RPM);
}


Moderator edit: [code] ... [/code] tags added. (Nick Gammon)
Proud Member of the Nighthawk Robotics Club Team 569 B

Go Up