Getting code to get EEPROM number

Hello!

Im saving a number to EEPROM, I can confirm that its saved vid EEPROM.get, shows on LCD.
Code down below is "permanent", so when the motor passes by between 280 and 320 pulses, it stops for 1 sec.
What I would like to have is that this piece of code is "dynamic", so it knows what the number in the EEPROM is right now. And adjust after that.
Any suggestions?

// -- This code stops the motor for 1 sec when it passes by 280-320 pulses.
{
  if(PulseCount < 320 && PulseCount > 280){
  digitalWrite(controlPin1, LOW);        
  digitalWrite(controlPin2, LOW);
    lcd.setCursor(0, 1);
    lcd.print("Program 2");
    Serial.println("Programmed stop 2 activated ⛔");
      delay(1000);
  }

This is the code snippet that saves the pulse number from the motor to EEPROM...

 {
  val = digitalRead(intest3Pin);        
  if (val == HIGH) {                     

EEPROM.put(0,PulseCount);                // PulseCount is a number that goes to the EEPROM
    lcd.setCursor(0, 1);             
    lcd.print(PulseCount);
        delay(750);
            lcd.setCursor(0, 1);           
    lcd.print("Pos Saved!");
            Serial.println("Saving liftposition to memory");
        delay(750);
  }     
}

I don't see any reference to EEPROM at all...

Please post all your code. If it's to long, try to make a small program which demonstrates the same problem.

Snippets R Us

And you do know the life of EEPROM is limited? You can only write is 100.000 (100k) times. If you write every second you can write for a little over 1 day and then your done.

septillion:
Snippets R Us

And you do know the life of EEPROM is limited? You can only write is 100.000 (100k) times. If you write every second you can write for a little over 1 day and then your done.

OP would have to lean on that switch all day to wear out the EEPROM in one day.

OP: You can READ from EEPROM as many times as you like without concern of wearing it out. So, ANYWHERE you want to know the value in EEPROM, read it.

Forgot to mention this VERY important thing :o see that now.

The memory WILL NOT be written to alot, maybe once a month, or less.
So we are not talking constant saving here.
But it will be read a couple of times per day.

OP made me look like a fool by adding code and deleting posts... No clue there was a switch before.

septillion:
OP made me look like a fool by adding code and deleting posts... No clue there was a switch before.

I missed that OP edited the original post. My apologies for thinking that you missed the fact that the write happened only when the switch was pressed.

Sorry guys.
Just trying to keep it simple. :frowning:

But overall explained. Im building a machine that has a motor with a rotary encoder.
It moves forward or backward. Only moves when buttons are pressed.

The function I'm after is to be able to press a button, to save its position.
And then when the motor passes by that position, it will pause its journey for 1 second.

So, right now I'm able to save the position, and I have confirmed its saved. So that works.
Now the goal is for this code to read this position from the memory.
And I dont know whats the best way to do it.

I GUESS, that I could have a code in my main code that simple updates itself with the new position.

Any suggestions? :o

If you can write with EEPROM.write() or EEPROM.put() you can now just read it with EEPROM.read() or EEPROM.get(). You can read it every time you want to check if that position was passed. No harm in reading EEPROM often but it is a bit slower. So I would read the EEPROM into a variable in setup() and use that. Don't forget to update that variable as well when you save a new value to EEPROM.

Ok, thanks, I'll try making something out of this now. :slightly_smiling_face:
I'll report back. :drooling_face:

Stuck again...
I've tried this code, and variations. Mostly get the message
"'f' was not declared in this scope"

  {
    EEPROM.get(0, f); 
  if(0){
  digitalWrite(controlPin1, LOW);        
  digitalWrite(controlPin2, LOW);  
    lcd.setCursor(0, 1);
    lcd.print("Prg.STOP");
    Serial.println("Programmed Stop Activated ⛔");
      delay(1000);
  }
}

I also have this in Void Setup

float f = 0.00f;   //Variable to store data read from EEPROM.
  int eeAddress = 0; //EEPROM address to start reading from

  
  Serial.begin(9600);    // set up the LCD's number of columns and rows:


    EEPROM.get(eeAddress, f);
  Serial.println(f, 3);    //This may print 'ovf, nan' if the data inside the EEPROM is not a valid float.

Feels like im missing something small and "easy"....

Feels like im missing something small and "easy"....

Yes, indeed.

Simple things like the concept of "POST ALL OF YOUR CODE".

Things like variable scope.

Declaring a variable in setup() and expecting to be able to use it in loop() is NOT realistic.

@OP, I agree with Paul and I told you twice before:

septillion:
Please post all your code. If it's to long, try to make a small program which demonstrates the same problem.

Last bit isn't only great for us, but it's even better for you to get a feeling for how something should work at the basics. >50% of the time you're able to spot the error yourself if you do :wink:

Okey guys, here it is, but its alot of code :open_mouth:
And notes in Swedish.
The ERROR code is marked, almost lowest position in the code.

This machine/code can take the motor backward or forward, by pressing two buttons.
It can save a position to memory to be used as a "programmed motor stop" when passing by that position of the motor (motor with sensors, wich gives me pulses).
What it cant to, is to use this saved position in the code.
I want it to update the new position/number, everytime i press one button.
:o

CODE PART 1

//  ---- MOTOR CONST INTARNA  - DESSA GÅR TILL H-BRYGGAN ----
const int controlPin1 = 7;
const int controlPin2 = 6;
const int enablePin = 9;


#include <Wire.h>                   // Tillåter I2C kommunikation - Pins A4 (SDA), A5 (SCL)
#include <LiquidCrystal_I2C.h>      // Denna aktiverar LCD
#include <EEPROM.h>                 // Denna aktiverar EEPROM minnet


// DESSA TILLHÖR GRAFIK O PULSRÄKNINGEN
char lineText[]="  Hoy! Denna text syns ingenstans.  ";
int sensor01;
int sensor02;
int stepOld;
int step;
long PulseCount;


// ---- MOTOR TEST INT ---------------------------------------------------------------------------------
int intest1Pin = 4;        // KNAPP 1  -  Kabel GRÅ    - Går åt plus hållet    - UPP
int intest2Pin = 5;        // KNAPP 2  -  Kabel GRÖN   - Går åt minus hållet   - NER
int intest3Pin = 10;       // KNAPP 3  -  Kabel SVART  - SPARA POSITION        - Minnet SPARA
int intest4Pin = 11;       // KNAPP 4  -  Kabel BLÅ    - VISA SPARAD POSITION  - Minnet LÄS
int stopsensor1Pin = 12;   // SENSOR 1  -  Kabel GRÖN  - Enskild sensor - STOPPAR NER
int stopsensor2Pin = 8;    // SENSOR 2  -  Kabel GRÅ   - Enskild sensor - STOPPAR UPP
int stopsensor3Pin = 15;   // SENSOR 3  -  Kabel ROSA  - Enskild sensor - TOTALSTOPP
int val = 0;
int tonePin = 13;          // --- BEEPER ---





// DESSA TILLHÖR GRAFIK O PULSRÄKNINGEN - I2C
// initialize the library with the numbers of the interface pins
// 0x27 adressen hittades via I2C scanner på den med svart tejp - De andra hade 0x3F
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); 

