Hi Forum
I am new here and new to Arduino.
But never the less I have started building a wood stove controller.
The controlling is done by servos controlling the air vents in regards to the chimney and room temperature.
I have some issues,
I would like to do different actions depending if the temperature is increasing or decreasing. ?
And I cant seem to find any examples on the www.
Also I need to change servo direction. Can that be done by some mapping command or how is it done ?
As I said I am new to this, and my sketch is made up of a lot of snips, from around the www.
Therefor probably not the prettiest code around.
But it pretty much does what I want it to do.
There is still a lot to do, clean up, write in servo2 and so on. But here it is.
// Stove Control ver: 1,3
// Include Library Code
#include <max6675.h>
#include <DallasTemperature.h>
#include <OneWire.h>
#include <LiquidCrystal.h>
#include <Servo.h>
#include <PID_v1.h>
// define integers
int x = 0;
int setBeepBuzzer = 1;
// int setStoveMode = 0; //New
int muteBuzzer = LOW;
int muteButtonLight = LOW;
int overTemp = LOW;
int stoveMode = HIGH;
// int stoveMode2 = LOW; //New
int glowMode = LOW; //New
int stoveShutdown = LOW;
double stoveRunTemp = 140;
// double stoveRunTemp2 = 120; //New
int overtempSetPoint = 180;
int servo1Pos = 11;
int servo2Pos = 22;
int roomTempSetPoint = 23;
// int glowModeSetting = 35; //New
int minServo1Setting = 41;
int minServo2Setting = 32;
int stoveShutdownSetting = 2;
double cdiff = 0;
double roomTemp = 23;
double c = 0;
int cInt = c;
int cfg = 0;
double Output;
double cnew;
int stoveRunTempInt = stoveRunTemp;
// int stoveRunTemp2Int = stoveRunTemp2; //New
// define pin constants
// Pin 2-7 assigned to LCD
const int rs = 2, en = 3, d4 = 4, d5 = 5, d6 = 6, d7 = 7; // initialize the library by associating any needed LCD interface pin
LiquidCrystal lcd(rs, en, d4, d5, d6, d7); // with the arduino pin number it is connected to
const int buzzerPin = 8;
const int RoomTemp = 9;
const int muteButtonLightPin = 10;
const int servo1Pin = 11;
const int servo2Pin = 12;
const int buttonRoomTempSetPointUp = 14;
const int buttonRoomTempSetPointDown = 15;
const int buttonMutePin =16;
const int buttonStoveTempSetPointUp = 17;
const int buttonStoveTempSetPointDown = 18;
// ThermoCouple // MAX6675 pin assignment
const int thermo_gnd_pin = 45;
const int thermo_vcc_pin = 47;
const int thermo_sck_pin = 49;
const int thermo_cs_pin = 51;
const int thermo_so_pin = 53;
int count = 0;
MAX6675 thermocouple(thermo_sck_pin, thermo_cs_pin, thermo_so_pin);
Servo servo1; //defining servos
Servo servo2;
PID myPID(&c, &Output, &stoveRunTemp, 4, .2, 1, DIRECT);
#define ONE_WIRE_BUS 9 // Data wire is plugged into port 9 on the Arduino
OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature.
void setup() {
servo1.attach(servo1Pin,560,2350); // (pin, min, max)
servo2.attach(servo2Pin,600,2380); // (pin, min, max)
pinMode(buzzerPin, OUTPUT);
pinMode(muteButtonLightPin, OUTPUT);
pinMode(buttonRoomTempSetPointUp, INPUT);
pinMode(buttonRoomTempSetPointDown, INPUT);
pinMode(buttonMutePin, INPUT);
pinMode(buttonStoveTempSetPointUp, INPUT);
pinMode(buttonStoveTempSetPointDown, INPUT);
Serial.begin(9600);
pinMode(thermo_vcc_pin, OUTPUT);digitalWrite(thermo_vcc_pin, HIGH);
pinMode(thermo_gnd_pin, OUTPUT);digitalWrite(thermo_gnd_pin, LOW);
Serial.println("Balle: MAX6675 test");
delay(500); // wait for MAX chip to stabilize
lcd.begin(16, 2);
servo1.write(180);
servo2.write(180);
myPID.SetOutputLimits(minServo1Setting, 180);
// myPID.SetOutputLimits(minServo2Setting, 180);
myPID.SetMode(AUTOMATIC);
// 1234567890123456
lcd.print("* Stove Robot *");
lcd.setCursor(0, 1);
// 1234567890123456
lcd.print("* by BALLE *");
// wait for MAX chip to stabilize
delay(500);
}
void loop() {
// basic serial readout and sensor test, print the current temp
// Serial.print("F = ");
// Serial.print(thermocouple.readFahrenheit());
Serial.print(" C = ");
Serial.print(thermocouple.readCelsius());
sensors.requestTemperatures(); //Send the command to get temperatures
float x=sensors.getTempCByIndex(0); //Serial.print("Temperature for the device 1 (index 0) is: ");
Serial.print(" C1= "); //Serial.println(x);
Serial.println(sensors.getTempCByIndex(0));
roomTemp = (sensors.getTempCByIndex(0));
delay(1000);
digitalWrite(muteButtonLightPin, HIGH); //turn off muteButtonLight
// thermocouple check
thermocouple.readCelsius();
cnew = thermocouple.readCelsius();
while (isnan(cnew)) {
cnew = thermocouple.readCelsius();
lcd.setCursor(0, 1);
lcd.print("* T/C Problem *");
digitalWrite(muteButtonLightPin, LOW); //blink muteButtonLight
delay(1200);
lcd.setCursor(0, 1);
lcd.print("* *");
digitalWrite(muteButtonLightPin, HIGH); //blink MuteButtonLight
delay(500);
}
cdiff = cnew - c;
c = cnew;
cInt = c;
// Compute PID
myPID.Compute();
//****************************************************
// Servo control section
// servoPos = 0 then damper is closed
// servoPos = 180 then damper is fully opened
if (!stoveShutdown && c <= roomTemp + 20) { // close the damper if the fire is out
stoveShutdown = HIGH; //LOW or HIGH ?
}
if (stoveShutdown && c >= roomTemp +25) { // open the damper when the fire is started
stoveShutdown = LOW;
}
if (overTemp == HIGH) {
stoveMode = LOW;
}
if (roomTemp <= roomTempSetPoint -.5) {
stoveMode = HIGH;
}
if (roomTemp >= roomTempSetPoint +.5 && stoveRunTemp >= 140) {
stoveRunTemp = stoveRunTemp -20;
}
if (stoveShutdown) {
servo1.write(stoveShutdownSetting);
servo1Pos = stoveShutdownSetting;
}
else {
if (stoveMode) {
servo1.write(Output);
servo1Pos = Output;
}
else
{
servo1.write(minServo1Setting);
servo1Pos = minServo1Setting;
}
}
// input loop for 5 seconds
for (int y = 0; y < 50; y++) {
// read mute button
if (muteBuzzer == LOW) { // HIGH or LOW ?
muteBuzzer = digitalRead(buttonMutePin);
}
// buzzer overtemp alarm section
if (muteBuzzer) {
if (c < overtempSetPoint) { //resets the mute buzzer variable if stove temp goes below overtemp
muteBuzzer = LOW; // HIGH or LOW ?
overTemp = LOW;
}
digitalWrite(buzzerPin, HIGH);
}
else
{
if (c > overtempSetPoint) {
digitalWrite(buzzerPin, LOW);
digitalWrite(muteButtonLightPin, LOW);
overTemp = HIGH;
}
else
{
digitalWrite(buzzerPin, HIGH);
digitalWrite(muteButtonLightPin, HIGH);
overTemp = LOW;
}
}