Hello together,
I am a beginner in C++ and Arduino and need some help to put and get a Long number into the EEPROM. I have read and tried already the examples who comes in the Arduino IDE and also maked some Google search. But until now I can´t get it work.
I have a Code to contoll a conveyor with a Arduino Nano who is working how i want it. The conveyor turns clock and anticlock wise. On each side of the conveyor are sensors who controll the position. When one of the sides is reached the motor stopps and waits a certain time until it begun the reverse turning. For each cycle there is a counter inside the code to control and stop the conveyor. When this reached a predefined maxcycles the equipment stops completely.
The actual value and target value of the counter i defined as "unsigned long" variable.
Like i told the code is working how it´s right now. Only i don´t want to loose the expired cycle numbers when i turn off the Arduino from the power supply. For that i would need to stored it on the EEPROM.
I found a example how to write and read a Long into the EEPROM but i can´t put it work with my existing Code.
Follows the Example Code that i found:
#include <EEPROM.h>
//------------------------------------------------
void writeLongIntoEEPROM(int address, long number)
{
EEPROM.write(address, (number >> 24) & 0xFF);
EEPROM.write(address + 1, (number >> 16) & 0xFF);
EEPROM.write(address + 2, (number >> 8) & 0xFF);
EEPROM.write(address + 3, number & 0xFF);
}
long readLongFromEEPROM(int address)
{
return ((long)EEPROM.read(address) << 24) +
((long)EEPROM.read(address + 1) << 16) +
((long)EEPROM.read(address + 2) << 8) +
(long)EEPROM.read(address + 3);
}
//------------------------------------------------
void setup() {
Serial.begin(9600);
writeLongIntoEEPROM(100, 123456);
long number = readLongFromEEPROM(100);
Serial.print("Number: ");
Serial.println(number);
}
//-------------------------------------------------
void loop() {}
Follows my Conveyor Code:
#include <EEPROM.h>
#define sensorleft 2 // Pin Sensor Left
#define sensorright 3 // Pin Sensor Right
#define startbutton 4 // Pin Start Button
#define stopbutton 5 // Pin Stop Button
#define relayLeft 11 // Pin Relay Left Turning
#define relayRight 12 // Pin Relay Right Turning
#define relayOn 0
#define relayOff 1
enum {waiting, on, right, stopright, left, stoplinks, off};
byte step = waiting;
unsigned long cycle = 0;
unsigned long maxcycles = 4; //Now it´s 4 to test. It will be 80000 Cycles
//----------------------------------------------------
void setup() {
Serial.begin(9600);
Serial.println(F("Start...."));
pinMode(sensorleft, INPUT_PULLUP);
pinMode(sensorright, INPUT_PULLUP);
pinMode(startbutton, INPUT_PULLUP);
pinMode(stopbutton, INPUT_PULLUP);
pinMode(relayLeft, OUTPUT);
digitalWrite(relayLeft, relayOff);
pinMode(relayRight, OUTPUT);
digitalWrite(relayRight, relayOff);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
}
//----------------------------------------------
void loop() {
Serial.print(F("Cyclen:"));
Serial.println(cycle);
Serial.println(maxcycles);
conveyor();
}
//--------------------------------------------------
void conveyor(){
int startPin = digitalRead(startbutton);
int stopPin = digitalRead(stopbutton);
int sensorLinksEnd = digitalRead(sensorleft);
int sensorrightEnd = digitalRead(sensorright);
static unsigned long lastmillis;
const unsigned long pauseTime = 5000; // in ms
if (stopPin == LOW )
{
step = off;
}
switch (step)
{
case waiting:
Serial.println(F("Equipment Waiting for Start()"));
if (startPin == LOW )
{
cycle = 0;
step = on;
}
digitalWrite(relayRight, relayOff);
digitalWrite(relayLeft, relayOff);
break;
case on:
Serial.println(F("Equipment On()"));
digitalWrite(LED_BUILTIN, HIGH);
step = right;
break;
case right:
Serial.println(F("Right Turning ()"));
digitalWrite(relayRight, relayOn);
if (sensorrightEnd == LOW )
{
digitalWrite(relayRight, relayOff);
step = stopright;
lastmillis = millis();
}
break;
case stopright:
Serial.println(F("Stop Right()"));
digitalWrite(relayRight, relayOff);
digitalWrite(relayLeft, relayOff);
if (millis() - lastmillis > pauseTime)
{
step = left;
}
break;
case left:
Serial.println(F("Left Turning()"));
digitalWrite(relayRight, relayOff);
digitalWrite(relayLeft, relayOn);
if (sensorLinksEnd == LOW )
{
digitalWrite(relayLeft, relayOff);
step = stoplinks;
lastmillis = millis();
}
break;
case stoplinks:
Serial.println(F("Stop Left()"));
digitalWrite(relayRight, relayOff);
digitalWrite(relayLeft, relayOff);
if (millis() - lastmillis > pauseTime)
{
if (cycle == maxcycles)
{
step = off;
}
else
{
cycle++;
step = right;
}
}
break;
case off:
Serial.println(F("Equipment Off()"));
digitalWrite(LED_BUILTIN, LOW);
step = waiting;
break;
}
}
I would be gratefull for some help and tips how to make this work.