Start in the same state after turning the power off and on

Hi,

What I want the program to start in the same state after turning the power off and on.
When I test the program it puts 255 in settings instead of 0, 1, 2 or 3

Please advise how I can solve the problem.

#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 setup() {

  States = dueFlashStorage.read(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, INPUT);	
	  pinMode(Led1, INPUT);
	  pinMode(Led2, INPUT);
	  pinMode(Led3, INPUT);

  Serial.begin (19200);
}

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;
      }
    break;
  
		case State_S1:
		digitalWrite(Led1, HIGH);
		if ( !val2){
			  digitalWrite(Led1, LOW);
			  States = State_S2;
        }
    break;
  
    case State_S2:
		digitalWrite(Led2, HIGH);
		if ( !val3){
			digitalWrite(Led2, LOW);
			States = State_S3;
		}
    break;

    case State_S3:
		digitalWrite(Led3, HIGH);
		if ( !val0){
			digitalWrite(Led3, LOW);
			States = State_S0;
		}
	 break;
  }
    dueFlashStorage.write(0, States);

      Serial.println(States);
}

Hi @Jan123456;
put a Serial.println(state,HEX); after the line
States = dueFlashStorage.read(0);
and tell us what was printed.
Ops. Put Serial.begin (19200); in the setup() first line.
RV mineirin

Hi,

I get FF

Hi @Jan123456
All LEDs are with pinMode(x, INPUT);
Is it correct or should it be OUTPUT?

RV mineirin

This may be nothing to do with your issues, but you are writing to the same memory location every time around your loop which could be 1000's of times a second. With a real EEPROM device, that would wear out that memory address in a couple of minutes or so.

I've not used a DUE but you are using FLASH memory for your storage. Does your library take care of erasing the memory (location/page/sector) before writing the new value. FLASH needs to be erased (back to 0xFF I think) if you want to write a new value.

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;
  }
}

You are probably reading 0xFF because the act of uploading a new sketch erases the flash memory.

From the DueFlashStorage github page:

Note: The flash storage is reset every time you upload a new sketch to your Arduino.

@ruilviana , I'm not sure that writing 0 to the flash will work. It all depends on how the flash library handles writes. If I recall, with flash, you can change a bit from a 1 to a 0 without erasing, but you can't change a 0 back to a 1 without erasing some portion of the flash device first.

I may be wrong here - because I've not used flash much - but if you were to write 0x02 to a location, then you can't then overwrite it with 0x03 as that would involve turning a 0 back to a 1. You can write 0x03, but it will be read back as 0x02.

Hi @markd833
Before publishing the code I tested it here with an Arduino Due and it worked correctly.

I used the reset, turned it off and on several times and the flash value remained.
Only returned to 0XFF when re-uploading the code.

RV mineirin

I guess that the flash library must take care of page/sector erases behind the scenes!

Hi, ruilviana/markd833

Sorry about Leds they must indeed be outputs.
I have tested the modified program and it works.
I have to say that I don't fully understand how it works yet.
But I'm going to apply it in my project.

Thanks for support,
Jan

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.