I am reading an LDR sensor and in my script have a set value to compare it to.
When ldr value is above this, IR lights are off. if it goes below for x amount of time,the IR leds turn on
I want to be able to change the value it is compared to (int ldrLow) through a serial connection.
It won't change often, just for tweaking when needed.
After some fiddling, i got the serial stuff going (mainly m having a bitch-in with data types)
I am trying to store the value in EEPROM to save it during reboots.
When i send the set command, it sets ldrLow. When i send the get command (made for debugging) it returns the correct value. When i reset the board, a whole different number is returned from memory. it's doing my head in, help!
the code:
#include <EEPROM.h>
int ldrLow = 400;
String msg;
void serialEvent() {
Serial.println("serial things happened");
runSerial();
}
void runSerial() {
while (Serial.available()) {
delay(10);
if (Serial.available() > 0) {
char c = Serial.read();
if (c != '\n' && c != '\r') {
msg += c;
}
}
}
if (msg != "") {
if (msg.indexOf("setldr=") != -1) {
msg.remove(0, 7);
int lengte = msg.length();
if (lengte > 4) {
lengte = 4;
}
int what = msg.substring(0, lengte).toInt();
if (what != 0 && what < 1025) {
EEPROM.put(0, what);
ldrLow = what;
}
}
if (msg.indexOf("getldr") != -1) {
int tmp = 1;
EEPROM.get(0, tmp);
Serial.println(tmp);
}
//Serial.println (msg);
}
Serial.flush();
msg = "";
}
void setup() {
Serial.begin(115200);
EEPROM.begin(2);
EEPROM.get(0, ldrLow);
}
void loop() {
}
And the serial output. first i type setldr=600 then getldr, then a reset and another getldr..
serial things happened
600
serial things happened
600
SDK:2.2.2-dev(38a443e)/Core:3.0.2=30002000/lwIP:STABLE-2_1_2_RELEASE/glue:1.2-48-g7421258/BearSSL:6105635
serial things happened
1714512486