He estado trabajando en este programa para un controlador simple. Tengo un prototipo completamente conectado y funcionando, sin embargo, el pulsador momentáneo no es muy confiable. A veces lo lee y otras veces lo ignora.
Aquí está mi código.
/*
* Valve Purge Control/heater control
*/
#include <math.h>
int feedValve = 12; // feed valve is connected to pin 12
int returnValve = 11; // return valve is connected to pin 11
int heater = 10; // heater is connected to pin 10
//int loopValves = 9 // loop valves are connected to pin 9
//int pump = 8 // pump is connected to pin 8
int button = 2; // button is connected to pin 2
int val; // variable for reading the pin status
int val2; // variable for reading the delayed/debounced status
int buttonState; // variable to hold the button state
int buttonMode = 0; // Is valve on or off?
void setup() {
pinMode(feedValve, OUTPUT); // Set the Feed Valve pin as output
pinMode(returnValve, OUTPUT); // Set the Return Valve pin as output
pinMode(heater, OUTPUT); // Set the Heater Valve pin as output
//pinMode(loopValves, OUTPUT); // Set the Loop Valves pin as output
//pinMode(pump, OUTPUT): // Set Pump pin as output
pinMode(button, INPUT); // Set the switch pin as input
Serial.begin(9600); // Set up serial communication at 9600bps
buttonState = digitalRead(button); // read the initial state
}
double Thermister(int RawADC) {
double Temp;
Temp = log(((10240000/RawADC) - 10000));
Temp = 1 / (0.001129148 + (0.000234125 * Temp) + (0.0000000876741 * Temp * Temp * Temp));
Temp = Temp - 273.15; // Convert Kelvin to Celcius
Temp = (Temp * 1.8) + 32.0; // Convert to F
return Temp;
}
void loop(){
val = digitalRead(button); // read input value and store it in val
delay(10);
val2 = digitalRead(button); // read the input again to check for bounces
if (val == val2) { // make sure we got 2 consistant readings!
if (val != buttonState) { // the button state has changed!
if (val == LOW) { // check if the button is pressed
if (buttonMode == 0) { // is the button off?
buttonMode = 1; // turn buttonMode to 1
} else {
if (buttonMode == 1){
buttonMode = 0; // turn buttonMode to 0
}
}
}
buttonState = val;
}
if (buttonMode == 1) {
double Temp = Thermister(analogRead(0)); // read Thermistor
//Serial.println(Temp);
if (Temp > 90){
digitalWrite(feedValve, HIGH); // turn on feed valve
delay(5000);
digitalWrite(returnValve, HIGH); // turn on return valve
}
if (Temp < 100)digitalWrite(heater, HIGH); // turn heater on
else if (Temp > 105)digitalWrite(heater, LOW); // turn heater off
}
if (buttonMode == 0) {
digitalWrite(heater, LOW); // turn heater off
digitalWrite(feedValve, LOW); // turn feed valve off
delay(5000);
digitalWrite(returnValve, LOW); // turn return valve off
}
}
}