How do i interface Eemprom with Linear Acuator

I just want to save the positions so when it is powered off and then on it will remember where it is . could u help me out . I don't understand coding . please help if u can . thanks

#include <JC_Button.h>
#include <Encoder.h>
#include <EEPROM.h>
String readString;

const int RELAY = {30,31,32,33,34,35,36,37}; //RELAY[0] and RELAY[1] to access the pins 29 &27 is forward , 30 ,32 is backward ,
const int BTN_EXTEND = 4;
const int BTN_RETRACT = 5;
const uint8_t MANUAL = 1; //a constant to indicate manual mode
const uint8_t AUTOMATIC = 2; //a constant to indicate automatic mode
const int BTN_MEM_PIN = {8,9,10};
const int BTN_SET_MEM = 11;
const int LED = {39,40,41};

//Set up the linear actuator encoder
//On many of the Arduino boards pins 2 and 3 are interrupt pins
// which provide the best performance of the encoder data.
Encoder myEnc(2, 3);
long oldPosition = -999;
long targetPosition = 0;
#define ACCURACY 100 //How close to your target position is close enough. Higher accuracy may result in
// a bit of jitter as the actuator nears the position
#define DEBOUNCE_MS 10 //A debounce time of 20 milliseconds usually works well for tactile button switches.
#define PULLUP true //To keep things simple, we use the Arduino's internal pullup resistor.
#define INVERT true //Since the pullup resistor will keep the pin high unless the
//switch is closed, this is negative logic, i.e. a high state
//means the button is NOT pressed. (Assuming a normally open switch.)
uint8_t MODE = MANUAL;

Button btnExtend(BTN_EXTEND, PULLUP, INVERT, DEBOUNCE_MS);
Button btnRetract(BTN_RETRACT, PULLUP, INVERT, DEBOUNCE_MS);
Button btnSetPos(BTN_SET_MEM, PULLUP, INVERT, DEBOUNCE_MS);
Button btnPos1(BTN_MEM_PIN[0], PULLUP, INVERT, DEBOUNCE_MS);
Button btnPos2(BTN_MEM_PIN[1], PULLUP, INVERT, DEBOUNCE_MS);
Button btnPos3(BTN_MEM_PIN[2], PULLUP, INVERT, DEBOUNCE_MS);

long memPosition = {0,0,0};

void setup() {
Serial.begin(9600);

pinMode(RELAY[0], OUTPUT);
pinMode(RELAY[1], OUTPUT);
pinMode(RELAY[2], OUTPUT);
pinMode(RELAY[3], OUTPUT);
pinMode(RELAY[4], OUTPUT);
pinMode(RELAY[5], OUTPUT);
pinMode(RELAY[6], OUTPUT);
pinMode(RELAY[7], OUTPUT);
pinMode(LED[0], OUTPUT);

}

void loop() {

btnExtend.read();
btnRetract.read();
btnSetPos.read();
btnPos1.read();
btnPos2.read();
btnPos3.read();

if (btnExtend.isPressed()) {
extendActuator();
MODE = MANUAL;
}

if (btnRetract.isPressed()) {
retractActuator();
MODE = MANUAL;
}

if (!btnExtend.isPressed() && !btnRetract.isPressed() && MODE == MANUAL) {
stopActuator();
MODE = MANUAL;
}

if(btnPos1.wasReleased()) {
Serial.println("btnPos1");
MODE = AUTOMATIC;
targetPosition = memPosition[0];
}
if(btnPos2.wasReleased()) {
Serial.println("btnPos2");
MODE = AUTOMATIC;
targetPosition = memPosition[1];
}
if(btnPos3.wasReleased()) {
Serial.println("btnPos3");
MODE = AUTOMATIC;
targetPosition = memPosition[2];
}

//check the encoder to see if the position has changed
long newPosition = myEnc.read();
if (newPosition != oldPosition) {
oldPosition = newPosition;
Serial.println(newPosition);
}

if(MODE == AUTOMATIC && newPosition != targetPosition) {
Serial.print("Target/Actual:");Serial.print(targetPosition);Serial.print(" / ");Serial.print(newPosition);Serial.print(" [");Serial.print(abs(targetPosition - newPosition));Serial.println("]");
if(targetPosition < newPosition) {
Serial.println("AUTO RETRACT");
retractActuator();
MODE = AUTOMATIC;
}
if(targetPosition > newPosition) {
Serial.println("AUTO EXTEND");
extendActuator();
MODE = AUTOMATIC;
}
if( (targetPosition == newPosition) || abs(targetPosition - newPosition) <= ACCURACY) {
Serial.println("AUTO STOP");
stopActuator();
MODE = MANUAL;
}
}

if(btnSetPos.isPressed()) {
if(btnPos1.isPressed())
memPosition[0] = newPosition;
if(btnPos2.isPressed())
memPosition[1] = newPosition;
if(btnPos3.isPressed())
memPosition[2] = newPosition;

}
}

