EEPROM Schrijven / Lezen

Hallo allemaal!

Stap je voor Stapje kom ik vooruit en mij Code wordt langer en langer... :smiley:
Ik ben nu aangekomen bij mijn idee om een "vaste value" in het EEPROM geheugen te schrijven, zodat dit vast gegeven als uitgangspunt genomen kan worden.

Het probleem alles "werkt" de stepcount wordt weggeschreven naar de eeprom, vervolgens (laatste gedeelt van de code) als ik op Up druk laat de Serial monitor continu 252.00 zien (loop) reset ik de Arduino en druk ik op Down krijg ik herhaaldelijk ovf te zien :o

iemand een idee?

// ************* Include Library's *************
#include <EEPROM.h>





// ************* Define Pins on Arduino / ESP8266 *************
const int stepPin = 3;        // Arduino Pin 3 / ESP8266 GPIO 14
const int dirPin = 4;         // Arduino Pin 4 / ESP8266 GPIO 16
const int EnablePin = 8;      // Arduino Pin 8 / ESP8266 GPIO 13
const int SleepPin = 9;       // Arduino Pin 9 / ESP8266 GPIO 12
//const int Motor0Pin = ;     // Arduino Not Connected / ESP8266 Not Connected
//const int Motor1Pin = 10;   // Arduino Pin 3 / ESP8266 GPIO 10
//const int Motor2Pin = 11;   // Arduino Pin 3 / ESP8266 GPIO 9

const int UpPin = 5;          // Arduino Pin 5 / ESP8266 GPIO 5
const int StopPin = 6;        // Arduino Pin 6 / ESP8266 GPIO 4
const int DownPin = 7;        // Arduino Pin 7 / ESP8266 GPIO 2

// ************* Define variables *************
int UpState = 0;              // variable for reading the pushbutton status
int StopState = 0;            // variable for reading the pushbutton status
int DownState = 0;            // variable for reading the pushbutton status

int stepCount = 0;            // number of steps the motor has taken


// ************* Define variables for Programming *************
long buttonTimer = 0;
long longPressTime = 3000;

boolean ButtonPress = false;
boolean LongButtonPress = false;

int ProgramState = 0;         // The current State of the Program
int address = 0;              // The current address in the EEPROM
float f = 0.00f;              //Variable to store data read from EEPROM.




void setup() {
  // initialize the serial port:
  Serial.begin(9600);

  // Sets the two pins as Outputs
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);


  pinMode(UpPin, INPUT_PULLUP);
  pinMode(StopPin, INPUT_PULLUP);
  pinMode(DownPin, INPUT_PULLUP);

  pinMode(EnablePin, OUTPUT);
  pinMode(SleepPin, OUTPUT);


}


void loop() {
  // read the state of the pushbutton value:
  UpState = digitalRead(UpPin);
  StopState = digitalRead(StopPin);
  DownState = digitalRead(DownPin);

  // ************* START PROGRAMMING SECTION *************
  if (StopState == HIGH) {
    if (ButtonPress == false) {
      ButtonPress = true;
      buttonTimer = millis();
    }
    if ((millis() - buttonTimer > longPressTime) && (LongButtonPress == false)) {
      LongButtonPress = true;
      Serial.println ("Start programming");
      ProgramState = 1;
    }
  } else {
    if (ButtonPress == true) {
      if (LongButtonPress == true) {
        LongButtonPress = false;
      } else {



        
        EEPROM.update(address, (stepCount / 200 ));  //Write a byte to the EEPROM. EEPROM.write(address, value)
        address = address + 1;
        if (address == EEPROM.length()) {
          address = 0;
          Serial.println("Writing to EEPROM ...");
          delay(100);
        }



        
        Serial.println ("Done programming");
        ProgramState = 0;
      }
      ButtonPress = false;
    }
  }
  // ************* Start Step Counting *************
  if (UpState == HIGH && ProgramState == 1) {
    // turn Up:
    digitalWrite(SleepPin, HIGH); //Enable/disable low-power sleep mode, LOW – Sleep, HIGH – Active
    digitalWrite(EnablePin, LOW); //Enable/disable the stepper driver, HIGH – Disable, LOW – Enable
    digitalWrite(dirPin, HIGH); // Enables the motor to move in a particular direction

    // Makes 200 pulses for making two full cycle rotation
    for (int x = 0; x < 50; x++) {
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(500);
      digitalWrite(stepPin, LOW);
      delayMicroseconds(500);
    }
    Serial.print("steps:");
    Serial.println(stepCount);
    stepCount++;
  }
  if (DownState == HIGH  && ProgramState == 1) {
    // turn Down:
    digitalWrite(SleepPin, HIGH); //Enable/disable low-power sleep mode, LOW – Sleep, HIGH – Active
    digitalWrite(EnablePin, LOW); //Enable/disable the stepper driver, HIGH – Disable, LOW – Enable
    digitalWrite(dirPin, LOW); //Changes the rotations direction

    // Makes 200 pulses for making two full cycle rotation
    for (int x = 0; x < 50; x++) {
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(500);
      digitalWrite(stepPin, LOW);
      delayMicroseconds(500);
    }
    Serial.print("steps:");
    Serial.println(stepCount);
    stepCount--;
  }
  // ************* End Step Counting *************


  // ************* END PROGRAMMING SECTION *************






 // ************* START BASIC SECTION *************

 
  // check if the upbutton is pressed. if it is, the upState is HIGH:
  if (UpState == HIGH && ProgramState == 0) {
    // turn Up:
    digitalWrite(SleepPin, HIGH); //Enable/disable low-power sleep mode, LOW – Sleep, HIGH – Active
    digitalWrite(EnablePin, LOW); //Enable/disable the stepper driver, HIGH – Disable, LOW – Enable
    digitalWrite(dirPin, HIGH); // Enables the motor to move in a particular direction

    // get Steps from EEPROM And Trun
    for (int x = 0; x < EEPROM.get(address, f); x++) 
     Serial.println(f, 3);    //This may print 'ovf, nan' if the data inside the EEPROM is not a valid float.
    {
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(500);
      digitalWrite(stepPin, LOW);
      delayMicroseconds(500);
    }
    Serial.println("Blinds UP");
  }

  // check if the downbutton is pressed. if it is, the downState is HIGH:
  if (StopState == HIGH  && ProgramState == 0) {
    // turn Down:
    digitalWrite(SleepPin, LOW); //Enable/disable the stepper driver, HIGH – Disable, LOW – Enable
    digitalWrite(EnablePin, LOW); //Enable/disable low-power sleep mode, LOW – Sleep, HIGH – Active
    delay(1000); // One second delay
  }

  // get Steps from EEPROM And Trun
  if (DownState == HIGH  && ProgramState == 0) {
    // turn Down:
    digitalWrite(SleepPin, HIGH); //Enable/disable low-power sleep mode, LOW – Sleep, HIGH – Active
    digitalWrite(EnablePin, LOW); //Enable/disable the stepper driver, HIGH – Disable, LOW – Enable
    digitalWrite(dirPin, LOW); //Changes the rotations direction

    // Makes 200 pulses for making two full cycle rotation
    for (int x = 0; x < EEPROM.get(address, f); x--) 
     Serial.println(f, 3);    //This may print 'ovf, nan' if the data inside the EEPROM is not a valid float.
    {
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(500);
      digitalWrite(stepPin, LOW);
      delayMicroseconds(500);
    }

    Serial.println("Blinds Down");

  }

  else {

  }

}

