Pages: 1 [2] 3   Go Down
Author Topic: need help with AttachInterrupt and EEPROM  (Read 1502 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 52
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Jr. Member
**
Karma: 0
Posts: 52
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Tried a few things, still getting nowhere.  Here's my current code:
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?
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Code:
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.
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 525
Posts: 26554
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

How's the hardware going for that project?
Logged

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.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 52
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How's the hardware going for that project?

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

Offline Offline
Jr. Member
**
Karma: 0
Posts: 52
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Code:
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.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
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.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 52
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, so this:
Code:
EEPROM.write (0, pattern);

would save "pattern" to position 0 then?  Then use this:
Code:
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?
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

What do you mean, "a pattern called 0"?
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17263
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17263
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?  smiley-grin
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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).
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Well I live at 175 Brighton St., when can I expect our check for $175?  smiley-grin

Ah yes, Lefty. But what is the house *called*?
Logged

Pages: 1 [2] 3   Go Up
Jump to: