Could anyone here have a look at my eeprom code and possibly suggest what's wrong with the code as to why it won't save? This eeprom code works well in other programs I've written so there's something I missed but I'm not seeing it. It's just the eeprom code that's not working, the rest of the program functions perfectly. Any help with this will be very much appreciated.
Thanks
jessey
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//This is the code I started out with for my PWM Motor Speed Controller
//This is the URL where I got the code: http://www.dummies.com/how-to/content/how-to-control-the-speed-of-a-dc-motor-with-the-ar.html
//Topic: How to turn off PWM: URL-----> http://forum.arduino.cc/index.php?topic=381881.0
//Topic: Arduino PWM Motor Speed Controller Questions: URL-----> http://forum.arduino.cc/index.php?topic=378883.0
//Topic: mega 2560 PWM frequency: URL-----> http://forum.arduino.cc/index.php?topic=72092.msg539486#msg539486
#include <EEPROM.h>
int potPin = A0;
int motorPin = 9;
int potValue = 0;
int motorValue = 0;
int CCW_Led_Indicator = 10;
int CW_Led_Indicator = 11;
int relay = 12;
#define CCW_LED_INDICATOR_ADDR 0
#define CW_LED_INDICATOR_ADDR CCW_LED_INDICATOR_ADDR + sizeof(int)
#define RELAY_ADDR CW_LED_INDICATOR_ADDR + sizeof(int)
void setup()
{
pinMode(8, INPUT);// set to input for motor reversing pushbutton pin 8
pinMode(CCW_Led_Indicator, OUTPUT); //CCW_Led_Indicator on pin 10
pinMode(CW_Led_Indicator, OUTPUT); //CW_Led_Indicator on pin 11
pinMode(relay, OUTPUT); // pin 12
// this is where the program retrieves the saved variables...
EEPROM.get( CCW_LED_INDICATOR_ADDR, CCW_Led_Indicator );
EEPROM.get( CW_LED_INDICATOR_ADDR, CW_Led_Indicator );
EEPROM.get( RELAY_ADDR, relay);
}
void loop()
{
// these 4 lines of code below changes the PWM frequency from
// the default of 490 Hz to 31000 Hz to get rid of motor humming.
int myEraser = 7;
TCCR2B &= ~myEraser;
int myPrescaler = 1; // 1 = 31000 Hz
TCCR2B |= myPrescaler;
Serial.begin(9600);
if (digitalRead(8) == LOW) // if the switch on pin 8 is pressed then implement code below
{ //----------start of switching the rotation of the motor shaft---------------- beginning of If digitalRead8=LOW
while(digitalRead(8) == LOW) // program remains here until pushbutton is released
{ //---------------------------------------------------------------------------- beginning of while digitalRead8=LOW
analogWrite(motorPin, 0); // shut off the motor before changing the rotation
// these if statements below will slow the motor down to almost a stop before the motor switches rotation.
if (motorValue >= 0 && motorValue <= 80) {delay(250);}
if (motorValue >= 81 && motorValue <= 110) {delay(400);}
if (motorValue >= 111 && motorValue <= 130) {delay(500);}
if (motorValue >= 131 && motorValue <= 180) {delay(650);}
if (motorValue >= 181 && motorValue <= 210) {delay(800);}
if (motorValue >= 211 && motorValue <= 230) {delay(900);}
if (motorValue >= 231 && motorValue <= 255) {delay(1100);}
// circuit uses 1 amp when motor = from 158 to 166 and potentiometer = from 637 to 668 as shown on the serial monitor.
// circuit uses 1.25 amps when motor = 255 and potentiometer = 1023, this is the top speed of the motor.
} //---------------------------------------------------------------------------- end of while digitalRead8=LOW
// this if statement below turns the relay and rotation indicator leds on/off
if (digitalRead(relay) == HIGH)
{
digitalWrite(relay, LOW); // pin 12
digitalWrite(CCW_Led_Indicator, LOW); // Pin 11 of Mega 2560
digitalWrite(CW_Led_Indicator, HIGH); // Pin 10 of Mega 2560
EEPROM.put( RELAY_ADDR, relay );
EEPROM.put( CCW_LED_INDICATOR_ADDR, CCW_Led_Indicator );
EEPROM.put( CW_LED_INDICATOR_ADDR, CW_Led_Indicator );
}// these 3 lines of code above should save the variables
else
{
digitalWrite(relay, HIGH);
digitalWrite(CCW_Led_Indicator, HIGH); // Pin 11 of Mega 2560
digitalWrite(CW_Led_Indicator, LOW); // Pin 10 of Mega 2560
EEPROM.put( RELAY_ADDR, relay );
EEPROM.put( CCW_LED_INDICATOR_ADDR, CCW_Led_Indicator );
EEPROM.put( CW_LED_INDICATOR_ADDR, CW_Led_Indicator );
}// these 3 lines of code above should save the variables
} //------end of switching the rotation of the motor shaft---------------------- end of If digitalRead8=LOW
potValue = analogRead(potPin);
motorValue = map(potValue, 0, 1023, 0, 255);
analogWrite(motorPin, motorValue);
Serial.print("potentiometer = " );
Serial.print(potValue);
Serial.print("\t motor = ");
Serial.println(motorValue);
delay(2);
} //---------------------------------------------------------------------------- end of Void Loop
int CCW_Led_Indicator = 10;
int CW_Led_Indicator = 11;
int relay = 12;
// this is where the program retrieves the saved variables...
EEPROM.get( CCW_LED_INDICATOR_ADDR, CCW_Led_Indicator );
EEPROM.get( CW_LED_INDICATOR_ADDR, CW_Led_Indicator );
EEPROM.get( RELAY_ADDR, relay);
if (digitalRead(relay) == HIGH)
{
digitalWrite(relay, LOW); // pin 12
digitalWrite(CCW_Led_Indicator, LOW); // Pin 11 of Mega 2560
digitalWrite(CW_Led_Indicator, HIGH); // Pin 10 of Mega 2560
EEPROM.put( RELAY_ADDR, relay );
EEPROM.put( CCW_LED_INDICATOR_ADDR, CCW_Led_Indicator );
EEPROM.put( CW_LED_INDICATOR_ADDR, CW_Led_Indicator );
}// these 3 lines of code above should save the variables
else
{
digitalWrite(relay, HIGH);
digitalWrite(CCW_Led_Indicator, HIGH); // Pin 11 of Mega 2560
digitalWrite(CW_Led_Indicator, LOW); // Pin 10 of Mega 2560
EEPROM.put( RELAY_ADDR, relay );
EEPROM.put( CCW_LED_INDICATOR_ADDR, CCW_Led_Indicator );
EEPROM.put( CW_LED_INDICATOR_ADDR, CW_Led_Indicator );
}// these 3 lines of code above should save the variables
You are saving and retrieving pin numbers and not pin states.
cattledog:
You are saving and retrieving pin numbers and not pin states.
Thanks for looking at my code and pointing that out. How would I correct the code to get it functioning the way I need? Do I change the way I declare the variables that I want to save the pin states for?
I did a lot of searching last night and didn't find anything similar that others have done to use as an example.
I'm a retired senior that likes to dabble in electronics and I do a lot of cutting and pasting to complete a project and that's what I like about Arduino is that a lot of people post their projects but I'm kinda stuck on this one.
Any help with this would be very much appreciated.
jessey:
Thanks for looking at my code and pointing that out. How would I correct the code to get it functioning the way I need? Do I change the way I declare the variables that I want to save the pin states for?
I did a lot of searching last night and didn't find anything similar that others have done to use as an example.
I'm a retired senior that likes to dabble in electronics and I do a lot of cutting and pasting to complete a project and that's what I like about Arduino is that a lot of people post their projects but I'm kinda stuck on this one.
Any help with this would be very much appreciated.
Thanks
jessey
Well, let me ask you this question. There are thousands of sketches out there that retrieve a pin state. Also many in the IDE examples. How do they do all do it? Go look.
Thanks for looking at my code and pointing that out. How would I correct the code to get it functioning the way I need? Do I change the way I declare the variables that I want to save the pin states for?
I think for the storing of the values, when power is on and pins are set, you can just use
if (digitalRead(relay) == HIGH)
{
digitalWrite(relay, LOW); // pin 12
digitalWrite(CCW_Led_Indicator, LOW); // Pin 11 of Mega 2560
digitalWrite(CW_Led_Indicator, HIGH); // Pin 10 of Mega 2560
EEPROM.put( RELAY_ADDR, digitalRead(relay) );
EEPROM.put( CCW_LED_INDICATOR_ADDR, digitalRead(CCW_Led_Indicator) );
EEPROM.put( CW_LED_INDICATOR_ADDR, digitalRead(CW_Led_Indicator) );
}// these 3 lines of code above should save the variables
else
{
digitalWrite(relay, HIGH);
digitalWrite(CCW_Led_Indicator, HIGH); // Pin 11 of Mega 2560
digitalWrite(CW_Led_Indicator, LOW); // Pin 10 of Mega 2560
EEPROM.put( RELAY_ADDR, digitalRead(relay) );
EEPROM.put( CCW_LED_INDICATOR_ADDR, digitalRead(CCW_Led_Indicator) );
EEPROM.put( CW_LED_INDICATOR_ADDR, digitalRead(CW_Led_Indicator) );
}// these 3 lines of code above should save the variables
I think that when you read the values at setup you could do this to read and set the values
aarg:
Well, let me ask you this question. There are thousands of sketches out there that retrieve a pin state. Also many in the IDE examples. How do they do all do it? Go look.
aarg,
In answer to your question I did look, all night in fact. I looked at so many my head was swimming. All of the ones I looked at shared no resemblance to the way I was saving pin numbers. Most were saving in for loops of which I have no concept of. I also looked in the IDE and there too - there were no clear examples there that worked for me either. So that's why I asked the question here.
cattledog:
I think for the storing of the values, when power is on and pins are set, you can just use
if (digitalRead(relay) == HIGH)
{
digitalWrite(relay, LOW); // pin 12
digitalWrite(CCW_Led_Indicator, LOW); // Pin 11 of Mega 2560
digitalWrite(CW_Led_Indicator, HIGH); // Pin 10 of Mega 2560
EEPROM.put( RELAY_ADDR, digitalRead(relay) );
EEPROM.put( CCW_LED_INDICATOR_ADDR, digitalRead(CCW_Led_Indicator) );
EEPROM.put( CW_LED_INDICATOR_ADDR, digitalRead(CW_Led_Indicator) );
}// these 3 lines of code above should save the variables
else
{
digitalWrite(relay, HIGH);
digitalWrite(CCW_Led_Indicator, HIGH); // Pin 11 of Mega 2560
digitalWrite(CW_Led_Indicator, LOW); // Pin 10 of Mega 2560
EEPROM.put( RELAY_ADDR, digitalRead(relay) );
EEPROM.put( CCW_LED_INDICATOR_ADDR, digitalRead(CCW_Led_Indicator) );
EEPROM.put( CW_LED_INDICATOR_ADDR, digitalRead(CW_Led_Indicator) );
}// these 3 lines of code above should save the variables
I think that when you read the values at setup you could do this to read and set the values
Your problem was only really in learning to combine separate tasks. You have to:
1 - read a pin
2 - save the state
reply #4 combines the two in one statement but you don't have to do it that way.
Examples do abound to do 1 and 2, and they are also very well documented. Very often a digitalRead() is saved in a variable. Very often a variable is written to EEPROM. Do you see what I'm getting at? As a programmer, you have to learn to connect the dots. Doing this in stages would look like this:
You really should consider saving data to EEPROM only when it changes. Saving every time through loop() if the relay (pin) is HIGH will shorten the life of you (non-replacable) EEPROM. Considerably.
@jessy Please do not start begin working with this until you address the issue raised by PaulS in reply #8. I did not think this through before I posted. Sorry
You can definitely wear out the eerpom writing to it every pass of the loop. In fact, when you were developing the program and were writing a pin number to the eeprom address you may have done that already.
You may need to move the addresses from 0-2-4 to new ones.
Only save the data when it changes, and if you really are doing very frequent writes, you may need to move the locations around in the eeprom to do "wear leveling".
You may be better off following the method suggested by aarg and read the value into a state. Test if the state is different than the previous state, and only store if there has been a change. Look at the state change detection example in the digital examples section of the IDE if you don't know what to do.
cattledog:
You can definitely wear out the eerpom writing to it every pass of the loop.
Hi cattledog,
Just a quick note: The way I have my push button code set up in the if statement, the eeprom writing can only occur once after the pushbutton is released and not every pass through the Void Loop? I don't understand your concern for that?
Just a quick note: The way I have my push button code set up in the if statement, the eeprom writing can only occur once after the pushbutton is released and not every pass through the Void Loop? I don't understand your concern for that?
I did not study the code logic very closely, and don't understand how things are wired. I was thinking that the relay was on for multiple passes through the loop. Sorry for the confusion.
Delta_G:
You seriously looked all night and didn't see one example where someone got the state of a pin using digitalRead.
Hello Delta_G,
Yes I did spend my whole night looking. I’m sorry I don’t measure up to your standard of comprehension when it comes to being able to decipher other peoples code but that's just the way it is but I at least spend the time and try.. And yes I did find some examples and one threads title was: “how do i read/write output pin states to eeprom?” (Have a look at that thread and explain to me how easy it is if you got the time.) how do i read/write output pin states to eeprom? - Storage - Arduino Forum but like the other code snips I found it was also too complicated and I couldn’t decipher it, it was just way over my head as to what was going on but I am still enjoying my hobby and not ready to throw in the towel just yet.
I tried the code that cattledog posted for me to save the pin states but I got errors when I tried to compile it so I came up with another way to load the pin states by saving one variable and using it in the Void setup() to load the pin state values of my leds and relay before dropping down into the Void loop() and it seems to be working good. I posted my code and schematics if anyone wants to have a look.
Thanks
jessey
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//This is the URL where I got the basic code for this project: http://www.dummies.com/how-to/content/how-to-control-the-speed-of-a-dc-motor-with-the-ar.html
//Topic: How to turn off PWM: URL-----> http://forum.arduino.cc/index.php?topic=381881.0
//Topic: Arduino PWM Motor Speed Controller Questions: URL-----> http://forum.arduino.cc/index.php?topic=378883.0
//Topic: mega 2560 PWM frequency: URL-----> http://forum.arduino.cc/index.php?topic=72092.msg539486#msg539486
//Topic: Saving to EEPROM?: URL-----> http://forum.arduino.cc/index.php?topic=384724.0
#include <EEPROM.h>
int potPin = A0;
int motorPin = 9;
int potValue = 0;
int motorValue = 0;
int CCW_Led = 10;
int CW_Led = 11;
int relay = 12;
int a;//this var gives up the pin state values of the leds & relay
#define A_ADDR 0
void setup()
{
pinMode(8, INPUT); //pushbutton pin 8
pinMode(CCW_Led, OUTPUT); //CCW_Led on pin 10
pinMode(CW_Led, OUTPUT); //CW_Led on pin 11
pinMode(relay, OUTPUT); // pin 12
EEPROM.get( A_ADDR, a);
if (a == 0)
{digitalWrite(CCW_Led, LOW); digitalWrite(CW_Led, HIGH); digitalWrite(relay, LOW);}
else
{digitalWrite(CCW_Led, HIGH); digitalWrite(CW_Led, LOW); digitalWrite(relay, HIGH);}
}
void loop()
{
//---------------------------------------
int myEraser = 7;// --- these 4 lines of code change
TCCR2B &= ~myEraser;// --- the PWM frequency from the
int myPrescaler = 1;// 1 = 31000 Hz --- default 490 Hz to 31000 Hz
TCCR2B |= myPrescaler;// --- to get rid of motor humming
//---------------------------------------
if (digitalRead(8) == LOW)
{
if (digitalRead(relay) == HIGH)
{digitalWrite(relay, LOW); a = 0; digitalWrite(CCW_Led, LOW);
digitalWrite(CW_Led, HIGH); EEPROM.put(A_ADDR, a);}
else
{digitalWrite(relay, HIGH);a = 1;digitalWrite(CCW_Led, HIGH);
digitalWrite(CW_Led, LOW); EEPROM.put(A_ADDR, a);}
while(digitalRead(8) == LOW) // program remains here until pushbutton is released
{
analogWrite(motorPin, 0);delay(100); // shut off the motor before changing the rotation
}
delay(300);
}
potValue = analogRead(potPin);
motorValue = map(potValue, 0, 1023, 0, 255);
analogWrite(motorPin, motorValue);
delay(2);
} //--------------------------------------------------------------------- end of Void Loop