I am creating a pond controller with an arduino nano That communicates over bluetooth to a mit app inventor app. I have the app send the start and stop times for the 4 electrical ports and then the arduino saves them to eeprom so if the power goes out it saves the programed times. Every loop i call EEPROM.update and give it the new data points, but when I read them back they are completely different numbers.
#include <EEPROM.h>
#include <Wire.h>
#include "RTClib.h"
#define Output1 4
#define Output2 5
#define Output3 6
#define Output4 7
#define R 10
#define G 11
#define B 9
#define DeBug 8
int Update;
int P1State = 2;
int P2State = 2;
int P3State = 2;
int P4State = 2;
int P1;
int P2;
int P3;
int P4;
int RR;
int GG;
int BB;
int Start1;
int End1;
int Start2;
int End2;
int Start3;
int End3;
int Start4;
int End4;
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars];
boolean newData = false;
RTC_DS1307 RTC;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Wire.begin();
RTC.begin();
pinMode (DeBug, INPUT);
pinMode (Output1, OUTPUT); //Sets Port 1-4 As Outputs
pinMode (Output2, OUTPUT);
pinMode (Output3, OUTPUT);
pinMode (Output4, OUTPUT);
pinMode (R, OUTPUT); //Sets RGB Pins As Outputs
pinMode (G, OUTPUT);
pinMode (B, OUTPUT);
pinMode (13, OUTPUT);
digitalWrite(Output1,HIGH);
digitalWrite(Output2,HIGH);
digitalWrite(Output3,HIGH);
digitalWrite(Output4,HIGH);
if (! RTC.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
}
RTC.adjust(DateTime(__DATE__, "10:59:30"));
EEPROM.get(1, Start1);
EEPROM.get(2, End1);
EEPROM.get(3, Start2);
EEPROM.get(4, End2);
EEPROM.get(5, Start3);
EEPROM.get(6, End3);
EEPROM.get(7, Start4);
EEPROM.get(8, End4);
Serial.println("Start Up Done");
}
void loop() {
// put your main code here, to run repeatedly:
DateTime now = RTC.now();
recvWithStartEndMarkers();
if (newData == true) {
strcpy(tempChars, receivedChars);
// this temporary copy is necessary to protect the original data
// because strtok() used in parseData() replaces the commas with \0
parseData();
showParsedData();
newData = false;
}
/*
Serial.print("Hour-");
Serial.print(now.hour());
Serial.print(" Minute-");
Serial.print(now.minute());
Serial.print(" Second-");
Serial.println(now.second());
*/
Serial.print("Start1-");
Serial.println(Start1);
Serial.print("Start1.1-");
Serial.println(EEPROM.get(1,P1State));
Serial.print("Start2-");
Serial.println(Start2);
Serial.print("Start2.1-");
Serial.println(EEPROM.get(3,P2State));
Serial.print("Start3-");
Serial.println(Start3);
Serial.print("Start3.1-");
Serial.println(EEPROM.get(5,P3State));
Serial.print("Start4-");
Serial.println(Start4);
Serial.print("Start4.1-");
Serial.println(EEPROM.get(7,P4State));
EEPROM.update(1, Start1);
EEPROM.update(2, End1);
EEPROM.update(3, Start2);
EEPROM.update(4, End2);
EEPROM.update(5, Start3);
EEPROM.update(6, End3);
EEPROM.update(7, Start4);
EEPROM.update(8, End4);
if (now.hour() == Start1 && now.minute() < 1) {
Serial.print("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
digitalWrite(Output1,HIGH);
}
if (now.hour() == End1 && now.minute() < 1) {
Serial.print("End1");
digitalWrite(Output1,LOW);
}
if (now.hour() == Start2 && now.minute() < 1) {
digitalWrite(Output2,HIGH);
}
if (now.hour() == End2 && now.minute() < 1) {
digitalWrite(Output2,LOW);
}
if (now.hour() == Start3 && now.minute() < 1) {
digitalWrite(Output3,HIGH);
}
if (now.hour() == End3 && now.minute() < 1) {
digitalWrite(Output3,LOW);
}
if (now.hour() == Start4 && now.minute() < 1) {
digitalWrite(Output4,HIGH);
}
if (now.hour() == End4 && now.minute() < 1) {
digitalWrite(Output4,LOW);
}
if (P1 == 1) {
digitalWrite(Output1, LOW);
P1State = 1;
}
else {
digitalWrite(Output1, HIGH);
P1State = 2;
}
if (P2 == 1) {
digitalWrite(Output2, LOW);
P2State = 1;
}
else {
digitalWrite(Output2, HIGH);
P2State = 2;
}
if (P3 == 1) {
digitalWrite(Output3, LOW);
P3State = 1;
}
else {
digitalWrite(Output3, HIGH);
P3State = 2;
}
if (P4 == 1) {
digitalWrite(Output4, LOW);
P4State = 1;
}
else {
digitalWrite(Output4, HIGH);
P4State = 2;
}
analogWrite(R, RR);
analogWrite(G, GG);
analogWrite(B, BB);
if (digitalRead(DeBug) == HIGH) {
Serial.print("This just in ... ");
Serial.println(receivedChars);
}
delay(100);
}
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
void parseData() { // split the data into its parts
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(tempChars, ","); // get the first part - the string
P1 = atoi(strtokIndx); // copy it to messageFromPC
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
P2 = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
P3 = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
P4 = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
RR = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
GG = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
BB = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
Start1 = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
End1 = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
Start2 = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
End2 = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
Start3 = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
End3 = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
Start4 = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
End4 = atoi(strtokIndx); // convert this part to an integer
Serial.print(P1State);
Serial.print(",");
Serial.print(P2State);
Serial.print(",");
Serial.print(P3State);
Serial.print(",");
Serial.print(P4State);
Serial.print(",");
}
//============
void showParsedData() {
//Serial.print(BB);
}
I made a output on the serial monitor to show what it gets from the phone and what it reads from eeprom.
Start1-12
Start1.1-1292
Start2-6
Start2.1-3334
Start3-18
Start3.1-6162
Start4-0
Start4.1-0
Start1-12
Start1.1-1292
Start2-6
Start2.1-3334
Start3-18
Start3.1-6162
Start4-0
Start4.1-0
Start1 is from phone and start1.1 is from the eeprom.