Hey there,
i have trouble with writing a integer value into the eeprom.
I use the BlendMicro Board: http://redbearlab.com/blendmicro/
I want to save an integer to the eeprom. I use the put() and get() command for that. That's correct, isn't it? But when I read the written eeprom, i get totally wrong values (like 21720 instead of 1200).
I tryed to
Bit more explanation:
Basically I send values from my Android phone to the arduino via Bluetooth to control servos. The first 2 digits are the channel, the other 4 the value for the servo (integer, from 1000 to 2000. This is pwm signal in milliseconds).
Additionally I want the arduino to know the maximum and minimum position of the servos to avoid damage (due to lost of signal, whatever).
So I send them to the arduino and want to store them in the eeprom. Everytime I start the arduino the eeprom shall be read.
explanation of the variables:
ivalue: integer,
ichannel: integer,
Servo1min, etc: integer, minimal or maximal position of the servo
Here is my code:
(The part of the code where I write the data to the eeprom is quite at the end)
#include <SPI.h>
#include <EEPROM.h>
#include <boards.h>
#include <RBL_nRF8001.h>
#include <Servo.h>
#define SERVO_PIN_1 9
#define SERVO_PIN_2 10
#define LED_PIN1 11
#define LED_PIN2 12
#define LED_PIN3 13
#define LED_PIN4 A0
//pin 9, 10: no analog write(pwm) if Servo used!!!!
byte bdata0 = 0;
byte bdata1 = 0;
byte bdata2 = 0;
byte bdata3 = 0;
byte bdata_all = 0;
long ldata_all;
String sdata_all = "0";
String schannel = "0";
int ichannel = 0;
int ivalue = 0;
int Servo1min = 1400;
int Servo1mid = 1500;
int Servo1max = 1600;
int Servo2min = 1400;
int Servo2mid = 1500;
int Servo2max = 1600;
Servo Servo1;
Servo Servo2;
void setup() {
ble_begin();
Serial.begin(57600);
ble_set_name("BLEtest3");
// pinMode(TEST_PIN1, OUTPUT);
Servo1.attach(SERVO_PIN_1);
Servo2.attach(SERVO_PIN_2);
pinMode(LED_PIN1, OUTPUT);
pinMode(LED_PIN2, OUTPUT);
pinMode(LED_PIN3, OUTPUT);
pinMode(LED_PIN4, OUTPUT);
digitalWrite(LED_PIN1, HIGH);
delay(5000);//time to open serial after connecting to computer
//read eeprom
EEPROM.get(1, Servo1min );
EEPROM.get(2, Servo1mid );
EEPROM.get(3, Servo1max );
EEPROM.get(4, Servo2min );
EEPROM.get(5, Servo2mid );
EEPROM.get(6, Servo2max );
Servo1.writeMicroseconds(Servo1mid);
Servo2.writeMicroseconds(Servo2mid);
Serial.println(Servo1min);
Serial.println(Servo1mid);
Serial.println(Servo1max);
Serial.println(Servo2min);
Serial.println(Servo2mid);
Serial.println(Servo2max);
digitalWrite(LED_PIN1, LOW);
//digitalWrite(TEST_PIN1, HIGH);
}
void loop() {
while (ble_available()) {
bdata0 = ble_read();//reading all data. it must be String. Data has 2-4 digits: 1st one always indicates "channel" (1-9), the following the (pwm) value. Android sends 4byte blocks: To send numbers bigger than 255, after 1st byte is "filled" (255) the 2nd get 1 and the 1st one starts at 0 again. then the second gets 2, and so on, till it reaches 255.
bdata1 = ble_read();
bdata2 = ble_read();
bdata3 = ble_read();
/*Serial.println(bdata0);//print for control
Serial.println(bdata1);
Serial.println(bdata2);
Serial.println(bdata3);
*/
ldata_all = bdata0 + (bdata1 * 256L) + (bdata2 * 65536L); // + (bdata3 * 16777216); // "connect" data0 and data1 is quite easy, but didn't found out how to handle data2 and data3. Anyway, we don't need higher numbers (3 for pwm and 1-2 left for channel (max 60 channels possible - that's enough:D )
//Serial.println("ldata_all");
//Serial.println(ldata_all);
sdata_all = ldata_all;
//schannel = sdata_all.charAt(0);//saving channel in int
schannel = sdata_all.substring(0, 2);
ichannel = schannel.toInt();
Serial.println("ichannel");
Serial.println(ichannel);
sdata_all.remove(0, 2);//remove 1st two digits, so just the value remains
ivalue = sdata_all.toInt();//convert back to int :D
Serial.println("ivalue");
Serial.println(ivalue);//print for control
switch (ichannel) {
case 11://if channel 1 do:
if ((ivalue > Servo1min) && (ivalue < Servo1max)) {
Servo1.writeMicroseconds(ivalue);
}
break;
case 12://if channel 2 do:
if ((ivalue > Servo2min) && (ivalue < Servo2max)) {
Servo2.writeMicroseconds(ivalue);
}
break;
case 13://if channel 3 do:
if (sdata_all.substring(0, 1) == "2") {
digitalWrite(LED_PIN1, HIGH);
}
else {
digitalWrite(LED_PIN1, LOW);
}
if (sdata_all.substring(1, 2) == "2") {
digitalWrite(LED_PIN2, HIGH);
}
else {
digitalWrite(LED_PIN2, LOW);
}
if (sdata_all.substring(2, 3) == "2") {
digitalWrite(LED_PIN3, HIGH);
}
else {
digitalWrite(LED_PIN3, LOW);
}
if (sdata_all.substring(3, 4) == "2") {
digitalWrite(LED_PIN4, HIGH);
}
else {
digitalWrite(LED_PIN4, LOW);
}
case 51:
Servo1min = ivalue;
EEPROM.put(1, Servo1min);
break;
case 52:
Servo1mid = ivalue;
EEPROM.put(2, Servo1mid);
break;
case 53:
EEPROM.put(3, ivalue);
Servo1max = ivalue;
break;
case 54:
EEPROM.put(4, ivalue);
Servo2min = ivalue;
break;
case 55:
EEPROM.put(5, ivalue);
Servo2mid = ivalue;
break;
case 56:
EEPROM.put(6, ivalue);
Servo2max = ivalue;
break;
break;
}
Serial.println("storage");
Serial.println(Servo1min);
Serial.println(Servo1mid);
Serial.println(Servo1max);
Serial.println(Servo2min);
Serial.println(Servo2mid);
Serial.println(Servo2max);
}
// Allow BLE Shield to send/receive data
ble_do_events();
}