need help with AttachInterrupt and EEPROM

I need help with AttachInterrupt. I read this http://arduino.cc/en/Reference/AttachInterrupt but I am lost. Here’s my code:

/*
 *  bike light with pattern reverse button & pot
 */
 
int potPin = 0;                 // input pin for the pot
int timer = 50;      // you can adjust the numbers below to set the max and min for the speed adjustment
 int x = 10;        // sets the max speed (0 = fast) the lower the number the faster it can go
 int y = 300;      // sets the min speed (100 = slow) the higher the number the slower it can go

int switchPin = 2;              // switch is connected to pin 2
volatile int state = CHANGE;
int switchPin2 = 3;             // switch to reverse pattern is connected to pin 13
int led1Pin = 11;
int led2Pin = 10;
int led3Pin = 9;
int led4Pin = 8;
int led5Pin = 7;
int led6Pin = 6;
int led7Pin = 5;
int led8Pin = 4;

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 val_rev;                        // variable for reading the pin status
int val2_rev;                       // variable for reading the delayed status
int buttonState_rev;

int lightMode = 0;              // What mode is the light in?

void setup() {
  pinMode(switchPin, INPUT);    // Set the switch pin as input

  pinMode(led1Pin, OUTPUT);
  pinMode(led2Pin, OUTPUT);
  pinMode(led3Pin, OUTPUT);
  pinMode(led4Pin, OUTPUT);
 pinMode(led5Pin, OUTPUT);
  pinMode(led6Pin, OUTPUT);
  pinMode(led7Pin, OUTPUT);
  pinMode(led8Pin, OUTPUT);
  
  Serial.begin(9600);           // Set up serial communication at 9600bps
  buttonState = digitalRead(switchPin);   // read the initial state
}

