keeping mode val even when power is off

hello I'm trying to create a Rgb controller board for my trunk of my car.In my trunk I have a main 16 gauge wire running from the battery to the trunk this will be main power for the module. this wire will supply 12v to the module witch will be dropped by 3x LM323. I have the gnd leads connected to a tip120 transistor. what I have done is disconnected the trunk light and put the 12v right to the base and gnd flows through and powers the main module. the module it consists of 4 75HC595 shift registers 1 for each led color diode within the led then one for a 7 segment led display to show me which mode i'm in. all connected to a arduino nano Now here is the problem I have a mode button, when I press it jumps to the next mode. I works great but when I close my trunk the power cuts off to transistor and the mode val reverts back to 0. so when I open my trunk It starts at mode 0 pattern instead of mode 3 pattern. if the pot val equaled between 0-100 mode will be 0 and if the value equaled between 110-200 mode will be 1 and so on

now I need to figure out a way to keep the pattern val even if the power is cut.\ I have thought of 1 of 2 ways 1. write the mode val to eeprom. problem is that I don't want to have the eeprom erased and rewritten with the same value every time it makes a loop.

  1. I was thinking of using a pot that I can turn to select the modes, but the problem with this approach. is that I need to have the pot value be bet tween 2 values so

int data = 2; //red int latch = 3; // int clock = 4; // int data2 = 5; // green int latch2 = 6; // int clock2 = 7; // int data3 = 8; // blue int latch3 = 9; // int clock3 = 10; // int switchpin = 11; // switch is connected to pin 11 for mode switch int timer = 150; //led refresh int leddata = 12; // led 7 seqment int ledlatch = 13; // int ledclock = A0; //

//button int switchPin = 11; // switch is connected to pin 2

int val; // variable for reading the pin status int val2; // variable for reading the delayed status int buttonState; // variable to hold the button state int value=0; int Mode = 0; // What mode is the light in?

void setup() { pinMode(switchPin, INPUT); // Set the switch pin as input pinMode (data, OUTPUT); pinMode (data2, OUTPUT); pinMode (data3, OUTPUT); pinMode (latch, OUTPUT); pinMode (latch2, OUTPUT); pinMode (latch3, OUTPUT); pinMode (clock, OUTPUT); pinMode (clock2, OUTPUT); pinMode (clock3, OUTPUT); pinMode (leddata, OUTPUT); pinMode (ledlatch, OUTPUT); pinMode (ledclock, OUTPUT);

Serial.begin(9600);

}

void loop(){

val = digitalRead(switchPin); // read input value and store it in val delay(10); // 10 milliseconds is a good amount of time val2 = digitalRead(switchPin); // read the input again to check for bounces if (val == val2) { // make sure we got 2 consistant readings! if (val != buttonState) { // the button state has changed! if (val == LOW) { // check if the button is pressed if (Mode == 0) { // if its off Mode = 1; // turn lights on! } else { if (Mode == 1) { // if its all-on Mode = 2; // make it blink! } else { if (Mode == 2) { // if its blinking Mode = 3; // make it wave! } else { if (Mode == 3) { // if its waving, Mode = 4; // turn light off! } else { if (Mode == 4) { // if its blinking Mode = 5; // make it wave! } else { if (Mode == 5) { // if its waving, Mode = 0; // turn light off!

} } } } } } } }

buttonState = val; // save the new state in our variable

Please click Modify ( or ) on your post, select the source code, click the button above the edit window, then click .

stainedglasswiz:

  1. write the mode val to eeprom

