Go Down

Topic: need help with AttachInterrupt and EEPROM (Read 1 time) previous topic - next topic

nyg78

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.

PaulS

Quote
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.

Quote
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().

nyg78

Tried a few things, still getting nowhere.  Here's my current code:
Code: [Select]


/*
*  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?

Nick Gammon

Quote
Code: [Select]

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.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

CrossRoads

How's the hardware going for that project?
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

nyg78


How's the hardware going for that project?


As soon as I get this part figured out, it's a go.

nyg78


Quote
Code: [Select]

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.



I get what you are saying Nick.  I have looked at the examples in my Arduino 0022 program and at Playground, but I don't see how to do it from those examples.  I'm completely lost.  Never used EEPROM before.

Nick Gammon

The EEPROM is just another form of memory. You have 1024 bytes of it on the Atmega328 numbered from 0 to 1023.

Once written to, it remembers what you put there, even after a power-off.

You simply choose a byte number where you want to store something. eg.

Code: [Select]

EEPROM.write (55, 123);


That would save the number 123 into position 55 in the EEPROM.

Then when you power off the Arduino, and start it up again, you can get that back ...

Code: [Select]

byte mysecretnumber = EEPROM.read(55);


Now you would get back 123 into the variable mysecretnumber.

That's it basically. So you need to save something important, when it changes, and then you can get it back next time. Bear in mind that after 100,000 writes that memory location might become "worn out" so you don't keep writing the same thing to it, especially not every time around the main loop.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

nyg78

Ok, so this:
Code: [Select]

EEPROM.write (0, pattern);


would save "pattern" to position 0 then?  Then use this:
Code: [Select]

byte pattern = EEPROM.read(0);

to recall that pattern?  Now, because I have a pattern called 0, will I need to change the EEPROM position number, or won't that matter?

Nick Gammon

What do you mean, "a pattern called 0"?
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Nick Gammon

I think you are getting confused here. Variables contain numbers. They aren't "called" numbers.

Let me give you an example. Say you live at 5 High Street. That is your address. Now if I want to send you $5 I send it to 5 High Street. But the $5 and the street number (although both 5) are different things. If I want to send you $10 I don't mail it to 10 High Street, do I? Because you don't live there.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

retrolefty

Quote
because I have a pattern called 0


No, you have a byte of data written into memory address zero of the EEPROM memory area. The value of that memory location came from the value of the variable you named pattern in your sketch. If you loaded a different sketch into your board, there may very well no longer be a variable called pattern, but the contents of address 0 of EEPROM memory would still contain a byte of data of a value that is was in the pattern variable when you last wrote it to EEPROM memory. The EEPROM has 1024 memory locations each able to store a single byte of data, the valid address values are 0-1023 and the contents of the data you can store at any given address can be 0-255.

That help?

Lefty

retrolefty


I think you are getting confused here. Variables contain numbers. They aren't "called" numbers.

Let me give you an example. Say you live at 5 High Street. That is your address. Now if I want to send you $5 I send it to 5 High Street. But the $5 and the street number (although both 5) are different things. If I want to send you $10 I don't mail it to 10 High Street, do I? Because you don't live there.


Well I live at 175 Brighton St., when can I expect our check for $175?  :D

Nick Gammon


Now, because I have a pattern called 0, will I need to change the EEPROM position number, or won't that matter?


In short, no you don't change it. Think of my example of the street numbers. The pattern, whether it is zero or something else, is stored and recalled from EEPROM location zero (in this example).
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Nick Gammon


Well I live at 175 Brighton St., when can I expect our check for $175?  :D


Ah yes, Lefty. But what is the house *called*?
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Go Up