/*
Controlling a servo position using a potentiometer (variable resistor)
by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>
modified on 8 Nov 2013
by Scott Fitzgerald
http://www.arduino.cc/en/Tutorial/Knob
*/
#include <OneWire.h>
#include <Servo.h>
#include <LiquidCrystal_I2C.h>
#include <DallasTemperature.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 column and 2 rows
Servo myservo; // create servo object to control a servo
#define potPin 0
// Define to which pin of the Arduino the 1-Wire bus is connected:
#define ONE_WIRE_BUS 8
// Create a new instance of the oneWire class to communicate with any OneWire device:
OneWire oneWire(ONE_WIRE_BUS);
// Pass the oneWire reference to DallasTemperature library:
DallasTemperature sensors(&oneWire);
int val; // variable to read the value from the analog pin
const float BETA = 3950; // should match the Beta Coefficient of the thermistor
String degF = "\337F"; // degrees Farenheit symbol
int maxOpen = 4;
int lastSetF;
float lastF;
float lastDifference;
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
lcd.init(); // initialize the lcd
lcd.backlight();
// Start up the library:
sensors.begin();
Serial.begin(9600);
}
void loop() {
int setValue = analogRead(potPin);
// Send the command for all devices on the bus to perform a temperature conversion:
sensors.requestTemperatures();
// Fetch the temperature in degrees Celsius for device index:
float celsius = sensors.getTempCByIndex(0); // the index 0 refers to the first device
// Fetch the temperature in degrees Fahrenheit for device index:
float farenheit = sensors.getTempFByIndex(0);
int setTempF = map(setValue,0,1023,59,72);
if(lastSetF != setTempF || farenheit != lastF){
lcd.setCursor(0,0);
lcd.print("Set: ");
lcd.print(setTempF);
lcd.print(degF);
lcd.print(" | ");
lcd.print("Zone 1");
//lcd.print();
lcd.setCursor(0,1);
lcd.print("Cur: ");
lcd.print(round(farenheit));
lcd.print(degF);
lcd.print(" | ");
lcd.print(" 1");
//lcd.print(degC);
lastSetF = setTempF;
lastF = farenheit;
}
float difference = farenheit - setTempF;
if(difference >= maxOpen && lastDifference != difference){
myservo.write(180); // sets the servo position according to the scaled value
lastDifference = difference;
} else if( farenheit - setTempF >0){
lastDifference = difference;
float setPoint = (difference / maxOpen) * 90 + 90;
myservo.write(setPoint);
} else {
myservo.write(90);
lastDifference = difference;
}
}