Correct choice.

  val = digitalRead(switchPin);      // read input value and store it in val
  delay(10);                         // 10 milliseconds is a good amount of time
  val2 = digitalRead(switchPin);     // read the input again to check for bounces
  if (val == val2) {                 // make sure we got 2 consistant readings!
    if (val != buttonState) {          // the button state has changed!
      if (val == LOW) {                // check if the button is pressed


        ++Mode;
        if ( Mode > 5 )
        {
          Mode = 0;
        }
        // Write Mode to the EEPROM here.


      }
    }

thank you for the fast response now, Now I have not ever used the eeprom read write. I need to know where to insert EEPROM.write and the EEPROM.read. and should worry about the eeprom going bad in a few years from all the writes and erases

I need to know where to insert EEPROM.write

Where you want to write the value. Where does the value you want to save change? It is after the value changes that you want to save it.

and the EEPROM.read.

When does it matter what the value is? When the Arduino resets. What is the first thing that happens on reset? The setup() function is executed. That seems like a good place to me to read the initial value.

should worry about the eeprom going bad in a few years from all the writes and erases

Yes, but... How long do you expect to use the Arduino to run this sketch? How often do you expect mode to change? You only need to store mode when it changes.

I tried to insert the the EEprom.write and the EEProm.read into my sketch. and it does not work. am I doin something wrong??

#include <EEPROM.h>

int data = 2;   //pin 15 on the 75HC595
int latch = 3;  //pin 12 on the 75HC595
int clock = 4; //pin 10 on the  75HC595
int data2 = 5;   //pin 15 on the #2 75HC595
int latch2 = 6;  //pin 12 on the #2 75HC595
int clock2 = 7; //pin 10 on the  #2 75HC595
int data3 = 8;   //pin 15 on the #3 75HC595
int latch3 = 9;  //pin 12 on the #375HC595
int clock3 = 10; //pin 10 on the #375HC595
int switchpin = 11; // switch is connected to pin 11 for mode switch
int timer = 150; //led refresh

//button
int switchPin = 11;              // switch is connected to pin 2


int val;                        // variable for reading the pin status
int val2;                       // variable for reading the delayed status
int buttonState;                // variable to hold the button state
int value = 0;
int Mode = 0;              // What mode is the light in?
int storedval = 0;


void setup() {
  pinMode(switchPin, INPUT);    // Set the switch pin as input
   pinMode (data, OUTPUT);
  pinMode (data2, OUTPUT);
  pinMode (data3, OUTPUT);
  pinMode (latch, OUTPUT);
  pinMode (latch2, OUTPUT);
  pinMode (latch3, OUTPUT);
  pinMode (clock, OUTPUT);
  pinMode (clock2, OUTPUT);
  pinMode (clock3, OUTPUT);
   pinMode(12, OUTPUT);
  pinMode(A0, INPUT);

    Serial.begin(9600);
  Mode = EEPROM.read(storedval);
 
  
}

void loop(){
  
  val = digitalRead(switchPin);      // read input value and store it in val
  delay(10);                         // 10 milliseconds is a good amount of time
  val2 = digitalRead(switchPin);     // read the input again to check for bounces
  if (val == val2) {                 // make sure we got 2 consistant readings!
    if (val != buttonState) {          // the button state has changed!
      if (val == LOW) {                // check if the button is pressed
        if (Mode == 0) {          // if its off
          Mode = 1;               // turn lights on!
        } else {
          if (Mode == 1) {        // if its all-on
            Mode = 2;             // make it blink!
          } else {
            if (Mode == 2) {      // if its blinking
              Mode = 3;           // make it wave!
            } else {
	      if (Mode == 3) { //  if its waving, 
                Mode = 4;           // turn light off!
                 } else {
                 if (Mode == 4) {      // if its blinking
              Mode = 5;           // make it wave!
            } else {
			  if (Mode == 5) { //  if its waving, 
                Mode = 0;           // turn light off!
                
}
            }
}
            }
          }
        }
      }
    }
 EEPROM.write(storedval, Mode);

buttonState = val;                 // save the new state in our variable

A couple of points. First, remember that the value type you write into the EEPROM is a BYTE. Second, I would put in a check in the setup section which reads EEPROM address 0 and looks for a particular value (e.g. 0xa5). If it reads this, then you know the EEPROM contains valid information and is not empty. Go ahead and read from address 1 the value of MODE.
If it does not contain the value (again, 0xa5), then write this value to address 0 of EEPROM and a default value to address 1 for MODE.

If your EEPROM is empty, then the first time you read it, the value of MODE will be undetermined.

        if (Mode == 0) {          // if its off
          Mode = 1;               // turn lights on!
        } else {
          if (Mode == 1) {        // if its all-on
            Mode = 2;             // make it blink!
          } else {
            if (Mode == 2) {      // if its blinking
              Mode = 3;           // make it wave!
            } else {
	      if (Mode == 3) { //  if its waving, 
                Mode = 4;           // turn light off!
                 } else {
                 if (Mode == 4) {      // if its blinking
              Mode = 5;           // make it wave!
            } else {
			  if (Mode == 5) { //  if its waving, 
                Mode = 0;           // turn light off!

can be replaced with:

   Mode++;
    if(Mode > 5)
      Mode = 0;

storedval is a lousy name for a variable that holds an ADDRESS.