Hallo allemaal!
Stap je voor Stapje kom ik vooruit en mij Code wordt langer en langer...
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 {
 }
}