// Aktivera denna rad, och av-avaktivera den ovanför, om någon av de andra I2C displayerna ska användas
// LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); 







// DESSA TILLHÖR GRAFIK O PULSRÄKNINGEN
#define SENSOR_A 3
#define SENSOR_B 2

void checkState()
{
  sensor01 = digitalRead(SENSOR_A);
  sensor02 = digitalRead(SENSOR_B);

  if(sensor01 == 1 && sensor02 == 1){
    step = 0;
    if(stepOld == 1){
      PulseCount--;
    }
    if(stepOld == 3){
      PulseCount++;
    }
    stepOld = 0;
  }

  if(sensor01 == 0 && sensor02 == 1){
    step = 1;
    if(stepOld == 2){
      PulseCount--;
    }
    if(stepOld == 0){
      PulseCount++;
    }
    stepOld = 1;
  }

  if(sensor01 == 0 && sensor02 == 0){
    step = 2;
    if(stepOld == 3){
      PulseCount--;
    }
    if(stepOld == 1){
      PulseCount++;
    }
    stepOld = 2;
  }

  if(sensor01 == 1 && sensor02 == 0){
    step = 3;
    if(stepOld == 0){
      PulseCount--;
    }
    if(stepOld == 2){
      PulseCount++;
    }
    stepOld = 3;
  }
}


// VOID SETUP - KÖRS BARA EN GÅNG I BÖRJAN, EN FÖRBEREDELSE, LOOP ÄR EXEKVERING OCH GÖR DET MESTA JOBBET
// DESSA TILLHÖR GRAFIK O PULSRÄKNINGEN