void extendActuator() {
//Serial.println("extendActuator");
digitalWrite(RELAY[0], HIGH);
digitalWrite(RELAY[1], LOW);
digitalWrite(RELAY[2], HIGH);
digitalWrite(RELAY[3], LOW);
}

void retractActuator() {
//Serial.println("retractActuator");
digitalWrite(RELAY[0], LOW);
digitalWrite(RELAY[1], HIGH);
digitalWrite(RELAY[2], LOW);
digitalWrite(RELAY[3], HIGH);
}

void stopActuator() {
//Serial.println("stopActuator");
digitalWrite(RELAY[0], HIGH);
digitalWrite(RELAY[1], HIGH);
digitalWrite(RELAY[2], HIGH);
digitalWrite(RELAY[3], HIGH);
digitalWrite(RELAY[4], HIGH);
digitalWrite(RELAY[5], HIGH);
digitalWrite(RELAY[6], HIGH);
digitalWrite(RELAY[7], HIGH);
digitalWrite(LED[0], LOW);

}

Read the forum guidelines.

Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

i hope its correct now

Not as I can se. Use Ctrl + T in IDE and use code tags, symbol number 6 from the left here, before depositing the code.

ok there we go . i new to this thing . thanks

Hey @Rexthebest
What are these values(positions ) (variables) you want to keep?

RV mineirin

hello .im i want to keep the last memory position . . Like the last saved one . Am the values will always be changing /position .

Do you want save this values?
memPosition[0]; and memPosition[1]; and memPosition[2]; ??

RV mineirin

yes .

@Rexthebest
try this way:
PS; The EEPROM.get() lines was wrong.
I already corrected

#include <JC_Button.h>
#include <Encoder.h>
#include <EEPROM.h>
String readString;

const int RELAY = {30, 31, 32, 33, 34, 35, 36, 37}; //RELAY[0] and RELAY[1] to access the pins 29 &27 is forward , 30 ,32 is backward ,
const int BTN_EXTEND = 4;
const int BTN_RETRACT = 5;
const uint8_t MANUAL = 1; //a constant to indicate manual mode
const uint8_t AUTOMATIC = 2; //a constant to indicate automatic mode
const int BTN_MEM_PIN = {8, 9, 10};
const int BTN_SET_MEM = 11;
const int LED = {39, 40, 41};

//Set up the linear actuator encoder
//On many of the Arduino boards pins 2 and 3 are interrupt pins
// which provide the best performance of the encoder data.
Encoder myEnc(2, 3);
long oldPosition = -999;
long targetPosition = 0;
#define ACCURACY 100 //How close to your target position is close enough. Higher accuracy may result in
// a bit of jitter as the actuator nears the position
#define DEBOUNCE_MS 10 //A debounce time of 20 milliseconds usually works well for tactile button switches.
#define PULLUP true //To keep things simple, we use the Arduino's internal pullup resistor.
#define INVERT true //Since the pullup resistor will keep the pin high unless the
//switch is closed, this is negative logic, i.e. a high state
//means the button is NOT pressed. (Assuming a normally open switch.)
uint8_t MODE = MANUAL;

Button btnExtend(BTN_EXTEND, PULLUP, INVERT, DEBOUNCE_MS);
Button btnRetract(BTN_RETRACT, PULLUP, INVERT, DEBOUNCE_MS);
Button btnSetPos(BTN_SET_MEM, PULLUP, INVERT, DEBOUNCE_MS);
Button btnPos1(BTN_MEM_PIN[0], PULLUP, INVERT, DEBOUNCE_MS);
Button btnPos2(BTN_MEM_PIN[1], PULLUP, INVERT, DEBOUNCE_MS);
Button btnPos3(BTN_MEM_PIN[2], PULLUP, INVERT, DEBOUNCE_MS);