void loop(){
  digitalWrite(switchPin, state);
  val = digitalRead(switchPin);      // read input value and store it in val
  delay(10);                         // 10 milliseconds is a good amount of time
      timer = 0;
 for ( int i = 0; i < 10; i++ ) {
    timer += analogRead(0);               // reads the pot and sets the timer to the value
}
    timer = timer / 10; 
    timer = map(timer, 0, 1023, x, y);   // maps the timer value to the max and min vaues set above with x and y  
 
  val2 = digitalRead(switchPin);     // read the input again to check for bounces
  val_rev = digitalRead(switchPin2);      //REVERSE read input value and store it in val
  delay(10);                         // 10 milliseconds is a good amount of time
  val2_rev = digitalRead(switchPin2);     // read the input again to check for bounces
  val_rev = digitalRead(switchPin2); 
  if (!val_rev) {                 // make sure we got 2 consistant readings!
buttonState_rev = !buttonState_rev ;                 // save the new state in our variable
  }
  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 (lightMode == 0) {          // if its left to right
          lightMode = 1;               // chase left to right then back!
        } else {
          if (lightMode == 1) {        // if its left to right then back
              lightMode = 2;           // all off
            } else {
              if (lightMode == 2) {    // if its all off
                lightMode = 0;
              }
	    }
        }
      }
    buttonState = val;                 // save the new state in our variable
  }
  // Now do whatever the lightMode indicates
  if (lightMode == 0)  { // single left to right
    if ( buttonState_rev == LOW ) {
    digitalWrite(led1Pin, HIGH);
    delay(timer);
    digitalWrite(led1Pin, LOW);
    digitalWrite(led2Pin, HIGH);
    delay(timer);
    digitalWrite(led2Pin, LOW);
    digitalWrite(led3Pin, HIGH);
    delay(timer);
    digitalWrite(led3Pin, LOW);
    digitalWrite(led4Pin, HIGH);
    delay(timer);
    digitalWrite(led4Pin, LOW);
    digitalWrite(led5Pin, HIGH);
    delay(timer);
    digitalWrite(led5Pin, LOW);
    digitalWrite(led6Pin, HIGH);
    delay(timer);
    digitalWrite(led6Pin, LOW);
    digitalWrite(led7Pin, HIGH);
    delay(timer);
    digitalWrite(led7Pin, LOW);
    digitalWrite(led8Pin, HIGH);
    delay(timer);
    digitalWrite(led8Pin, LOW);
  }
  else  { //single right to left
    digitalWrite(led8Pin, HIGH);
    delay(timer);
    digitalWrite(led8Pin, LOW);
    digitalWrite(led7Pin, HIGH);
    delay(timer);
    digitalWrite(led7Pin, LOW);
    digitalWrite(led6Pin, HIGH);
    delay(timer);
    digitalWrite(led6Pin, LOW);
    digitalWrite(led5Pin, HIGH);
    delay(timer);
    digitalWrite(led5Pin, LOW);
    digitalWrite(led4Pin, HIGH);
    delay(timer); 
    digitalWrite(led4Pin, LOW);
    digitalWrite(led3Pin, HIGH);
    delay(timer);
    digitalWrite(led3Pin, LOW);
    digitalWrite(led2Pin, HIGH);
    delay(timer); 
    digitalWrite(led2Pin, LOW);
    digitalWrite(led1Pin, HIGH);
    delay(timer);
    digitalWrite(led1Pin, LOW);
  }
 }
 if (lightMode == 1)  { // single light left to right then back
    digitalWrite(led1Pin, HIGH);
    delay(timer);
    digitalWrite(led1Pin, LOW);
    digitalWrite(led2Pin, HIGH);
    delay(timer);
    digitalWrite(led2Pin, LOW);
    digitalWrite(led3Pin, HIGH);
    delay(timer);
    digitalWrite(led3Pin, LOW);
    digitalWrite(led4Pin, HIGH);
    delay(timer);
    digitalWrite(led4Pin, LOW);
    digitalWrite(led5Pin, HIGH);
    delay(timer);
    digitalWrite(led5Pin, LOW);
    digitalWrite(led6Pin, HIGH);
    delay(timer);
    digitalWrite(led6Pin, LOW);
    digitalWrite(led7Pin, HIGH);
    delay(timer);
    digitalWrite(led7Pin, LOW);
    digitalWrite(led8Pin, HIGH);
    delay(timer);
    digitalWrite(led8Pin, LOW);
    digitalWrite(led7Pin, HIGH);
    delay(timer);
    digitalWrite(led7Pin, LOW);
    digitalWrite(led6Pin, HIGH);
    delay(timer);
    digitalWrite(led6Pin, LOW);
    digitalWrite(led5Pin, HIGH);
    delay(timer);
    digitalWrite(led5Pin, LOW);
    digitalWrite(led4Pin, HIGH);
    delay(timer);
    digitalWrite(led4Pin, LOW);
    digitalWrite(led3Pin, HIGH);
    delay(timer);
    digitalWrite(led3Pin, LOW);
    digitalWrite(led2Pin, HIGH);
    delay(timer);
    digitalWrite(led2Pin, LOW);
    digitalWrite(led1Pin, HIGH);
  }
  else  {  // reverse
    digitalWrite(led8Pin, HIGH);
    delay(timer); 
    digitalWrite(led8Pin, LOW);
    digitalWrite(led7Pin, HIGH);
    delay(timer);
    digitalWrite(led7Pin, LOW);
    digitalWrite(led6Pin, HIGH);
    delay(timer);
    digitalWrite(led6Pin, LOW);
    digitalWrite(led5Pin, HIGH);
    delay(timer);
    digitalWrite(led5Pin, LOW);
    digitalWrite(led4Pin, HIGH);
    delay(timer);  
    digitalWrite(led4Pin, LOW);
    digitalWrite(led3Pin, HIGH);
    delay(timer);
    digitalWrite(led3Pin, LOW);
    digitalWrite(led2Pin, HIGH);
    delay(timer);
    digitalWrite(led2Pin, LOW);
    digitalWrite(led1Pin, HIGH);
    delay(timer);
    digitalWrite(led1Pin, LOW);
    digitalWrite(led2Pin, HIGH);
    delay(timer);
    digitalWrite(led2Pin, LOW);
    digitalWrite(led3Pin, HIGH);
    delay(timer);
    digitalWrite(led3Pin, LOW);
    digitalWrite(led4Pin, HIGH);
    delay(timer); 
    digitalWrite(led4Pin, LOW);
    digitalWrite(led5Pin, HIGH);
    delay(timer);
    digitalWrite(led5Pin, LOW);
    digitalWrite(led6Pin, HIGH);
    delay(timer);
    digitalWrite(led6Pin, LOW);
    digitalWrite(led7Pin, HIGH);
    delay(timer);
    digitalWrite(led7Pin, LOW);
    digitalWrite(led8Pin, HIGH);
  }
}
 if (lightMode == 2) { // all off
    digitalWrite(led1Pin, LOW);
    digitalWrite(led2Pin, LOW);
    digitalWrite(led3Pin, LOW);
    digitalWrite(led4Pin, LOW);
    digitalWrite(led5Pin, LOW);
    digitalWrite(led6Pin, LOW);
    digitalWrite(led7Pin, LOW);
    digitalWrite(led8Pin, LOW);
  }
}

The problem is, when I push the button to change the pattern (SwitchPin 2), sometimes it won’t change unless I turn the pot first. Please help. Thank you.

