Go Down

Topic: Help Required - RPM Counter With Analog input (Read 7680 times) previous topic - next topic

Qwik

Jan 21, 2011, 01:45 am Last Edit: Jan 21, 2011, 02:47 am by Qwik Reason: 1
Hello to all,
Can any anyone help me?
I am trying to make a RPM counter using a hall sensor (UNG3503UA, connected to analog pin 0), by detecting when the voltage drops just below 1/2 of the operational output voltage.
Would anyone mind looking over my very amateur code, to see where I have gone wrong,

**The intro screen is loading up ok, then it displays the rpm text and 0,
but when i apply a pulsing voltage (5v) to the input, nothing happends**

Any help is very much appreciated,
Nick




#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);  // Initialize the library with the numbers of the interface pins

int   analogPin = A0;                        // Pin that the Hall sensor is attached to
int   threshold = 400;                  // A defined threshold level that's in the range of the analog input
int   hallState = 0;                    // Value used to store state of Sensor
int   oldHallstate = 0;                 // This Stores he last Hallstate value
int   rpm = 0;                          // Value used to store state of Sensor
long  startTime;                        // Start time for timing
long  elapsedTime;                      // Elapsed time for stop watch
int test;
   
void setup()
{
 lcd.begin(16, 4);                     // Set up the LCD's number of columns and rows:
 lcd.setCursor(0, 0);                  // Sets the posiion of the cursor
 lcd.print("x");     // Print a message to the LCD.
 lcd.setCursor(0, 1);                  // Sets the posiion of the cursor
 lcd.print("x");        // Print a message to the LCD.
 lcd.setCursor(0, 2);                  // Sets the posiion of the cursor
 lcd.print("x");       // Print a message to the LCD.
 lcd.setCursor(0, 3);                  // Sets the posiion of the cursor
 lcd.print("x");    // Print a message to the LCD.
 delay(4000);                          // Delay for opening message
 lcd.begin(16, 4);                     // Clear Screen
 lcd.setCursor(5, 1);                  // Sets the posiion of the cursor
 lcd.print("RPM");                     // Print a rpm lable to the LCD.
}

void loop()
{
 {
   test = analogRead(0);  // read the analog value of the hall sensor
   if   (test >= threshold)           // if the analog value is high enough, turn state to high
        {            
          hallState = 1;
        }
 
   else {
          hallState = 0;
        }
 
  }
 
  {
   
    if       (hallState == 0 && oldHallstate == 1)
             {
                startTime = millis();
                delay(5);
                oldHallstate = hallState;  
             }
   
    else if  (hallState == 0 && oldHallstate == 1)
             {
                elapsedTime = millis() - startTime;
                oldHallstate = hallState;  
             }  
  }

   rpm =((int)(elapsedTime / 1000));
   lcd.setCursor(10, 1);                 // Sets the posiion of the cursor
   lcd.print(rpm);                       // Print rpm value

}

PaulS

Code: [Select]
int [glow]analogPin[/glow] = analogRead([glow]analogPin[/glow]);
  if   ([glow]analogPin[/glow] >= threshold)           // if the analog value is high enough, turn state to high

In the if statement, which analogPin are you referring to? The global one or the local one?

Qwik

I'm afraid that I have confused myself totally,
To be honest I'm not sure

PaulS

Defining local variables and global variables with the same name is almost never a good idea, or necessary.

Variable names should reflect the meaning of the contents. analogPin is not a very good name for either purpose. The value in the variable and the context in which it is used indicates that the global variable IS an analog pin. But, a name like hallSensorPin says a lot more about the use of that variable. In the same way, hallSensorValue tells you a lot about the contents of the variable, and can lead to spotting incorrect usage when it happens.

int hallSensorPin = analogRead(hallSensorValue);

would be spotted as an error right away, while

int hallSensorValue = analogRead(hallSensorPin);

would look perfectly reasonable. Now, there could still be errors, like hallSensorPin having a value of 3 while the sensor is connected to pin 5, but that is a lot less likely an event.

Quote
I am trying to make a RPM counter using a hall sensor (UNG3503UA, connected to analog pin 0), by detecting when the voltage drops just below 1/2 of the operational output voltage.

Do you have a link to this sensor? Typically, the output of a hall effect sensor is not linear. The applications for which a hall effect sensor are used, such as detecting and counting rotational motion, do not require linear output. Therefore, it seems strange to be using a hall effect sensor this way.

In general, the hall effect sensor is connected to a digital pin. The presence of a pulse will be detected when the voltage rises above 60% of the reference voltage. The exact value is not all that critical, since the sensor is so non-linear, the difference in time between 50% and 60% will hardly be detectable.

And, if time IS a factor, the slow response time of the ADC that is measuring the input will nullify that effect, anyway.

cachehiker

#4
Jan 23, 2011, 03:31 am Last Edit: Jan 23, 2011, 03:32 am by cachehiker Reason: 1
Code: [Select]
   if       (hallState == 0 && oldHallstate == 1)
            {
               startTime = millis();
               delay(5);
               oldHallstate = hallState;  
            }
   
   else if  (hallState == 0 && oldHallstate == 1)
            {
               elapsedTime = millis() - startTime;
               oldHallstate = hallState;  
            }  


Have I been smoking something or is this basically:

if X

else if X

How is it supposed to reach elapsedTime = ?

Go Up