Store Long into Arduino Nano EEPROM

Hello together,

I am a beginner in C++ and Arduino and need some help to put and get a Long number into the EEPROM. I have read and tried already the examples who comes in the Arduino IDE and also maked some Google search. But until now I can´t get it work.

I have a Code to contoll a conveyor with a Arduino Nano who is working how i want it. The conveyor turns clock and anticlock wise. On each side of the conveyor are sensors who controll the position. When one of the sides is reached the motor stopps and waits a certain time until it begun the reverse turning. For each cycle there is a counter inside the code to control and stop the conveyor. When this reached a predefined maxcycles the equipment stops completely.
The actual value and target value of the counter i defined as “unsigned long” variable.
Like i told the code is working how it´s right now. Only i don´t want to loose the expired cycle numbers when i turn off the Arduino from the power supply. For that i would need to stored it on the EEPROM.

I found a example how to write and read a Long into the EEPROM but i can´t put it work with my existing Code.

Follows the Example Code that i found:

#include <EEPROM.h>

//------------------------------------------------
void writeLongIntoEEPROM(int address, long number)
{ 
  EEPROM.write(address, (number >> 24) & 0xFF);
  EEPROM.write(address + 1, (number >> 16) & 0xFF);
  EEPROM.write(address + 2, (number >> 8) & 0xFF);
  EEPROM.write(address + 3, number & 0xFF);
}
long readLongFromEEPROM(int address)
{
  return ((long)EEPROM.read(address) << 24) +
         ((long)EEPROM.read(address + 1) << 16) +
         ((long)EEPROM.read(address + 2) << 8) +
         (long)EEPROM.read(address + 3);
}
//------------------------------------------------
void setup() {
  Serial.begin(9600);
  
  writeLongIntoEEPROM(100, 123456);
  long number = readLongFromEEPROM(100);
  
  Serial.print("Number: ");
  Serial.println(number);
}
//-------------------------------------------------
void loop() {}

Follows my Conveyor Code:

#include <EEPROM.h>

#define sensorleft     2  // Pin Sensor Left
#define sensorright    3  // Pin Sensor Right
#define startbutton    4  // Pin Start Button
#define stopbutton     5  // Pin Stop Button
#define relayLeft    11   // Pin Relay Left Turning
#define relayRight   12   // Pin Relay Right Turning

#define relayOn   0
#define relayOff  1

enum {waiting, on, right, stopright, left, stoplinks, off};
byte step = waiting;
unsigned long cycle = 0;
unsigned long maxcycles = 4; //Now it´s 4 to test. It will be 80000 Cycles

//----------------------------------------------------
void setup() {

  Serial.begin(9600);
  
  Serial.println(F("Start...."));
  pinMode(sensorleft, INPUT_PULLUP);
  pinMode(sensorright, INPUT_PULLUP);
  pinMode(startbutton, INPUT_PULLUP);
  pinMode(stopbutton, INPUT_PULLUP);
  pinMode(relayLeft, OUTPUT);
  digitalWrite(relayLeft, relayOff);
  pinMode(relayRight, OUTPUT);
  digitalWrite(relayRight, relayOff);
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);
}
//----------------------------------------------
void loop() {

  Serial.print(F("Cyclen:"));
  Serial.println(cycle);
  Serial.println(maxcycles);
  conveyor();
}

//--------------------------------------------------
void conveyor(){
  
    int startPin = digitalRead(startbutton);
    int stopPin = digitalRead(stopbutton);
    int sensorLinksEnd = digitalRead(sensorleft);
    int sensorrightEnd = digitalRead(sensorright);
    
    static unsigned long lastmillis;
    const unsigned long pauseTime = 5000; // in ms
  
  if (stopPin == LOW )
  {
    step = off;
  }
  switch (step)
  {
    case waiting:
      Serial.println(F("Equipment Waiting for Start()"));    
      if (startPin == LOW )
      {
        cycle = 0;
        step = on;
      }
      digitalWrite(relayRight, relayOff);
      digitalWrite(relayLeft, relayOff);
      break;
    case on:
      Serial.println(F("Equipment On()"));    
      digitalWrite(LED_BUILTIN, HIGH);
      step = right;
      break;
    case right:
      Serial.println(F("Right Turning ()"));    
      digitalWrite(relayRight, relayOn);
      if (sensorrightEnd == LOW )
      {
        digitalWrite(relayRight, relayOff);
        step = stopright;
        lastmillis = millis();
      }
      break;
    case stopright:
      Serial.println(F("Stop Right()"));    
      digitalWrite(relayRight, relayOff);
      digitalWrite(relayLeft, relayOff);
      if (millis() - lastmillis > pauseTime)
      {
        step = left;
      }
      break;
    case left:
      Serial.println(F("Left Turning()"));    
      digitalWrite(relayRight, relayOff);
      digitalWrite(relayLeft, relayOn);
      if (sensorLinksEnd == LOW )
      {
        digitalWrite(relayLeft, relayOff);
        step = stoplinks;
        lastmillis = millis();
      }
      break;
    case stoplinks:
      Serial.println(F("Stop Left()"));    
      digitalWrite(relayRight, relayOff);
      digitalWrite(relayLeft, relayOff);
      if (millis() - lastmillis > pauseTime)
      {
        if (cycle == maxcycles)
        {
          step = off;
        }
        else
        {
          cycle++; 
          step = right;
        }
      }
      break;
    case off:
      Serial.println(F("Equipment Off()"));    
      digitalWrite(LED_BUILTIN, LOW);
      step = waiting;
      break;
  }
}

I would be gratefull for some help and tips how to make this work.

use EEPROM.put() (and EEPROM.get() to read) and you won’t have to deal with the bytes

you can look also at this thread where I shared a simple code to deal with default values

@J-M-L thanks for the reply. I will take a look on the thread.

Some microcontrollers don’t have libraries that support put() and get() methods. It’s pretty easy to use a union to read and write EEPROM data, too:

#include <EEPROM.h>

union {
  char buffer[sizeof(long)];
  long bigVal;
}myUnion;

void setup() {
  int i;
  long test = 250000L;
  
  Serial.begin(9600);

  myUnion.bigVal = test;

  for (i = 0; i < sizeof(long); i++) {
    EEPROM.write(i, myUnion.buffer[i]); // Write the long one byte at a time
  }

  myUnion.bigVal = 0L;                  // Just clear the long

  for (i = 0; i < sizeof(long); i++) {
    myUnion.buffer[i] = EEPROM.read(i);  // Read back one byte at a time
  }
  Serial.print("The value of bigVal is: ");
  Serial.println(myUnion.bigVal);
}

void loop() {
}

The following presentation illustrates the mechanism of storing/retrieving “unsigned long int” type data (for example: 305419896 = 0x12345678) into four consecutive locations (100, 101, 102, 103) of EEPROM of ATmega328P MCU of Arduino NANO Board using EEPROM.put()/EEPROM.get() methods.

longInt
Figure-1:

Codes:

#include<EEPROM.h>
unsigned long int y = 305419896;//0x12345678
unsigned long int x;

void setup() 
{
  Serial.begin(9600);
  EEPROM.put(100, y);   //data is saved
  //-----------------------------------
  EEPROM.get(100, x);  //getting back data from EEPROM and save in x
  Serial.println(x, DEC); //shows: 305419896
  Serial.println(x, HEX);  //shows: 0x12345678

}

void loop() 
{
 
}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.