You could use interrupts, but you don't need to. The problem is that when you're doing this:

    digitalWrite(led1Pin, HIGH);
    delay(timer);                              // do nothing for (e.g. 0.3 seconds)
    digitalWrite(led1Pin, LOW);
    digitalWrite(led2Pin, HIGH);
    delay(timer);                              // do nothing for (e.g.) 0.3 seconds
    digitalWrite(led2Pin, LOW);
    digitalWrite(led3Pin, HIGH);
    delay(timer);                              // do nothing for (e.g.) 0.3 seconds
    digitalWrite(led3Pin, LOW);
    digitalWrite(led4Pin, HIGH);
    delay(timer);                              // do nothing for (e.g.) 0.3 seconds
    // etc.

you're not doing this

val_rev = digitalRead(switchPin2)

so you'll miss any button presses that begin and end during that time. Take a look at the example BlinkWithoutDelay.

A couple of other points:

the debounce logic looks funky (I haven't followed it through in detail, but it seems to be missing actual use of the values gotten from some of the reads;

this

        if (lightMode == 0) {          // if its left to right
          lightMode = 1;               // chase left to right then back!
        } else {
          if (lightMode == 1) {        // if its left to right then back
              lightMode = 2;           // all off
            } else {
              if (lightMode == 2) {    // if its all off
                lightMode = 0;
              }
	    }

can be replaced by this lightMode = lightMode++ % 3  // 0 = L->R, 1 = L->R->L..., 2 = all off

nyg78:
I need help with AttachInterrupt. ...

I don't see AttachInterrupt in your code. Anyway, as kerthyn says, you can probably solve all your problems by removing the delays and just having some sort of state machine that changes state after a certain amount of time elapses. It could also check for button presses.

  digitalWrite(switchPin, state);
  val = digitalRead(switchPin);

You can’t do both without swapping the pinMode.

This

  val2 = digitalRead(switchPin);     // read the input again to check for bounces
  val_rev = digitalRead(switchPin2);      //REVERSE read input value and store it in val
  delay(10);                         // 10 milliseconds is a good amount of time
  val2_rev = digitalRead(switchPin2);     // read the input again to check for bounces
  val_rev = digitalRead(switchPin2); 
  if (!val_rev) {                 // make sure we got 2 consistant readings!
buttonState_rev = !buttonState_rev ;                 // save the new state in our variable
  } 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 (lightMode == 0) {          // if its left to right
          lightMode = 1;               // chase left to right then back!
        } else {
          if (lightMode == 1) {        // if its left to right then back
              lightMode = 2;           // all off
            } else {
              if (lightMode == 2) {    // if its all off
                lightMode = 0;
              }
	    }
        }
      }
    buttonState = val;                 // save the new state in our variable
  }

is so obfuscated it makes my brain hurt.

This

 if (lightMode == 0)  { // single left to right
    if ( buttonState_rev == LOW ) {
    digitalWrite(led1Pin, HIGH);
    delay(timer);
    digitalWrite(led1Pin, LOW);
    digitalWrite(led2Pin, HIGH);
    delay(timer);
    digitalWrite(led2Pin, LOW);
    digitalWrite(led3Pin, HIGH);
    delay(timer);
    digitalWrite(led3Pin, LOW);
    digitalWrite(led4Pin, HIGH);
    delay(timer);
    digitalWrite(led4Pin, LOW);
    digitalWrite(led5Pin, HIGH);
    delay(timer);
    digitalWrite(led5Pin, LOW);
    digitalWrite(led6Pin, HIGH);
    delay(timer);
    digitalWrite(led6Pin, LOW);
    digitalWrite(led7Pin, HIGH);
    delay(timer);
    digitalWrite(led7Pin, LOW);
    digitalWrite(led8Pin, HIGH);
    delay(timer);
    digitalWrite(led8Pin, LOW);
  }
  else  { //single right to left
    digitalWrite(led8Pin, HIGH);
    delay(timer);
    digitalWrite(led8Pin, LOW);
    digitalWrite(led7Pin, HIGH);
    delay(timer);
    digitalWrite(led7Pin, LOW);
    digitalWrite(led6Pin, HIGH);
    delay(timer);
    digitalWrite(led6Pin, LOW);
    digitalWrite(led5Pin, HIGH);
    delay(timer);
    digitalWrite(led5Pin, LOW);
    digitalWrite(led4Pin, HIGH);
    delay(timer); 
    digitalWrite(led4Pin, LOW);
    digitalWrite(led3Pin, HIGH);
    delay(timer);
    digitalWrite(led3Pin, LOW);
    digitalWrite(led2Pin, HIGH);
    delay(timer); 
    digitalWrite(led2Pin, LOW);
    digitalWrite(led1Pin, HIGH);
    delay(timer);
    digitalWrite(led1Pin, LOW);
  }
 }

can be reduced to about 10 lines of code, something like