EEPROM.update schrijft een (1) enkele byte naar de EEPROM. Ik vermoed dat je EEPROM.get gebruikt om het terug te lezen maar omdat daar een float gebruikt worden er vier bytes terug gegeven.. Je hebt echter slechts 1 byte geschreven.

Ik heb geen idee wat je code precies is verondersteld te doen maar je kunt proberen the EEPROM.update te vervangen door EEPROM.put

EEPROM.put(address, (float)(stepCount / 200 ));

Niet getest; de cast naar een float forceert dat stepCound/200 als een float wordt weggeschreven.

Dan heb je iets verkeerd gedaan :wink:

#include <EEPROM.h>

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

  float pi = 3.14;
  float e = 2.71;
  float data;

  EEPROM.put(0, pi);
  EEPROM.get(0, data);
  Serial.println(data);

  EEPROM.put(0, e);
  EEPROM.get(0, data);
  Serial.println(data);


}

void loop()
{
}

Dit print 3.14 en 2.71.

Gewoon simpel beginnen :smiley:

Hoi achimpieters.

Wij zijn allemaal wel geïntrigeerd door jouw code hoor, daarom reageren we er ook op ;).

Om een float te maken, moet je 'm geen int noemen, maar float.

Dus zo:

float stepCount = 0;            // number of steps the motor has taken

Soms zijn dingen zo simpel op te lossen.

De variabele STEPS die in je 2e code blokje staat, word in je 3e code blok niet genoemd.
Dus dat voorbeeld wat je schijnbaar ergens gezien hebt, moet je dan wel aanpassen zodat de variabele past bij eentje die je zelf al in je bestaande code hebt.
EN anders moet je 'm aanmaken.

Je laatste code bevat geen lezen of schrijven van de eeprom; dat zal ons niet helpen om jou te helpen. Je moet een versie geven die niet werkt en aangeven wat er niet werkt.

Hieronder een stukje code om je steps weg te schrijven; ik heb eigenlijk geen idee waarom je een float gebruikt.

  int stepsAsInt = 100 / 9;
  int dataAsInt;

  EEPROM.put(0, stepsAsInt);
  EEPROM.get(0, dataAsInt);
  Serial.println(dataAsInt);


  float stepsAsFloat = 100/9.0;  // <---- 
  float dataAsFloat;

  EEPROM.put(0, stepsAsFloat);
  EEPROM.get(0, dataAsFloat);
  Serial.println(dataAsFloat);

Geef even speciale aandacht hoe stepsAsFloat is ingevuld. Als je daar 9 inplaats van 9.0 gebruikt wordt er een integer deling gedaan en is het resultaat 11 (en kun je net zo goed stepsAsInt gebruiken); met 9.0 wordt er een floating point deling gedaan en is het resultaat 11.11....

Verder hoog je in je originele post het eeprom adres op met 1; ten eerste heb ik geen idee waarom en ten tweede zal dat alleen werken voor 8-bit variabelen (byte, char) maar niet voor floats, integers en andere types. Voor die laatsten moet je het adres ophogen met de sizeof() van de variable of van het type.

// increment address with size of a float
address+=sizeof(float);
// increment address with size of integer variable
address+=sizeof(stepsAsInt);

Volgens mij klopt je redenering over de knoppen niet:
a) Je gebruikt INPUT_PULLUP dus de ingangen zijn bij niet ingedrukt standaard dus HIGH.
b) Jij test of ze hoog zijn dus dat zijn ze altijd tenzij ze ingedrukt worden. En dan worden ze LOW.