Hi @Jan123456,
The problem of reading 255 happens because when you upload the code, into the flash memory used by the DueFlashStorage.h library
values remain 0xFFFFFFFFFFFFF......
So when reading, the value 0XFF is returned, and it can't enter any case, and as a consequence it can't save any value, always getting 0XFF (255).
But if you test the memory in setup() and it is 0xFF, you set this value to 0, from then on everything will work correctly.
I made some modifications to your code.
Test there and then tell us the result.
RV mineirin
#include <DueFlashStorage.h>
DueFlashStorage dueFlashStorage;
// Define Dig Input's
const int Button0 = 3;
const int Button1 = 4;
const int Button2 = 5;
const int Button3 = 6;
const int Led0 = 7;
const int Led1 = 8;
const int Led2 = 9;
const int Led3 = 10;
// Initiate the global state variable
byte States = 0;
// Define Motor settings states
#define State_S0 0
#define State_S1 1
#define State_S2 2
#define State_S3 3
//---------------------------------------------------------
void writeState()
{
dueFlashStorage.write(0, States);
}
//---------------------------------------------------------
void setup() {
Serial.begin (19200);
States = dueFlashStorage.read(0);
Serial.println(States, HEX);
if (States == 0xFF)
{
dueFlashStorage.write(0, 0);
}
// initialize the pushbutton pin as an input:
pinMode(Button0, INPUT_PULLUP);
pinMode(Button1, INPUT_PULLUP);
pinMode(Button2, INPUT_PULLUP);
pinMode(Button3, INPUT_PULLUP);
// initialize the LED pin as an output:
pinMode(Led0, OUTPUT);
pinMode(Led1, OUTPUT);
pinMode(Led2, OUTPUT);
pinMode(Led3, OUTPUT);
}
//-----------------------------------------------------------
void loop() {
int val0 = digitalRead(Button0);
int val1 = digitalRead(Button1);
int val2 = digitalRead(Button2);
int val3 = digitalRead(Button3);
switch ( States )
{
case State_S0:
digitalWrite(Led0, HIGH);
if ( !val1)
{
digitalWrite(Led0, LOW);
States = State_S1;;
writeState();
}
break;
case State_S1:
digitalWrite(Led1, HIGH);
if ( !val2) {
digitalWrite(Led1, LOW);
States = State_S2;
writeState();
}
break;
case State_S2:
digitalWrite(Led2, HIGH);
if ( !val3) {
digitalWrite(Led2, LOW);
States = State_S3;
writeState();
}
break;
case State_S3:
digitalWrite(Led3, HIGH);
if ( !val0) {
digitalWrite(Led3, LOW);
States = State_S0;
writeState();
}
break;
}
}