Usb connection/disconnection and eeprom

Status
Not open for further replies.
Hi, this is domenico from italy and i love pjrc products! I've prepared a code that calibrates potentiometer and after that stores max and min values on teensy ++2 eeprom.
When i power off/on connecting/disconnecting teensy ground all stored values are correctly recalled by the code.
Instead when i connect disconnect the usb cable teensy works good except for eeprom because calibration values are not recalled correctly and i have to recalibrate everything.
Values are not deleted from eeprom since after usb cable connection if I disconnect/connect teensy ground all stored values are correctly recalled by the code.

Can you please help me on this?

Thank you,
Domenico
 
Here's the code:

#include <EEPROM.h>


const int sensorPin1 = A1;
const int ledPin1 = 26;
const int indicatorLedPin1 = 32;
const int buttonPin1 = 1;
int sensorMin1 ;
int sensorMax1 ;
int sensorValue1 = 0;


void setup() {

if(EEPROM.read(0)<2 && EEPROM.read(0)>=0)
{sensorMax1 =EEPROM.read(0)*100;sensorMax1 +=EEPROM.read(1);}
else
sensorMax1 =0;

if(EEPROM.read(2)<2 && EEPROM.read(2)>=0)
{sensorMin1 =EEPROM.read(2)*100;sensorMin1 +=EEPROM.read(3);}
else
sensorMin1 =1024; pinMode(indicatorLedPin1, OUTPUT);

pinMode (ledPin1, OUTPUT);
pinMode (buttonPin1, INPUT);

}

void loop() {

while (digitalRead(buttonPin1) == HIGH) {
calibrate1(); }


digitalWrite(indicatorLedPin1, LOW);
sensorValue1 = analogRead(sensorPin1);
sensorValue1 = map(sensorValue1, sensorMin1, sensorMax1, 0, 127);
sensorValue1 = constrain(sensorValue1, 0, 127);
analogWrite(ledPin1, sensorValue1);


}
void calibrate1() {

digitalWrite(indicatorLedPin1, HIGH);
sensorValue1 = analogRead(sensorPin1);

if (sensorValue1 > sensorMax1) {
sensorMax1 = sensorValue1;
EEPROM.write(0,sensorValue1/100);
EEPROM.write(1,sensorValue1%100);

}

if (sensorValue1 < sensorMin1) {
sensorMin1 = sensorValue1;
EEPROM.write(2,sensorValue1/100);
EEPROM.write(3,sensorValue1%100);
}
}
 
Could it be that your button pin reads logic high after rebooting even without the button pressed? Try to configure the button pin with INPUT_PULLUP, connect the button between the pin and ground making it active low. Then jump into the calibration routine when digitalRead(buttonPin1) == LOW. This should give more stable action.
 
Could it be that your button pin reads logic high after rebooting even without the button pressed? Try to configure the button pin with INPUT_PULLUP, connect the button between the pin and ground making it active low. Then jump into the calibration routine when digitalRead(buttonPin1) == LOW. This should give more stable action.


I'm sorry, I've tried but It doesn't work unfortunatelly.
Any other help ?
Thanks,
Domenico
 
Status
Not open for further replies.
Back
Top