void setup() 
{

// TA BORT DESSA RADER OM DE EJ HJÄLPER - TILLHÖR EEPROM läsningen
float f = 0.00f;   //Variable to store data read from EEPROM.
  int eeAddress = 0; //EEPROM address to start reading from

  
  Serial.begin(9600);
  // set up the LCD's number of columns and rows:



  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.setCursor(0, 0);
  lcd.print("                ");
  lcd.setCursor(0, 1);
  lcd.print("                ");

  attachInterrupt(0, checkState, CHANGE);
  attachInterrupt(1, checkState, CHANGE);

  PulseCount = 0;





  // TEST MOTOR  - VOID SETUP ---------------------------------------------------------------------------------
  pinMode(intest1Pin, INPUT);         //Declarar denna som input  UNO input är 4    Knapp 1   Grå     UPP
  pinMode(intest2Pin, INPUT);         //Declarar denna som input  UNO input är 5    Knapp 2   Grön    NER
  pinMode(intest3Pin, INPUT);         //Declarar denna som input  UNO input är 10   Knapp 3   Svart   SPARA POSITION
  pinMode(intest4Pin, INPUT);         //Declarar denna som input  UNO input är 11   Knapp 4   Blå     VISA SPARAD POSITION
  
  pinMode(stopsensor1Pin, INPUT);     //Declarar denna som input  UNO input är 12         grön
  pinMode(stopsensor2Pin, INPUT);     //Declarar denna som input  UNO input är 8          grå
  pinMode(stopsensor3Pin, INPUT);     //Declarar denna som input  UNO input är 15 (A1)    rosa

  // Högtalare
  pinMode(tonePin, OUTPUT);
  

//  ---- LCD start presentationen
{

lcd.setCursor(2, 0);
lcd.print("PROTOTYPE"); 
    Serial.println(" Boot initialized ");
delay(500);
// lcd.clear();

lcd.setCursor(11, 1);
lcd.print("9");
delay(100);

lcd.setCursor(11, 1);
lcd.print("90");
delay(100);

lcd.setCursor(11, 1);
lcd.print("90E");
delay(900);
lcd.clear();

lcd.setCursor(2, 0);
lcd.print("Prototype 1.0");
delay(200);

lcd.setCursor(0, 1);
lcd.print("www.");
delay(100);

lcd.setCursor(12, 1);
lcd.print(".se");
delay(100);

lcd.setCursor(0, 1);
lcd.print("www.PROTOTYPE.se");
delay(250);

lcd.setCursor(0, 1);
lcd.print("www.PROTOTYPE.se");
    Serial.println("unit is active ");
    
delay(800);
lcd.clear();

  }
}





void loop()


