DIY LightSaber

hello,

i am currently making a lightsaber using the arduino nano.

i have my code working, however when i power down the saber, and then power up again the color always go back to light blue. i understand that the code is doing that since i set the power up color to light blue.

What i would i like to do is, no matter what color i power down at, if i power up back up it goes back to that color.

I have no idea how to even start this, any advice will be greatly appreciated!!!

#include <FastLED.h>

#define buttonPin 2
#define LED_PIN 6
#define NUM_LEDS 58

CRGB leds[NUM_LEDS];

const byte Brightness = 200;
const int LEDTime = 40; // delay time between each LED
const int Timer = 100;
unsigned long pressLength = 0; // # how long the button is held down in milliseconds
bool lightState = false; // State of light, on or off.
int colorState = 0; // Which color the blade is.

/*
    - - -  Options for different durations of pressLength (milliseconds) - - -
  Remember to put conditions with longer pressLength first

*/

const int optionOne = 500; // power up saber and power down Saber
const int optionTwo = 20; // Change Blade color, Light Blue, Blue, Red, Green, Purple

void setup() {
  Serial.begin(9600);
  FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
  pinMode(buttonPin, INPUT);
}


// - - - Start of Main Loop - - -

void loop() {

  pressLength = 0;

  while (digitalRead(buttonPin) == HIGH) {
    delay(Timer);
    pressLength += Timer;
    //    Serial.println(pressLength);
  }

  if (pressLength >= optionOne && lightState == false) {
    // power on blade
    powerUp();
    colorState = 1;
    lightState = true;
  }


  else if (pressLength >= optionOne && lightState == true) {
    // power off blade
    powerDown();
    lightState = false;
  }

  else if (pressLength >= optionTwo && lightState == true) {
    // change blade color
    changeColor();
    delay(5);
  }
}
// - - - end of Main Loop - - -

// - - - Functions - - -

void powerUp() { // power on saber

  for (int i = 0; i < NUM_LEDS; i++) {
    leds[i] = CRGB(0, 100, Brightness);
    delay(LEDTime);
    FastLED.show();
  }
}


void powerDown() { // power down saber

  for (int i = NUM_LEDS - 1; i >= 0; i--) {
    leds[i] = CRGB(0, 0, 0);
    delay(LEDTime);
    FastLED.show();
  }
}


void changeColor() { // Change the color of the blade

  switch (colorState) {
    case 0:
      fill_solid(leds, NUM_LEDS, CRGB(0, 100, Brightness));
      break;

    case 1:
      fill_solid(leds, NUM_LEDS, CRGB(0, 0, Brightness));
      break;

    case 2:
      fill_solid(leds, NUM_LEDS, CRGB(Brightness, 0, 0));
      break;

    case 3:
      fill_solid(leds, NUM_LEDS, CRGB(0, Brightness, 0));
      break;

    case 4:
      fill_solid(leds, NUM_LEDS, CRGB(188, 0, 211));
      FastLED.show();
  }
  FastLED.show();
  colorState++;
  if (colorState > 4) {
    colorState = 0;
  }

}

Try this;

https://www.arduino.cc/en/Reference/EEPROM

A warning. The EEPROM is good for 100,000 writes per cell. After 100,000 writes a cell cannot be guaranteed to work right. 100,000 sounds like a lot, but it goes by real fast at 16MHz if you make a mistake and write to it too often. You can minimize the number of writes by only writing to EEPROM when the data changes (see the update() function).

groundFungus:
A warning. The EEPROM is good for 100,000 writes per cell. After 100,000 writes a cell cannot be guaranteed to work right. 100,000 sounds like a lot, but it goes by real fast at 16MHz if you make a mistake and write to it too often. You can minimize the number of writes by only writing to EEPROM when the data changes (see the update() function).

Good points. To have persistent memory between reboots there is not really any option apart from using EEPROM though is there?

Not that I know of. The least use is if there is a clean shut down function that can be executed before the unit is shut down, where the state can be saved just once per restart. If the unit can shut down at any time, unpredictably, you about have to save the state any time that the state changes or lose it on an un-predicted shut down.

The only other solution that I can think of would be to use latching relays. Three relays could be used to "remember" which of 8 colours was last selected.

That seems a bit of overkill though. I don't see the harm in the sabre coming up in its default state if it was not shutdown cleanly.

Add an external part to store values. FRAM has SRAM read/write speeds, EEPROM non-volatiliy. 10^14 write cycles (trillions).