He hecho un termostato simple para controlar un calentador de 12V. El rango de temperatura puede ser de 0 F a alrededor de 190 F. Por alguna razón, cuando la temperatura que se muestra en mi pantalla LCD va de 99 a 100, todo está bien, pero si vuelve a bajar de 100 a 99, el valor termina siendo 990. Creo esto tiene algo que ver con el redondeo en mi cálculo de Thermistor Temp. ¿Alguna idea? Código publicado a continuación.
/*
The circuit:
* 5V to Arduino 5V pin
* GND to Arduino GND pin
* CLK to Analog #5
* DAT to Analog #4
* BUTTON to Analog #2
* Thermistor to Analog #3
* Heater to digital #7
*/
// include the library code:
#include <Wire.h>
#include <LiquidCrystal.h>
#include "OneButton.h"
#include <EEPROM.h>
LiquidCrystal lcd(0);
int heater = 7; // heater is connected to pin 2
OneButton button(A2, true);
float settemp;
unsigned long currentTime;
unsigned long loopTime;
void setup()
{
currentTime = millis();
loopTime = currentTime;
EEPROM.read (1); // make the eeprom or atmega328 memory address 1
Serial.begin(9600);
// link the doubleclick function to be called on a doubleclick event.
button.attachDoubleClick(doubleclick);
button.attachClick(click);
pinMode(heater, OUTPUT); // Set the Heater Valve pin as output
lcd.begin(16,2);
delay(1000);
lcd.setBacklight(HIGH);
delay(1000);
lcd.setCursor(0,0);
lcd.print(" D.W. VEGGIE");
lcd.setCursor(0,1);
lcd.print(" BURNER SYSTEMS");
delay(4000);
lcd.clear();
delay(1000);
lcd.setCursor(0,0);
lcd.print(" CENTRITHERM");
lcd.setCursor(0,1);
lcd.print(" V1.0");
delay(4000);
lcd.clear();
delay(1000);
lcd.setCursor(0,0);
lcd.print("SET TEMP F ");
lcd.setCursor(0,1);
lcd.print("OIL TEMP F ");
}
double Thermister2(int RawADC) {
double Temp2;
Temp2 = log(((10240000/RawADC) - 10000));
Temp2 = 1 / (0.001129148 + (0.000234125 * Temp2) + (0.0000000876741 * Temp2 * Temp2 * Temp2));
Temp2 = Temp2 - 273.15; // Convert Kelvin to Celcius
Temp2 = (Temp2 * 1.8) + 32; // Convert to F
Temp2 = round(Temp2);
return Temp2;
}
void loop()
{
currentTime = millis();
settemp = EEPROM.read(1); // read the settemp on the eeprom
button.tick();
if(currentTime >= (loopTime + 500)){
double Temp2 = Thermister2(analogRead(1)); // read Thermistor
if (Temp2 < settemp) digitalWrite(heater, HIGH); // turn heater on
else if (Temp2 >= settemp) digitalWrite(heater, LOW); // turn heater off
lcd.setCursor(9,1);
lcd.print(Temp2,0);
loopTime = currentTime;
}
lcd.setCursor(9,0);
lcd.print(settemp,0);
EEPROM.write (1,settemp);
delay(10);
}
// this function will be called when the button was pressed 2 times in a short timeframe.
void doubleclick() {
settemp -- // remove one to the settemp, the settemp is the ideal temperature for you
;
} // doubleclick
// this function will be called when the button was pressed 1 time in a short timeframe.
void click() {
(settemp ++); // add one to the settemp, the settemp is the ideal temperature for you
} // click
// End
No, el problema es que no borraste el "0" al final de "100". Genere suficientes espacios después del valor actual para dejar en blanco el valor anterior.
Esto no se debe al redondeo, sino a la forma en que se muestran los caracteres en la pantalla LCD. Una vez que muestra 100, muestra tres caracteres en su pantalla, luego, cuando baja a 99, solo muestra 2 caracteres, pero la pantalla lo desvía a la derecha dejando el 0 del 100 aún en pantalla.
No estoy seguro de la solución exacta, pero intente experimentar con lcd.setCursor(9,1); y lcd.setCursor(9,0); funciones
O llama al lcd.clear(); función en algún lugar de su bucle vacío ();
Además, las escrituras en EEPROM lo desgastarán.
Es mejor usar una variable en la pila o en el montón.
Otro consejo, la única forma de depurar arduino es Serial.println(), es tedioso, sin embargo, confirmará lo que dijeron.
DW
sherrellbc