EEPROM.PUT "er there!"

Tired.

I want to collect voltage in 10bit form customary to the analogRead() function and store them in a "non-voliatile" register, hence EEPROM. The clunky conditional checks if the thing is attached to it's home, where it puts, or my computer where that pin6 is pulled up, and it gets. My data type is int long. I increment 8 bytes just to be "safe" and make this thing go easy the first time.

I just get 0's.

I haven't messed with these for two decades. I'm not adept.

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  eeAddress = 0; 
  pinMode(6, INPUT_PULLUP);
  pinMode(A2, INPUT);
  pinMode(A3, INPUT);

}

void loop() {
  // put your main code here, to run repeatedly:
  if (digitalRead(6) == LOW) {
    int long a2 = analogRead(A2);
       EEPROM.put(eeAddress, a2);
    eeAddress = eeAddress + 8;
    int long a3 = analogRead(A3);
    EEPROM.put(eeAddress, a3);
    eeAddress = eeAddress + 8;
  }
  if (digitalRead(6) == HIGH) {
    EEPROM.get(eeAddress, tot);
    Serial.print("A2: ");
    Serial.print(long(lngRead));
    eeAddress = eeAddress + 8;

    EEPROM.get(eeAddress, tot);
    Serial.print("  ...  A3: ");
    Serial.println(long(lngRead));
    eeAddress = eeAddress + 8;
  }
  delay(500);
}

OK. It's working half way

The analogRead() returns an integer.
analogRead() - Arduino Reference.
You can store that as a two-byte int. There is no need to overdo something, that only makes it confusing.

Your eeAddress is set to zero at setup(), after that it is incremented. That is not how to use a EEPROM location.

In the next snippets I assume that it is for an Arduino Uno (Nano/Mega/Micro) board and a 'int' is two bytes.

// Example of storing all of them.
for( int i=0; i<6; i++)   // channel 0...6
{
  int rawADC = analogRead( i);       // analogRead( i) or analogRead( i + A0)
  int offset = i * 2;    // two bytes per value
  EEPROM.put( offset, rawADC);
}


// Example to retrieve A2 from EEPROM
int i = 2;        // channel 2
int offset = i * 2;    // two bytes per value
int rawADC;
EEPROM.get( offset, rawADC);
Serial.println( rawADC);

mattlogue:
I just get 0's.

I haven't messed with these for two decades. I'm not adept.

you are stomping on your 32 bit value moving only one byte in EEPROM.

The EEPROM library can save an object:

#include "EEPROM.h"

constexpr int EEPROM_LOCATION = 0;

struct MyStoredData {
  int a2;
  int a3;
};

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(6, INPUT_PULLUP);
//  pinMode(A2, INPUT);
//  pinMode(A3, INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  if (digitalRead(6) == LOW) {
    MyStoredData readings;
    readings.a2 = analogRead(A2);
    readings.a3 = analogRead(A3);
    EEPROM.put(EEPROM_LOCATION, readings);
  }
  if (digitalRead(6) == HIGH) {
    MyStoredData stored;
    EEPROM.get(EEPROM_LOCATION, stored);
    Serial.print("A2: ");
    Serial.print(stored.a2);
    Serial.print("  ...  A3: ");
    Serial.println(stored.a3);
  }
  delay(500);
}

So, what did you change? What is working? etc

Some notes:

  • int long is a bit of a weird type... Just use a unsigned int
  • You don't reset the eeAddress when you want to read aka you continue reading where you left off writing.
  • You just keep on writing to memory indefinitely because you keep incrementing eeAddress
  • Don't know what the application is but do note the number of write cycles is limited...
  • You read back to 'tot' but print 'lngRead'

@BulldogLowell, he uses put() so he's storing it all. :wink:

Try printing eeaddress before using its value. Are you getting values from the same places that you put them ?