From b8114e3832f20b9dee83a35acc09b6b0743b9647 Mon Sep 17 00:00:00 2001 From: Unknown Date: Sat, 25 May 2019 23:36:25 +0200 Subject: [PATCH] First version including eeprom --- remote.ino | 114 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 remote.ino diff --git a/remote.ino b/remote.ino new file mode 100644 index 0000000..eb53ab8 --- /dev/null +++ b/remote.ino @@ -0,0 +1,114 @@ +#include + +// start reading from the first byte (address 0) of the EEPROM +int address = 0; +byte value; + +const int analogInPin = A0; + +int sensorValue = 0; +int outputValue = 0; + +//volatile int center[6] = {0, 0, 0, 0, 0, 0}; +volatile int minvalue[6] = {1000, 1000, 1000, 1000, 1000, 1000}; +volatile int maxvalue[6] = {0, 0, 0, 0, 0, 0}; + +void setup() { + Serial.begin(115200); + + value = EEPROM.read(address); + + if (value != 0) + { + address++; + for (int i = 0; i < 6; i++) + { + address += sizeof(int); + EEPROM.get(address, minvalue[i]); + address += sizeof(int); + EEPROM.get(address, maxvalue[i]); + // Serial.println(minvalue[i]); + // Serial.println(maxvalue[i]); + } + } + else + { + // if eeprom empty + delay(500); + + // for (int i = 0; i < 6; i++) + // { + // center[i] = analogRead(analogInPin + i); + // } + + int time = 10000; + while (millis() < time) + { + for (int i = 0; i < 6; i++) + { + sensorValue = analogRead(analogInPin + i); + if (sensorValue > maxvalue[i]) { + maxvalue[i] = sensorValue; + } + if (sensorValue < minvalue[i]) { + minvalue[i] = sensorValue; + } + } + } + + // for (int i = 0; i < 6; i++) + // { + // minvalue[i] = center[i] - minvalue[i]; + // maxvalue[i] = (maxvalue[i] - center[i]); + // } + + // write callibration to eeprom + address = 0; + EEPROM.write(address, 1); + + address++; + for (int i = 0; i < 6; i++) + { + address += sizeof(int); + EEPROM.put(address, minvalue[i]); + address += sizeof(int); + EEPROM.put(address, maxvalue[i]); + // Serial.println(minvalue[i]); + // Serial.println(maxvalue[i]); + } + } +} + +void loop() { + for (int i = 0; i < 6; i++) + { + sensorValue = analogRead(analogInPin + i); + + + if (sensorValue > maxvalue[i]) + maxvalue[i] = sensorValue; + if (sensorValue < minvalue[i]) + minvalue[i] = sensorValue; + + outputValue = map(sensorValue, minvalue[i], maxvalue[i], 0, 255); + outputValue = sensorValue > maxvalue[i] ? 255 : sensorValue < minvalue[i] ? 0 : outputValue; + // if (sensorValue <= center[i]) + // { + // outputValue = map(sensorValue, minvalue[i], center[i], 0, 127); + // } + // else + // { + // outputValue = map(sensorValue, center[i], maxvalue[i], 128, 255); + // } + + Serial.print(i); + Serial.print("_"); + Serial.print(outputValue); + if (i != 5) + Serial.print(","); + // Serial.print(" "); + } + Serial.println(); + + delay(20); +}