Hi
The following code is to measure RPM of a DC motor and also voltage across the armature of the machine. The problem is that , the LCD reads a 1.45V when there is no voltage being mesure or the analog input has 0V .....can someone please tell me why this is happening and what should be done to correct this??
#include <LiquidCrystal.h>
LiquidCrystal lcd(3, 5, 9, 10, 11, 12);
volatile float time = 0;
volatile float time_last = 0;
volatile int rpm_array[5] = {0,0,0,0,0};
volatile float voltage_array[5] = {0,0,0,0,0};
volatile float voltage=0;
float sensorValue;
void setup()
{
Serial.begin(9600);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
//Digital Pin 2 Set As An Interrupt
attachInterrupt(0, fan_interrupt, FALLING);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("RPM & BATT Volt");
}
//Main Loop To Calculate RPM and Update LCD Display
void loop()
{
int rpm = 0;
int rpm_out=0;
// int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
//float voltage = sensorValue * (5.0 / 1023.0);
// print out the value you read:
//Serial.println(voltage);
while(1){
//Slow Down The LCD Display Updates
delay(300);
//Clear The Bottom Row
lcd.setCursor(0, 1);
lcd.print(" ");
//Update The Rpm Count
rpm_out=rpm*7;
lcd.setCursor(0, 1);
lcd.print(rpm_out);
lcd.print(",");
sensorValue = analogRead(A0);
float voltage = sensorValue * (5.0 / 1023.0);
lcd.print(voltage);
sensorValue = analogRead(A0);
voltage = sensorValue * (5.0 / 1023.0);
if (voltage>=4.7)
{digitalWrite(8, HIGH);}
else
{digitalWrite(8, LOW);}
if (rpm_out>1550)
{digitalWrite(7, HIGH);}
else
{digitalWrite(7, LOW);}
//Update The RPM
if(time > 0)
{
//5 Sample Moving Average To Smooth Out The Data
rpm_array[0] = rpm_array[1];
rpm_array[1] = rpm_array[2];
rpm_array[2] = rpm_array[3];
rpm_array[3] = rpm_array[4];
rpm_array[4] = 60*(1000000/(time*7));
//Last 5 Average RPM Counts Eqauls....
rpm = (rpm_array[0] + rpm_array[1] + rpm_array[2] + rpm_array[3] + rpm_array[4]) / 5;
}
}
}
//Capture The IR Break-Beam Interrupt
void fan_interrupt()
{
time = (micros() - time_last);
time_last = micros();
}
Regards
Emmanuel