{

  // DESSA TILLHÖR GRAFIK O PULSRÄKNINGEN
  // LCD set the cursor to column 0, line 1 (note: line 1 is the second row, since counting begins with 0):
  
  lcd.setCursor(0, 0);

  lcd.print("Pulses:");
  if(PulseCount > -1){
    lcd.print(" ");
  }

  if(PulseCount < 10 && PulseCount > -10){
    lcd.print(" ");
  }

  if(PulseCount < 100 && PulseCount > -100){
    lcd.print(" ");
  }

  lcd.print(PulseCount);
  if(sensor01 == 1){
    lcd.print(" HIGH");
  }
  else{
    lcd.print(" LOW ");
  }

  lcd.setCursor(0, 1);

  if(sensor02 == 1){
    lcd.print("            HIGH");
  }
  else{
    lcd.print("            LOW ");
  }
// SAVING POSITION TO EEPROM -=-=-=-=-=-=-=-=-=-=-=-=-

 {
  val = digitalRead(intest3Pin);         // Knapp 3 - Denna ska spara position i minnet
  if (val == HIGH) {                     // Kollar om knappen är tryckt

EEPROM.put(0,PulseCount);                // Skriver PulseCount numret till minnet, adressen är 0
    lcd.setCursor(0, 1);                 // Dessa två rader skriver till LCD, i detta fall är 0,1 rad 2
    lcd.print(PulseCount);
        delay(750);
            lcd.setCursor(0, 1);           
    lcd.print("Pos Saved!");
            Serial.println("Saving liftposition to memory ");
        delay(750);
  }     
}





// READING FROM EEPROM -=-=-=-=-=-=-=-=-=-=-=-=-

 {
  val = digitalRead(intest4Pin);         // Knapp 4 - Ska läsa upp från minnet
  if (val == HIGH) {                     // Kollar om knappen är tryckt

EEPROM.get(0,PulseCount);                // Läser av numret som är sparat i minnet
    lcd.setCursor(0, 1);                 // Dessa två rader skriver till LCD, i detta fall är 0,1 rad 2
    lcd.print("Reading MEM");               
            Serial.println("Reading liftposition from memory ");         
        delay(1000);
  }     
}






//  if(PulseCount < 320 && PulseCount > 280){






//  ---- MOTORNS VOID LOOP  - KNAPPSTYRNINGEN TILL MOTORERNA ----

// intest 1 ÅKER UPP
// intest 2 ÅKER NER
// stopsensor1Pin heter stoppsensorn som hindrar UPP
// stopsensor2Pin heter stoppsensorn som hindrar NED
// stopsensor3Pin heter stoppsensorn som ska hindra UPP eller NED

{

// ---- ÅKER UPP ----
  val = digitalRead(intest1Pin);         // UPP - Läser input värde
  if (val == HIGH)                       // Checkar om knappen trycks
  {                     
  digitalWrite(controlPin1, HIGH);       
  digitalWrite(controlPin2, LOW);




// -- DESSA TRE TILLHÖR SENSORN SOM SKA STOPPA BÅDE UPP OCH NER
  {
  val = digitalRead(stopsensor3Pin);         
  if (val == HIGH) {                     
  digitalWrite(controlPin1, LOW);

// -- Dessa 3 rader visar USER STOP när sensorn stoppar nedgång - delay ställer flimret men slöar också ner visningen av pulsräkningen ------
    lcd.setCursor(0, 1);
    lcd.print("User Stop");
    Serial.println("User removeable module defined stop 1 ");
    delay(50);

// -- Dessa tre rader nedanför gör pipljudet när UPP & NER stopsensorn (ovan) täcks
  tone(tonePin, 600);
  delay(50);
  tone(tonePin, 950);
  delay(70);
  tone(tonePin, 800, 125);
  delay(800);
  noTone(tonePin);
  }
  }

// -- Detta är sensorn som, om täcks, hindrar motorn från att åka upp
  {
  val = digitalRead(stopsensor2Pin);         // Sensor 2 som stoppar motorn, likaså sensor 3 vars uppgift är att stoppa både upp och ner
  if (val == HIGH) {                     
  digitalWrite(controlPin1, LOW);

// -- Dessa 3 rader visar UPSTOP när sensorn stoppar nedgång - delay ställer flimret men slöar också ner visningen av pulsräkningen ------
    lcd.setCursor(0, 1);
    lcd.print("Up Stop");
    Serial.println("Upper security sensor reached  Can only go down ");
    delay(50);

// -- Dessa tre rader nedanför gör pipljudet när stopsensorn (ovan) täcks påvägen UPP
  tone(tonePin, 880);
  delay(125);
  tone(tonePin, 400, 125);
  delay(650);
  noTone(tonePin);
  }


// -- Dessa 3 rader visar UP när nedåtknapp trycks NED - delay ställer flimret men slöar också ner visningen av pulsräkningen ------
  lcd.setCursor(0, 1);
  lcd.print("Up");
      Serial.println("Lift going up at full speed ");
  delay(50);





  
  }
  }






// ---- ÅKER NER ----
  else {
  val = digitalRead(intest2Pin);         // NER - Läser input värde
  if (val == HIGH) {                     // Checkar om knappen trycks
  digitalWrite(controlPin1, LOW);       
  digitalWrite(controlPin2, HIGH);


// -- DESSA TRE TILLHÖR SENSORN SOM SKA STOPPA BÅDE UPP OCH NER
  {
  val = digitalRead(stopsensor3Pin);         // TEST
  if (val == HIGH) {                     
  digitalWrite(controlPin2, LOW);

// -- Dessa 3 rader visar STOP när sensorn stoppar nedgång - delay ställer flimret men slöar också ner visningen av pulsräkningen ------
    lcd.setCursor(0, 1);
    lcd.print("User Stop");
    Serial.println("User removeable module defined stop 2 ");
    delay(50);

// -- Dessa tre rader nedanför gör pipljudet när UPP & NER stopsensorn (ovan) täcks
  tone(tonePin, 600);
  delay(50);
  tone(tonePin, 950);
  delay(70);
  tone(tonePin, 800, 125);
  delay(800);
  noTone(tonePin);
  }
  }

  
// -- Detta är sensorn som, om täcks, hindrar motorn från att åka ner
  {
  val = digitalRead(stopsensor1Pin);         // Sensorn 1 som stoppar motorn, likaså sensor 3 vars uppgift är att stoppa både upp och ner
  if (val == HIGH) {                     
  digitalWrite(controlPin2, LOW);
  

// -- Dessa 3 rader visar DOWN STOP när sensorn stoppar nedgång - delay ställer flimret men slöar också ner visningen av pulsräkningen ------
    lcd.setCursor(0, 1);
    lcd.print("Down Stop");
    Serial.println("Lower security sensor reached  Can only go up ");
    delay(50);

// -- Dessa tre rader nedanför gör pipljudet när stopsensorn (ovan) täcks påvägen ner
  tone(tonePin, 880);
  delay(125);
  tone(tonePin, 400, 125);
  delay(650);
  noTone(tonePin);
  }

// -- Dessa 3 rader visar DOWN när nedåtknapp trycks NED - delay ställer flimret men slöar också ner visningen av pulsräkningen ------
    lcd.setCursor(0, 1);
    lcd.print("Down");
    Serial.println("Lift going down at full speed ");
    delay(50);
  }  

// -- Dessa två rader stoppar motorn när man släpper knappen
  }  else 
  {
  digitalWrite(controlPin1, LOW);        
  digitalWrite(controlPin2, LOW);       
  }




// -- Dessa rader stoppar motorn 1 sek när den är inom värdet, men bara när man kör UPP
// -- Det är dessa rader jag vill kunna programmera om med EEPROM
  {
    EEPROM.get( eeAddress, f );         // GET ERROR HERE - eeADRESS WAS NOT DECLARED IN THIS SCOPE
  if(eeAddress){
  digitalWrite(controlPin1, LOW);        
  digitalWrite(controlPin2, LOW);  
    lcd.setCursor(0, 1);
    lcd.print("Prg.STOP");
    Serial.println("Programmed Stop Activated ");
      delay(1000);
  }
}








} // Går till Åker ner
} // Går till Åker upp

} // Går till puls o grafikräkning - Högst upp

