Go Down

Topic: Fan RPM to LCD (Read 1 time) previous topic - next topic

mrob73

After a few hours of trial and error and taking into consideration the previous advice received from earlier posts. I got it to work. The only issue I am having is once the RPM of the fans goes above 1300 and then ramps back down the display reads the value as a four digit number rather than a three digit number. For example 400 is displayed as 4000, 600 is displayed as 6000, etc. I'm not sure why, but it is closer. This is the sketch I am currently running.
Code: [Select]
#include <LiquidCrystal.h>
float tempC;
float tempF;
float voltage;
int reading;
int interval = 50;
int NbTopsFan; //Varibles used for calculations
int Calc;
int hallsensor = 2;
typedef struct{                  //Defines the structure for multiple fans and their dividers
  char fantype;
  unsigned int fandiv;
}fanspec;
//Definitions of the fans
fanspec fanspace[3]={{0,1},{1,2},{2,8}};

char fan = 1;   //This is the varible used to select the fan and it's divider, set 1 for unipole hall effect sensor
               //and 2 for bipole hall effect sensor


void rpm ()      //This is the function that the interupt calls
{
NbTopsFan++;
}
//constants
const int tempPin = 0; //analog channel
const int fan1 = 3; //fan 1
const int fan2 = 6;//fan 2
const int setPointF = 68; //changing this set point will determine cabinet temperature and will be diplayed on the LCD

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

void setup() {
  pinMode(fan1, OUTPUT); // Set pin for output to control fan1
  pinMode(fan2, OUTPUT); // Set pin for output to control fan2
  pinMode(hallsensor, INPUT);
  attachInterrupt(0, rpm, RISING);
  // establishes fan start up on lcd
  lcd.begin(16, 2);
  lcd.print(" Starting Up");
  lcd.setCursor(0,1);
  lcd.print(" Please Wait");
  delay(10000);
  lcd.clear();
}
void loop() {
   //TMP36 voltage at the output varies linearly with ambient temperature.
  //As a result,it can be used as a thermometer according to the equation:
  // ?C = (Vout(V) - .5)*100
  //To find Vout in V, we use the following equation for a 5V input
  // Vout(V) = AnalogReading*(5/1024)
  reading = analogRead(tempPin);   //read the value from the sensor
  voltage = reading*(5.0/1024);  //convert reading to voltage (in V), for 5V input
  tempC = (voltage-0.5)*100;   //convert voltage to temperature
  tempF = ((tempC*9/5)+32); //convert C temperature to F
  // (note: line 1 is the second row, since counting begins with 0):
  NbTopsFan = 0; //Set NbTops to 0 ready for calculations
   sei(); //Enables interrupts
   delay (1000); //Wait 1 second
   cli(); //Disable interrupts
   Calc = ((NbTopsFan * 60)/fanspace[fan].fandiv); //Times NbTopsFan (which is apprioxiamately fequency the fan is spinning at) by 60 seconds before dividing by the fan's divider

  lcd.setCursor(0,0);// set the cursor to column 0, line 1
  lcd.print(tempF);  //Print Fahrenheit temperature to LCD
  lcd.print((char)223); // degree symbol
  lcd.print("F ");
  lcd.setCursor(0,7);  //print set
  lcd.print("Set:");
  lcd.setCursor(5,1);
  lcd.print(setPointF);
  lcd.print((char)223);
  lcd.print("F");
  lcd.setCursor(10,0);
  lcd.print("RPM");
  lcd.setCursor(10,1);
  lcd.print (Calc,DEC); //Prints " rpm"
  // If the temperature is higher than the set point, run the fans.
  // Fans reach full speed when temperature is more than 10°F above setpoint.
  analogWrite(fan1,constrain( (tempF - setPointF) * 25, 0, 255));
  analogWrite(fan2,constrain( (tempF - setPointF) * 25, 0, 255));

}

Any ideas as to why the value doesn't reset to three numbers?

PaulS

Quote
Any ideas as to why the value doesn't reset to three numbers?

Try writing 3 spaces after the value. The 0 in 1000 does not get overwritten by anything in 999, so you see 9990 on the display.

mrob73

Oh I forgot I added a 10k pull up resistor from pin 2 to 5V pin.

mrob73

Thank you Paul that worked. Thanks everyone for the assistance.

Go Up