Hey all I have the following code in my Arduino IDE:
#include <EEPROM.h>
#include <ArduinoJson.h>
bool debuggin = true;
int brightness = 255;
int ambientLight = 30;
struct RGBLA {
uint8_t R;
uint8_t G;
uint8_t B;
uint8_t L;
uint8_t A;
};
void setup() {
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
EEPROM.begin(512);
readSavedSettings();
updateEEProm(255,255,255,200,35,true,false);
}
RGBLA readEEProm() {
int addr = 0;
RGBLA customVars;
EEPROM.get(addr, customVars);
return customVars;
}
void readSavedSettings() {
RGBLA returnedVars = readEEProm();
if (debuggin) { Serial.println("START readSavedSettings"); }
if (returnedVars.A == 00 || returnedVars.A == 0) {
if (debuggin) { Serial.println("'A' not found! Default 30"); }
} else {
ambientLight = returnedVars.A;
if (debuggin) {
Serial.print("'A' found! ");
Serial.println(ambientLight);
}
}
for(uint8_t i = 0; i < 3/*strip.numPixels()*/; i++) {
if (debuggin) {
Serial.print("LED ");
Serial.print(i);
Serial.print(" set to: R: ");
Serial.print((uint8_t)returnedVars.R);
Serial.print(", G: ");
Serial.print((uint8_t)returnedVars.G);
Serial.print(", B: ");
Serial.println((uint8_t)returnedVars.B);
}
}
if (debuggin) {
Serial.println("Turned off RGB LEDS");
Serial.println("END readSavedSettings");
}
}
void updateEEProm(uint8_t R,
uint8_t G,
uint8_t B,
uint8_t L,
uint8_t A,
bool saveRGBL,
bool saveA) {
int addr = 0;
RGBLA customVars;
//Save old json before clearing EEProm
EEPROM.get(addr, customVars);
//Clear EEProm
for (unsigned int i = 0; i < EEPROM.length(); i++) {
EEPROM.write(i, 0);
}
if (saveRGBL) {
//Get A value so that we can save it the same value
DynamicJsonBuffer jsonBuffer(JSON_OBJECT_SIZE(5));
JsonObject& root = jsonBuffer.createObject();
if (debuggin) {
Serial.print("Old JSON:");
Serial.print(" R: ");
Serial.print((uint8_t)customVars.R);
Serial.print(" G: ");
Serial.print((uint8_t)customVars.G);
Serial.print(" B: ");
Serial.print((uint8_t)customVars.B);
Serial.print(" L: ");
Serial.print((uint8_t)customVars.L);
Serial.print(" A: ");
Serial.println((uint8_t)customVars.A);
}
root["R"] = (uint8_t)R;
root["G"] = (uint8_t)G;
root["B"] = (uint8_t)B;
root["L"] = (uint8_t)L;
root["A"] = (uint8_t)customVars.A;
if (debuggin) {
Serial.print("New JSON:");
Serial.print(" R: ");
Serial.print((uint8_t)root["R"]);
Serial.print(" G: ");
Serial.print((uint8_t)root["G"]);
Serial.print(" B: ");
Serial.print((uint8_t)root["B"]);
Serial.print(" L: ");
Serial.print((uint8_t)root["L"]);
Serial.print(" A: ");
Serial.println((uint8_t)root["A"]);
}
EEPROM.put(addr, root);
}
if (saveA) {
//Get R,G,B,L values so that we can save it the same value
DynamicJsonBuffer jsonBuffer(JSON_OBJECT_SIZE(5));
JsonObject& root = jsonBuffer.createObject();
if (debuggin) {
Serial.print("Old JSON:");
Serial.print(" R: ");
Serial.print((uint8_t)customVars.R);
Serial.print(" G: ");
Serial.print((uint8_t)customVars.G);
Serial.print(" B: ");
Serial.print((uint8_t)customVars.B);
Serial.print(" L: ");
Serial.print((uint8_t)customVars.L);
Serial.print(" A: ");
Serial.println((uint8_t)customVars.A);
}
root["R"] = (uint8_t)customVars.R;
root["G"] = (uint8_t)customVars.G;
root["B"] = (uint8_t)customVars.B;
root["L"] = (uint8_t)customVars.L;
root["A"] = (uint8_t)A;
if (debuggin) {
Serial.print("New JSON:");
Serial.print(" R: ");
Serial.print((uint8_t)root["R"]);
Serial.print(" G: ");
Serial.print((uint8_t)root["G"]);
Serial.print(" B: ");
Serial.print((uint8_t)root["B"]);
Serial.print(" L: ");
Serial.print((uint8_t)root["L"]);
Serial.print(" A: ");
Serial.println((uint8_t)root["A"]);
}
EEPROM.put(addr, root);
}
}
void loop() {
}
And this is the output of that code:
START readSavedSettings
'A' found! 96
LED 0 set to: R: 48, G: 255, B: 255
LED 1 set to: R: 48, G: 255, B: 255
LED 2 set to: R: 48, G: 255, B: 255
Turned off RGB LEDS
END readSavedSettings
Old JSON: R: 48 G: 255 B: 255 L: 63 A: 96
New JSON: R: 255 G: 255 B: 255 L: 200 A: 96
Old JSON: R: 48 G: 255 B: 255 L: 63 A: 160
New JSON: R: 48 G: 255 B: 255 L: 63 A: 35
Now when I reset it this is the output:
⸮dOX⸮,R⸮X⸮4xC⸮⸮⸮START readSavedSettings
'A' found! 96
LED 0 set to: R: 48, G: 255, B: 255
LED 1 set to: R: 48, G: 255, B: 255
LED 2 set to: R: 48, G: 255, B: 255
Turned off RGB LEDS
END readSavedSettings
Old JSON: R: 48 G: 255 B: 255 L: 63 A: 96
New JSON: R: 255 G: 255 B: 255 L: 200 A: 96
Old JSON: R: 48 G: 255 B: 255 L: 63 A: 160
New JSON: R: 48 G: 255 B: 255 L: 63 A: 35
How come the Old JSON has the 'L' value as 63 again when it should be the New JSON value of 200? Why did it not save it but saved all the others? Also, Why is 'R' 48 when it should be 255 like G and B?
Is there something wrong in my code?