Please review the code you posted.

Part 1 ends with

lcd.setCursor(2, 0);
lcd.print("PROTOTYPE"); 
    Serial.println("

and the second code segment does not follow on from that

Are you still declaring f in setup() and trying to use it in another function ?

So, now the code is correct.
It was hard uploading it, the webpage complained over the size. So i had to split it in two.
And could not upload it either because of some icons used in code was not allowed, so had to remove all of them.

Because im new to the forum I'm not allowed to do changes and new posts within
30 secs and 5 mins.... A bit frustrating when noticing what went wrong with uploading the code here!

The code should be OK now. But in two parts.

//  ---- LCD start presentationen
{

You forgot to comment the reason for the useless curly brace.

 {
  val = digitalRead(intest3Pin);         // Knapp 3 - Denna ska spara position i minnet
  if (val == HIGH) {                     // Kollar om knappen är tryckt

EEPROM.put(0,PulseCount);                // Skriver PulseCount numret till minnet, adressen är 0
    lcd.setCursor(0, 1);                 // Dessa två rader skriver till LCD, i detta fall är 0,1 rad 2
    lcd.print(PulseCount);
        delay(750);
            lcd.setCursor(0, 1);           
    lcd.print("Pos Saved!");
            Serial.println("Saving liftposition to memory ");
        delay(750);
  }     
}

The monkey that typed that code really needs to go into treatment. All that drinking is not good.

Use Tools + Auto Format before you embarrass yourself posting any more crappy looking code.

Because im new to the forum I'm not allowed to do changes and new posts within
30 secs and 5 mins....

But you could have attached the code to your post as a single file....

PaulS:
Ouch! What a burst :cry:
Keep in mind im totally new on this! I have No Clue on how this should look like.
Alot of trial and error. But the code actually works with the motor and all!
And it works good, and as intended.
Wich is quite a miracle in itself! Now there is only the last function to be added, wich is for the code to see whats in the memory and save that position/number for when the motor is passing it.
Then ill do correction and try to make the code look nicer.
But OK I'll do auto-format, and see what happens :slight_smile:

UKHeliBob:
Aha! Ok, here now its attached.

LCD_I2C_test_42_-_Attach.ino (11.8 KB)