byte l2r_pins [] = {4,5,6,7,8,9,10,11};
byte r2l_pins [] = {11,10,9,8,7,6,5,4};


byte *ptr;

if (lightmode == l2r) { 
   ptr = l2r_pins;
else
   ptr = r2l_pins;

for (int i = 0; i < N_LEDS; i++) {
     digitalWrite (*ptr++, HIGH);
     if (i > 0) digitalWrite (i-1, LOW);
     if (i < N_PINS -1) digitalWrite (i+1, LOW);
     delay (timer);
}
10 milliseconds is a good amount of time

For what? Useless comment, remove or explain.

int x = 10;        // sets the max speed (0 = fast) the lower the number the faster it can go
 int y = 300;      // sets the min speed (100 = slow) the higher the number the slower it can go

These are only used once and are not variable, use “const” or “#define” with meaningful names, eg

#define MAX_SPEED 10
#define MIN_SPEED 300

Rob

Thanks all for your help. I seemed to have fixed it. Now, I need help with one other thing. I want this to remember which pattern was displayed after it is turned off then back on. Right now, when I start it, it always starts at 0. If I have it in pattern 1 for example, I want to be able to turn it off then when I turn it back on, have it start at pattern 1. I know it has something to do with EEPROM. I read the examples but can't figure out how to add it to my code and get it to actually work. Please help. Thank you!

Well what have you tried so far? The examples for EEPROM that come with the IDE are pretty simple.

Do I need EEPROM clear, read, or write? Do I need all of them? Also in the write example, it uses Analog Input 0 which I already have a pot attached to. What do I do with this? I want to store what pattern was last used when the board was turned off then back on again.

Did you look at the examples? If so, what do you think you will need?

Look, don't over-complicate it. If they change patterns, save it to (say) address 0 in the EEPROM, like this:

EEPROM.write(0, pattern);

Then in setup, next time it boots, you can get the old pattern back:

pattern = EEPROM.read(0);

That's it! Try and see.

Thanks Nick, but it still doesn’t work. I must be missing something. Here’s what I did.

I added this at the beginning:

#include <EEPROM.h>
int addr = 0;
byte pattern;

In void setup I added this

pattern = EEPROM.read(0);

In void loop I added this:

EEPROM.write(0, pattern);

It still starts at the beginning like before. What am I missing?

Can I see the whole code? Maybe you do this?

pattern = EEPROM.read(0);
pattern = 0;

Who knows without seeing more?

I reverted back to the code in my first post because I still need to fix it. Here’s what I have right now. Everything works but the EEPROM.

/*
 *  bike light with pattern reverse button and pot
 */

#include <EEPROM.h>
int addr = 0;
byte pattern;
 
int potPin = 0;                 // input pin for the pot
int timer = 50;                 // you can adjust the numbers below to set the max and min for the speed adjustment
 int x = 10;                    // sets the max speed (0 = fast) the lower the number the faster it can go
 int y = 300;                   // sets the min speed (100 = slow) the higher the number the slower it can go

int switchPin = 2;              // switch is connected to pin 2
int switchPin2 = 3;             // switch to reverse pattern is connected to pin 3
int led1Pin = 11;
int led2Pin = 10;
int led3Pin = 9;
int led4Pin = 8;
int led5Pin = 7;
int led6Pin = 6;
int led7Pin = 5;
int led8Pin = 4;

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 val_rev;                        // variable for reading the pin status
int val2_rev;                       // variable for reading the delayed status
int buttonState_rev;


int lightMode = 0;              // What mode is the light in?

void setup() {
  pinMode(switchPin, INPUT);    // Set the switch pin as input

  pinMode(led1Pin, OUTPUT);
  pinMode(led2Pin, OUTPUT);
  pinMode(led3Pin, OUTPUT);
  pinMode(led4Pin, OUTPUT);
  pinMode(led5Pin, OUTPUT);
  pinMode(led6Pin, OUTPUT);
  pinMode(led7Pin, OUTPUT);
  pinMode(led8Pin, OUTPUT);
  
  pattern = EEPROM.read(0);
  pattern = 0;

  
  Serial.begin(9600);           // Set up serial communication at 9600bps
  buttonState = digitalRead(switchPin);   // read the initial state
}

void loop(){
  EEPROM.write(0, pattern);
  
  val = digitalRead(switchPin);      // read input value and store it in val
  delay(10);                         
    timer = 0;
 for ( int i = 0; i < 10; i++ ) {
    timer += analogRead(0);               // reads the pot and sets the timer to the value
}
    timer = timer / 10; 
    timer = map(timer, 0, 1023, x, y);   // maps the timer value to the max and min values set above with x and y  
 
  val2 = digitalRead(switchPin);     // read the input again to check for bounces
  val_rev = digitalRead(switchPin2);      //REVERSE read input value and store it in val
  delay(10);                         
  val2_rev = digitalRead(switchPin2);     // read the input again to check for bounces
   val_rev = digitalRead(switchPin2); 
 if (!val_rev) {                 // make sure we got 2 consistant readings!
    buttonState_rev = !buttonState_rev ;  // save the new state in our variable
  }
  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 (lightMode == 0) {          // if its single chase
          lightMode = 1;               // single chase then back
        } else {
          if (lightMode == 1) {        // if its single chase then back
            lightMode = 2;             // off
          } else {
            if (lightMode == 2) {      // off
                  lightMode = 0;
              }
	    }
          }
        }
    buttonState = val;                 // save the new state in our variable
  }

  // Now do whatever the lightMode indicates
  if (lightMode == 0)  { // single chase
    if ( buttonState_rev == LOW ) {
    digitalWrite(led1Pin, HIGH);
    delay(timer);
    digitalWrite(led1Pin, LOW);
    digitalWrite(led2Pin, HIGH);
    delay(timer);
    digitalWrite(led2Pin, LOW);
    digitalWrite(led3Pin, HIGH);
    delay(timer);
    digitalWrite(led3Pin, LOW);
    digitalWrite(led4Pin, HIGH);
    delay(timer);
    digitalWrite(led4Pin, LOW);
    digitalWrite(led5Pin, HIGH);
    delay(timer);
    digitalWrite(led5Pin, LOW);
    digitalWrite(led6Pin, HIGH);
    delay(timer);
    digitalWrite(led6Pin, LOW);
    digitalWrite(led7Pin, HIGH);
    delay(timer);
    digitalWrite(led7Pin, LOW);
    digitalWrite(led8Pin, HIGH);
    delay(timer); 
    digitalWrite(led8Pin, LOW);
    val_rev = digitalRead(switchPin2);
  }
  else  { // reverse chase
    digitalWrite(led8Pin, HIGH);
    delay(timer);
    digitalWrite(led8Pin, LOW);
    digitalWrite(led7Pin, HIGH);
    delay(timer);
    digitalWrite(led7Pin, LOW);
    digitalWrite(led6Pin, HIGH);
    delay(timer);
    digitalWrite(led6Pin, LOW);
    digitalWrite(led5Pin, HIGH);
    delay(timer);
    digitalWrite(led5Pin, LOW);
    digitalWrite(led4Pin, HIGH);
    delay(timer); 
    digitalWrite(led4Pin, LOW);
    digitalWrite(led3Pin, HIGH);
    delay(timer);
    digitalWrite(led3Pin, LOW);
    digitalWrite(led2Pin, HIGH);
    delay(timer); 
    digitalWrite(led2Pin, LOW);
    digitalWrite(led1Pin, HIGH);
    delay(timer);
    digitalWrite(led1Pin, LOW);
    val_rev = digitalRead(switchPin2);
  }
 }
 if (lightMode == 1)  { // single chase then back
    if ( buttonState_rev == LOW ) {
    digitalWrite(led1Pin, HIGH);
    delay(timer); 
    digitalWrite(led1Pin, LOW);
    digitalWrite(led2Pin, HIGH);
    delay(timer);
    digitalWrite(led2Pin, LOW);
    digitalWrite(led3Pin, HIGH);
    delay(timer);
    digitalWrite(led3Pin, LOW);
    digitalWrite(led4Pin, HIGH);
    delay(timer);
    digitalWrite(led4Pin, LOW);
    digitalWrite(led5Pin, HIGH);
    delay(timer);  
    digitalWrite(led5Pin, LOW);
    digitalWrite(led6Pin, HIGH);
    delay(timer);
    digitalWrite(led6Pin, LOW);
    digitalWrite(led7Pin, HIGH);
    delay(timer);
    digitalWrite(led7Pin, LOW);
    digitalWrite(led8Pin, HIGH);
    delay(timer);
    digitalWrite(led8Pin, LOW);
    digitalWrite(led7Pin, HIGH);
    delay(timer);
    digitalWrite(led7Pin, LOW);
    digitalWrite(led6Pin, HIGH);
    delay(timer);
    digitalWrite(led6Pin, LOW);
    digitalWrite(led5Pin, HIGH);
    delay(timer); 
    digitalWrite(led5Pin, LOW);
    digitalWrite(led4Pin, HIGH);
    delay(timer);
    digitalWrite(led4Pin, LOW);
    digitalWrite(led3Pin, HIGH);
    delay(timer);
    digitalWrite(led3Pin, LOW);
    digitalWrite(led2Pin, HIGH);
    delay(timer);
    digitalWrite(led2Pin, LOW);
    digitalWrite(led1Pin, HIGH);
    val_rev = digitalRead(switchPin2);
  }
  else  {  // reverse single chase then back
    digitalWrite(led8Pin, HIGH);
    delay(timer); 
    digitalWrite(led8Pin, LOW);
    digitalWrite(led7Pin, HIGH);
    delay(timer);
    digitalWrite(led7Pin, LOW);
    digitalWrite(led6Pin, HIGH);
    delay(timer);
    digitalWrite(led6Pin, LOW);
    digitalWrite(led5Pin, HIGH);
    delay(timer);
    digitalWrite(led5Pin, LOW);
    digitalWrite(led4Pin, HIGH);
    delay(timer);  
    digitalWrite(led4Pin, LOW);
    digitalWrite(led3Pin, HIGH);
    delay(timer);
    digitalWrite(led3Pin, LOW);
    digitalWrite(led2Pin, HIGH);
    delay(timer);
    digitalWrite(led2Pin, LOW);
    digitalWrite(led1Pin, HIGH);
    delay(timer);
    digitalWrite(led1Pin, LOW);
    digitalWrite(led2Pin, HIGH);
    delay(timer);
    digitalWrite(led2Pin, LOW);
    digitalWrite(led3Pin, HIGH);
    delay(timer);
    digitalWrite(led3Pin, LOW);
    digitalWrite(led4Pin, HIGH);
    delay(timer); 
    digitalWrite(led4Pin, LOW);
    digitalWrite(led5Pin, HIGH);
    delay(timer);
    digitalWrite(led5Pin, LOW);
    digitalWrite(led6Pin, HIGH);
    delay(timer);
    digitalWrite(led6Pin, LOW);
    digitalWrite(led7Pin, HIGH);
    delay(timer);
    digitalWrite(led7Pin, LOW);
    digitalWrite(led8Pin, HIGH);
    val_rev = digitalRead(switchPin2);
  }
 }
  if (lightMode == 2) { // all off
    digitalWrite(led1Pin, LOW);
    digitalWrite(led2Pin, LOW);
    digitalWrite(led3Pin, LOW);
    digitalWrite(led4Pin, LOW);
    digitalWrite(led5Pin, LOW);
    digitalWrite(led6Pin, LOW);
    digitalWrite(led7Pin, LOW);
    digitalWrite(led8Pin, LOW);
    val_rev = digitalRead(switchPin2);
  }
 }
}

