Hey Guys, I'm new to programming but I have been messing about with my arduino uno for a day or two now and i made a sketch as a practice project.
I was using the EEPROM.h Library because I need to store data on my arduino for a car project, but the data has to remain even if the ignition is cycled.
Anyway its pretty neat, i called it a tool but it just basically lets you run the EEPROM. functions from the serial port and prints the stuff on your screen. IDK if anyone could use this to their benefit, also i welcome the criticism. If there is room for improvement (as I'm sure there is) please highlight it.
Sorry about not commenting it. I've made it a habit to comment stuff I'm gonna come back to latter but i threw this together just to get my head around the EEPROM library.
Oh also you it displays the values in dec but the table axis are hex cause its the most logical way to organize it.
Good to use const and the word pin, a better type would be byte. Also specifically for the LED (on pin 13 usually) there is a default name for it LED_BUILTIN. Cf this explanation
Serial.begin(9600);
Both your arduino and computer are way faster than this, you can use 115200 baud there (or more)
And all the others similar lines. You are using tons of your very limited SRAM (2k on a UNO) for each character within the quotes. Read about PROGMEM and the description of the various types of memory available on an Arduino. Then You might want to use this form:
This is a personal preference but there is no need to do {}. If there is no expression to execute a semicolon is enough
while(Serial.available()==0) [color=red];[/color] // wait for input
Your input parsing is approximative, while working with short selection and quick entry through the console because parseInt() implements a timeout. A better parsing scheme in general would be needed where you wait until the carriage return for example to build the command (CF Robin's post on Serial Input Basics)
EEPROM.write(...)
Given your EEPROM has a limited lifetime (100,000 writes), instead of calling write() you might want to prefer the update() method. It's a bit slower as it reads first the value and won't write if the same value needs to be stored, but you'll get an expanded lifetime especially if you often write the same values.
Awesome Thank you very much. Ill Make these changes, I had been playing with the baud rate. No Errors at 250000. I was also wondering about the EEPROM only being writable a few times, this information was very useful to me. Ill make the changes now.
J-M-L:
Thanks for sharing!
Some constructive comments
const int ledPin = 13;
Good to use const and the word pin, a better type would be byte. Also specifically for the LED (on pin 13 usually) there is a default name for it LED_BUILTIN. Cf this explanation
Serial.begin(9600);
Both your arduino and computer are way faster than this, you can use 115200 baud there (or more)
And all the others similar lines. You are using tons of your very limited SRAM (2k on a UNO) for each character within the quotes. Read about PROGMEM and the description of the various types of memory available on an Arduino. Then You might want to use this form:
This is a personal preference but there is no need to do {}. If there is no expression to execute a semicolon is enough
while(Serial.available()==0) [color=red];[/color] // wait for input
Your input parsing is approximative, while working with short selection and quick entry through the console because parseInt() implements a timeout. A better parsing scheme in general would be needed where you wait until the carriage return for example to build the command (CF Robin's post on Serial Input Basics)
EEPROM.write(...)
Given your EEPROM has a limited lifetime (100,000 writes), instead of calling write() you might want to prefer the update() method. It's a bit slower as it reads first the value and won't write if the same value needs to be stored, but you'll get an expanded lifetime especially if you often write the same values.
It does not wait for the user input and uses the address 0 but it waits for the value input and overwrite the address 0 ............ !?
that's because it's a poor way to handle Serial input... it's all documented above... have you read the thread?
There is a time out associated to Serial.parseInt(); which is 1 second by default.
So unless you type in very quickly the address (or on the same line "4<space>address<CR><LF>") you'll miss that data
Jean24816:
Hummmmmmmmmmm, should I put a delay() in there ?
what do you want us to say? a delay where and to do what? usually when dealing with asynchronous protocols, it's not a great idea to try to second guess when data will be ready by adding fixed delays.... just read stuff when they come in and structure your code accordingly (usually a state machine could help)
There are (in the above quote) two statements; however, how do I defend if a new learner says that there is only one statement looking at the comma ( ; ) which is usually a statement terminating character? Therefore, should not the quoted expression be written as follows from literate programming point of view?
A semicolon alone is a legit empty expression statement. As is {}
Wether you place it on the same line or on a line by itself or multiple lines is personal indenting preference