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
}