The original project this is based on is here: Arduino Tutorial - Lesson 5

You should not have this executing in every loop.

void loop(){
  EEPROM.write(0, pattern);

because you will wear out location 0 in the EEPROM before too long.

Everything works but the EEPROM.

I don't understand what doesn't work, as far as I can see "pattern" is never used, so a) how do you know the EEPROM write isn't working, and b) why do it anyway?

Then you have

 pattern = EEPROM.read(0);
  pattern = 0;

What is this meant to do?


Rob

A quick test shows that the main loop could do 100,000 iterations in about 1/5 of a second.

Since the EEPROM can only handle 100,000 writes, I would guess you have worn it out in 0.2 seconds.

My original suggestion was:

If they change patterns, save it to (say) address 0 in the EEPROM ...

Emphasis added this time around. If you read up on the EEPROM library there is a clear warning on the write function that you can only do approximately 100,000 writes.

This is why I said "if they change patterns". I was hoping that patterns wouldn't change more than perhaps a few hundred or even a few thousand times.

And why do you do this:

pattern = EEPROM.read(0);
  pattern = 0;

Surely it's obvious that pattern will always be zero and thus it will always start back at the start?

Nick, I was doing what you suggested in Reply #9. Patterns shouldn't change that much, I just want it to remember which one was last used when turned back on.

