Eeprom returning value 0

I have written a code to store number of steps taken by a motor when in set mode & save the value to EEPROM . And when in drive mode , the motor should run clock wise and ccw to the same number of steps by recalling the value stored . The code works fine with arduino and uno . But , on ESP32 . The stored value is returning 0

When using the EEPROM functions on the ESP32 you need a couple of extra commands.

The ESP32 does not have an EEPROM. The EEPROM library emulates the standard functionality... but data is actually stored in flash memory.

You need to use...

#define EEPROM_SIZE 5   // Define how many bytes you want to use.

Then in setup()...

  EEPROM.begin(EEPROM_SIZE);

Then after any updates...

    EEPROM.commit();

This is my code i have done all the three thing .


#include <Stepper.h>
#include<EEPROM.h>

const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
// for your motor

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution,27,26);

float stepCount = 0 ; 
// the setup function runs once when you press reset or power the board
//int swi = 4;
int rotationCW = 0;
int rotationCCW = 0;
int location = 1;
//int button1 = 13;
float val;
//const int En = 25;
void setup() 
{
  // initialize digital pin LED_BUILTIN as an output.
         Serial.begin(9600);
         EEPROM.begin(4);
          //pinMode(swi,INPUT);
        // pinMode(button1,INPUT);
          pinMode(2, OUTPUT);
          //pinMode(En,OUTPUT);
          
          //digitalWrite(swi,LOW);
          //digitalWrite(button1,LOW);
           


}



// the loop function runs over and over again forever
void loop() 


{



            EEPROM.get(2,val);
  
              if (touchRead(T4)<=25)
              {
                stepCount=0;
                  digitalWrite(2, HIGH);   // turn the LED on (HIGH is the voltage level)
                        while (touchRead(T4)<=25)
                        
                          {
                              // step one step:
                              //digitalWrite(En,HIGH);
                              myStepper.step(200);
                              myStepper.setSpeed(1000);
                              Serial.print("steps:");
                              Serial.println(stepCount);
                              stepCount=stepCount+1;
                               
                              
                          }   
                          
                            //digitalWrite(En,LOW);
                            digitalWrite(2,LOW);
                        }
                       EEPROM.write(2,stepCount);
                               EEPROM.commit();
           
        
              
            if(touchRead(T0)<=25 && location ==1)
               
              {
            
                  while(touchRead(T0)<=25)
                 {
                 
                  Serial.println(val);
                  //digitalWrite(En,HIGH);
                 myStepper.setSpeed(1000);
                  myStepper.step((180*val));
                  delay(1000);
                 location=2;
               //  digitalWrite(En,LOW);
                 }
              }
         else  if(touchRead(T0)<=25 && location ==2)
          {
            
            while(touchRead(T0)<=25)
           {
            Serial.println(val);
            //digitalWrite(En,HIGH);
           myStepper.setSpeed(1000);
            myStepper.step(-(180*val));
            delay(1000);
           location=1;
          // digitalWrite(En,LOW);
           }
          }
}

EEPROM.write() stores a single byte... did you mean EEPROM.put() ?

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