long memPosition = {0, 0, 0};

void setup() {
  Serial.begin(9600);

  pinMode(RELAY[0], OUTPUT);
  pinMode(RELAY[1], OUTPUT);
  pinMode(RELAY[2], OUTPUT);
  pinMode(RELAY[3], OUTPUT);
  pinMode(RELAY[4], OUTPUT);
  pinMode(RELAY[5], OUTPUT);
  pinMode(RELAY[6], OUTPUT);
  pinMode(RELAY[7], OUTPUT);
  pinMode(LED[0], OUTPUT);

  EEPROM.get(0, memPosition[0]);
 EEPROM.get(4, memPosition[1]);
 EEPROM.get(8, memPosition[2]);

}

void loop() {

  btnExtend.read();
  btnRetract.read();
  btnSetPos.read();
  btnPos1.read();
  btnPos2.read();
  btnPos3.read();

  if (btnExtend.isPressed()) {
    extendActuator();
    MODE = MANUAL;
  }

  if (btnRetract.isPressed()) {
    retractActuator();
    MODE = MANUAL;
  }

  if (!btnExtend.isPressed() && !btnRetract.isPressed() && MODE == MANUAL) {
    stopActuator();
    MODE = MANUAL;
  }

  if (btnPos1.wasReleased()) {
    Serial.println("btnPos1");
    MODE = AUTOMATIC;
    targetPosition = memPosition[0];
    EEPROM.put(0, memPosition[0]);
  }
  if (btnPos2.wasReleased()) {
    Serial.println("btnPos2");
    MODE = AUTOMATIC;
    targetPosition = memPosition[1];
    EEPROM.put(4, memPosition[1]);
  }
  if (btnPos3.wasReleased()) {
    Serial.println("btnPos3");
    MODE = AUTOMATIC;
    targetPosition = memPosition[2];
    EEPROM.put(8, memPosition[2]);
  }

  //check the encoder to see if the position has changed
  long newPosition = myEnc.read();
  if (newPosition != oldPosition) {
    oldPosition = newPosition;
    Serial.println(newPosition);
  }

  if (MODE == AUTOMATIC && newPosition != targetPosition) {
    Serial.print("Target/Actual:"); Serial.print(targetPosition); Serial.print(" / "); Serial.print(newPosition); Serial.print(" ["); Serial.print(abs(targetPosition - newPosition)); Serial.println("]");
    if (targetPosition < newPosition) {
      Serial.println("AUTO RETRACT");
      retractActuator();
      MODE = AUTOMATIC;
    }
    if (targetPosition > newPosition) {
      Serial.println("AUTO EXTEND");
      extendActuator();
      MODE = AUTOMATIC;
    }
    if ( (targetPosition == newPosition) || abs(targetPosition - newPosition) <= ACCURACY) {
      Serial.println("AUTO STOP");
      stopActuator();
      MODE = MANUAL;
    }
  }

  if (btnSetPos.isPressed()) {
    if (btnPos1.isPressed())
      memPosition[0] = newPosition;
    if (btnPos2.isPressed())
      memPosition[1] = newPosition;
    if (btnPos3.isPressed())
      memPosition[2] = newPosition;

  }
}

void extendActuator() {
  //Serial.println("extendActuator");
  digitalWrite(RELAY[0], HIGH);
  digitalWrite(RELAY[1], LOW);
  digitalWrite(RELAY[2], HIGH);
  digitalWrite(RELAY[3], LOW);
}

void retractActuator() {
  //Serial.println("retractActuator");
  digitalWrite(RELAY[0], LOW);
  digitalWrite(RELAY[1], HIGH);
  digitalWrite(RELAY[2], LOW);
  digitalWrite(RELAY[3], HIGH);
}

void stopActuator() {
  //Serial.println("stopActuator");
  digitalWrite(RELAY[0], HIGH);
  digitalWrite(RELAY[1], HIGH);
  digitalWrite(RELAY[2], HIGH);
  digitalWrite(RELAY[3], HIGH);
  digitalWrite(RELAY[4], HIGH);
  digitalWrite(RELAY[5], HIGH);
  digitalWrite(RELAY[6], HIGH);
  digitalWrite(RELAY[7], HIGH);
  digitalWrite(LED[0], LOW);

} void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}
1 Like

Thank u soo much .. Respect . It works

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