Maybe I wasn't clear. That was an example of why it would not work. That was why I wanted to see more of your code. Clearly if you read pattern, and then change it later, you may as well not have bothered reading it.

If you post snippets of code, and say the whole thing doesn't work, then we have to guess what else there is that might cause it not to work. That two lines of code was such a guess. Unfortunately you copied my "bad example" into your code.

My code is in Reply #10. Do you (or anybody else) have any suggestions on what to change so it will remember what the last pattern displayed was when it is turned off then back on again? This is harder than I thought it would be.

My code is in Reply #10.

And there have been many posts since then pointing out problems. Have you corrected them? If so, post the new code. If not, fix them.

This is harder than I thought it would be.

Only because you are making it hard. It really isn't. Read the value in setup(). Write the value ONLY WHEN IT ACTUALLY CHANGES in loop().

Tried a few things, still getting nowhere. Here’s my current code:

/*
 *  bike light with pattern reverse button and pot
 */

#include <EEPROM.h>
int addr = 0;
byte pattern;
 
int potPin = 0;                 // input pin for the pot
int timer = 50;                 // you can adjust the numbers below to set the max and min for the speed adjustment
 int x = 10;                    // sets the max speed (0 = fast) the lower the number the faster it can go
 int y = 300;                   // sets the min speed (100 = slow) the higher the number the slower it can go

