Pages: [1]   Go Down
Author Topic: Help Required - RPM Counter With Analog input  (Read 7073 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-cry(int)(elapsedTime / 1000));
    lcd.setCursor(10, 1);                 // Sets the posiion of the cursor
    lcd.print(rpm);                       // Print rpm value

}
« Last Edit: January 20, 2011, 08:47:22 pm by Qwik » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 615
Posts: 49388
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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?
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 615
Posts: 49388
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

The Udaho Border
Offline Offline
Newbie
*
Karma: 2
Posts: 18
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
   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 = ?
« Last Edit: January 22, 2011, 09:32:21 pm by cachehiker » Logged

Pages: [1]   Go Up
Jump to: