right i think i will start again with what i have at mo, what i want to do and what i was thinking of doing
what i have at mo:
i have a DS18B20 temp sensor display the temp on a OLED and a high and low set point what can be adjusted using a rotary encoder. when the temp is outside the high and low set point pin13 will altar
// WATCHDOG
#include <avr/io.h>
#include <avr/wdt.h>
int encoderPin1 = 2;
int encoderPin2 = 3;
int buttonPin = 4;
int led = 13;
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
volatile int lastEncoded = 0;
volatile float encoderValue = 15;
volatile float encoderValue2 = 33;
long lastencoderValue = 0;
int lastMSB = 0;
int lastLSB = 0;
// TEMP
#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h>
/*-----( Declare Constants )-----*/
#define ONE_WIRE_BUS 7 /*-(Connect to Pin 7 )-*/
/*-----( Declare objects )-----*/
/* Set up a oneWire instance to communicate with any OneWire device*/
OneWire ourWire(ONE_WIRE_BUS);
/* Tell Dallas Temperature Library to use oneWire Library */
DallasTemperature sensors(&ourWire);
// LCD
#define _Digole_Serial_UART_ //To tell compiler compile the special communication only,
//all available are:_Digole_Serial_UART_, _Digole_Serial_I2C_ and _Digole_Serial_SPI_
#include <DigoleSerial.h>
//--------UART setup, if you don't use UART, use // to comment following line
#if defined(_Digole_Serial_UART_)
DigoleSerialDisp mydisp(&Serial, 9600); //UART:Arduino UNO: Pin 1(TX)on arduino to RX on module
#endif
#define LCDCol 16
#define LCDRow 2
const unsigned char fonts[] = {
6, 10, 18, 51, 120, 123};
void setup()
{
MCUSR=0;
wdt_enable(WDTO_8S);
pinMode(led, OUTPUT);
pinMode(encoderPin1, INPUT);
pinMode(encoderPin2, INPUT);
pinMode(buttonPin, INPUT);
digitalWrite(encoderPin1, HIGH); //turn pullup resistor on
digitalWrite(encoderPin2, HIGH); //turn pullup resistor on
attachInterrupt(0, updateEncoder, CHANGE);
attachInterrupt(1, updateEncoder, CHANGE);
mydisp.begin();
delay(1000);
mydisp.clearScreen(); //CLear screen
mydisp.drawStr(5, 0, "Hello"); //display string at: x=5, y=0
delay(1000);
mydisp.clearScreen(); //CLear screen
mydisp.drawStr(0, 0, "Temp:");
mydisp.drawStr(0, 1, "Set LO:");
mydisp.drawStr(0, 2, "Set HI:");
sensors.begin(); // Start up the DallasTemperature library
}
void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter++;
Serial.println(buttonPushCounter);
}
}
lastButtonState = buttonState;
sensors.requestTemperatures(); // Send the command to get temperatures
mydisp.setPrintPos(7, 0);
mydisp.print(sensors.getTempCByIndex(0)); // display a char array
mydisp.setPrintPos(7, 1);
mydisp.print(encoderValue); // display a char array
mydisp.setPrintPos(7, 2);
mydisp.print(encoderValue2); // display a char array
if (buttonPushCounter % 2 == 0) {
mydisp.drawStr(13, 1, "*");
} else {
mydisp.drawStr(13, 1, " ");
}
if (buttonPushCounter % 2 == 1) {
mydisp.drawStr(13, 2, "*");
} else {
mydisp.drawStr(13, 2, " ");
}
if (encoderValue < sensors.getTempCByIndex(0) && encoderValue2 > sensors.getTempCByIndex(0)) {
digitalWrite(led, HIGH);
}
else {
digitalWrite(led, LOW);
}
wdt_reset();
}
void updateEncoder(){
int MSB = digitalRead(encoderPin1); //MSB = most significant bit
int LSB = digitalRead(encoderPin2); //LSB = least significant bit
int encoded = (MSB << 2) |LSB; //converting the 2 pin value to single number
int sum = (lastEncoded << 2) | encoded; //adding it to the previous encoded value
if (buttonPushCounter % 2 == 0) {
if(sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011) encoderValue = encoderValue + 0.1;
if(sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) encoderValue = encoderValue - 0.1;
}
if (buttonPushCounter % 2 == 1) {
if(sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011) encoderValue2 = encoderValue2 + 0.1;
if(sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) encoderValue2 = encoderValue2 - 0.1;
}
lastEncoded = encoded; //store this value for next time
}
what i want to do:
when i power up my high and low set point comes up with 15 and 30 because of the valve below
volatile float encoderValue = 15;
volatile float encoderValue2 = 33;
so if i adjest them and the watchdog reset they go back to 15 and 30
what i was thinking of doing:
well what i was thinking is when i power up it looks at the eeprom for its high and low and when the watchdog comes to reset it save the new high and low to the eeprom.
what do you think is a good way of going about this or is there a easier or better way of doing this?
thanks Joe