int switchPin = 2;              // switch is connected to pin 2
int switchPin2 = 3;             // switch to reverse pattern is connected to pin 3
int led1Pin = 11;
int led2Pin = 10;
int led3Pin = 9;
int led4Pin = 8;
int led5Pin = 7;
int led6Pin = 6;
int led7Pin = 5;
int led8Pin = 4;

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 val_rev;                        // variable for reading the pin status
int val2_rev;                       // variable for reading the delayed status
int buttonState_rev;


int lightMode = 0;              // What mode is the light in?

void setup() {
  pinMode(switchPin, INPUT);    // Set the switch pin as input

  pinMode(led1Pin, OUTPUT);
  pinMode(led2Pin, OUTPUT);
  pinMode(led3Pin, OUTPUT);
  pinMode(led4Pin, OUTPUT);
  pinMode(led5Pin, OUTPUT);
  pinMode(led6Pin, OUTPUT);
  pinMode(led7Pin, OUTPUT);
  pinMode(led8Pin, OUTPUT);
  
  pattern = EEPROM.read(0);

  
  Serial.begin(9600);           // Set up serial communication at 9600bps
  buttonState = digitalRead(switchPin);   // read the initial state
}

void loop(){
  val = digitalRead(switchPin);      // read input value and store it in val
  delay(10);                         
    timer = 0;
 for ( int i = 0; i < 10; i++ ) {
    timer += analogRead(0);               // reads the pot and sets the timer to the value
}
    timer = timer / 10; 
    timer = map(timer, 0, 1023, x, y);   // maps the timer value to the max and min values set above with x and y  
 
  val2 = digitalRead(switchPin);     // read the input again to check for bounces
  val_rev = digitalRead(switchPin2);      //REVERSE read input value and store it in val
  delay(10);                         
  val2_rev = digitalRead(switchPin2);     // read the input again to check for bounces
   val_rev = digitalRead(switchPin2); 
 if (!val_rev) {                 // make sure we got 2 consistant readings!
    buttonState_rev = !buttonState_rev ;  // save the new state in our variable
  }
  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
      EEPROM.write(0, pattern);
        if (lightMode == 0) {          // if its single chase
          lightMode = 1;               // single chase then back
        } else {
          if (lightMode == 1) {        // if its single chase then back
            lightMode = 2;             // off
          } else {
            if (lightMode == 2) {      // off
                  lightMode = 0;
              }
	}
          }
        }
    buttonState = val;                 // save the new state in our variable
  }

  // Now do whatever the lightMode indicates
  if (lightMode == 0)  { // single chase
    if ( buttonState_rev == LOW ) {
    digitalWrite(led1Pin, HIGH);
    delay(timer);
    digitalWrite(led1Pin, LOW);
    digitalWrite(led2Pin, HIGH);
    delay(timer);
    digitalWrite(led2Pin, LOW);
    digitalWrite(led3Pin, HIGH);
    delay(timer);
    digitalWrite(led3Pin, LOW);
    digitalWrite(led4Pin, HIGH);
    delay(timer);
    digitalWrite(led4Pin, LOW);
    digitalWrite(led5Pin, HIGH);
    delay(timer);
    digitalWrite(led5Pin, LOW);
    digitalWrite(led6Pin, HIGH);
    delay(timer);
    digitalWrite(led6Pin, LOW);
    digitalWrite(led7Pin, HIGH);
    delay(timer);
    digitalWrite(led7Pin, LOW);
    digitalWrite(led8Pin, HIGH);
    delay(timer); 
    digitalWrite(led8Pin, LOW);
    val_rev = digitalRead(switchPin2);
  }
  else  { // reverse chase
    digitalWrite(led8Pin, HIGH);
    delay(timer);
    digitalWrite(led8Pin, LOW);
    digitalWrite(led7Pin, HIGH);
    delay(timer);
    digitalWrite(led7Pin, LOW);
    digitalWrite(led6Pin, HIGH);
    delay(timer);
    digitalWrite(led6Pin, LOW);
    digitalWrite(led5Pin, HIGH);
    delay(timer);
    digitalWrite(led5Pin, LOW);
    digitalWrite(led4Pin, HIGH);
    delay(timer); 
    digitalWrite(led4Pin, LOW);
    digitalWrite(led3Pin, HIGH);
    delay(timer);
    digitalWrite(led3Pin, LOW);
    digitalWrite(led2Pin, HIGH);
    delay(timer); 
    digitalWrite(led2Pin, LOW);
    digitalWrite(led1Pin, HIGH);
    delay(timer);
    digitalWrite(led1Pin, LOW);
    val_rev = digitalRead(switchPin2);
  }
 }
 if (lightMode == 1)  { // single chase then back
    if ( buttonState_rev == LOW ) {
    digitalWrite(led1Pin, HIGH);
    delay(timer); 
    digitalWrite(led1Pin, LOW);
    digitalWrite(led2Pin, HIGH);
    delay(timer);
    digitalWrite(led2Pin, LOW);
    digitalWrite(led3Pin, HIGH);
    delay(timer);
    digitalWrite(led3Pin, LOW);
    digitalWrite(led4Pin, HIGH);
    delay(timer);
    digitalWrite(led4Pin, LOW);
    digitalWrite(led5Pin, HIGH);
    delay(timer);  
    digitalWrite(led5Pin, LOW);
    digitalWrite(led6Pin, HIGH);
    delay(timer);
    digitalWrite(led6Pin, LOW);
    digitalWrite(led7Pin, HIGH);
    delay(timer);
    digitalWrite(led7Pin, LOW);
    digitalWrite(led8Pin, HIGH);
    delay(timer);
    digitalWrite(led8Pin, LOW);
    digitalWrite(led7Pin, HIGH);
    delay(timer);
    digitalWrite(led7Pin, LOW);
    digitalWrite(led6Pin, HIGH);
    delay(timer);
    digitalWrite(led6Pin, LOW);
    digitalWrite(led5Pin, HIGH);
    delay(timer); 
    digitalWrite(led5Pin, LOW);
    digitalWrite(led4Pin, HIGH);
    delay(timer);
    digitalWrite(led4Pin, LOW);
    digitalWrite(led3Pin, HIGH);
    delay(timer);
    digitalWrite(led3Pin, LOW);
    digitalWrite(led2Pin, HIGH);
    delay(timer);
    digitalWrite(led2Pin, LOW);
    digitalWrite(led1Pin, HIGH);
    val_rev = digitalRead(switchPin2);
  }
  else  {  // reverse single chase then back
    digitalWrite(led8Pin, HIGH);
    delay(timer); 
    digitalWrite(led8Pin, LOW);
    digitalWrite(led7Pin, HIGH);
    delay(timer);
    digitalWrite(led7Pin, LOW);
    digitalWrite(led6Pin, HIGH);
    delay(timer);
    digitalWrite(led6Pin, LOW);
    digitalWrite(led5Pin, HIGH);
    delay(timer);
    digitalWrite(led5Pin, LOW);
    digitalWrite(led4Pin, HIGH);
    delay(timer);  
    digitalWrite(led4Pin, LOW);
    digitalWrite(led3Pin, HIGH);
    delay(timer);
    digitalWrite(led3Pin, LOW);
    digitalWrite(led2Pin, HIGH);
    delay(timer);
    digitalWrite(led2Pin, LOW);
    digitalWrite(led1Pin, HIGH);
    delay(timer);
    digitalWrite(led1Pin, LOW);
    digitalWrite(led2Pin, HIGH);
    delay(timer);
    digitalWrite(led2Pin, LOW);
    digitalWrite(led3Pin, HIGH);
    delay(timer);
    digitalWrite(led3Pin, LOW);
    digitalWrite(led4Pin, HIGH);
    delay(timer); 
    digitalWrite(led4Pin, LOW);
    digitalWrite(led5Pin, HIGH);
    delay(timer);
    digitalWrite(led5Pin, LOW);
    digitalWrite(led6Pin, HIGH);
    delay(timer);
    digitalWrite(led6Pin, LOW);
    digitalWrite(led7Pin, HIGH);
    delay(timer);
    digitalWrite(led7Pin, LOW);
    digitalWrite(led8Pin, HIGH);
    val_rev = digitalRead(switchPin2);
  }
 }
  if (lightMode == 2) { // all off
    digitalWrite(led1Pin, LOW);
    digitalWrite(led2Pin, LOW);
    digitalWrite(led3Pin, LOW);
    digitalWrite(led4Pin, LOW);
    digitalWrite(led5Pin, LOW);
    digitalWrite(led6Pin, LOW);
    digitalWrite(led7Pin, LOW);
    digitalWrite(led8Pin, LOW);
    val_rev = digitalRead(switchPin2);
  }
 }
}

Since I have a pattern called “0” would I need to change that in order for EEPROM to read and write to “0” or is that something different?

byte pattern;

...

pattern = EEPROM.read(0);

...

EEPROM.write(0, pattern);

They are the only references to pattern in your code. What makes you think pattern will ever change?

You don't alter the value anywhere. You don't use the value you read from EEPROM, and you don't save the value from the button.

Also you don't check if pattern has changed before you write it.

How's the hardware going for that project?