PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 satır
2.0KB

  1. /***
  2. EEPROM Update method
  3. Stores values read from analog input 0 into the EEPROM.
  4. These values will stay in the EEPROM when the board is
  5. turned off and may be retrieved later by another sketch.
  6. If a value has not changed in the EEPROM, it is not overwritten
  7. which would reduce the life span of the EEPROM unnecessarily.
  8. Released using MIT licence.
  9. ***/
  10. #include <EEPROM.h>
  11. /** the current address in the EEPROM (i.e. which byte we're going to write to next) **/
  12. unsigned int address = 0;
  13. void setup(){ /** EMpty setup **/ }
  14. void loop()
  15. {
  16. /***
  17. need to divide by 4 because analog inputs range from
  18. 0 to 1023 and each byte of the EEPROM can only hold a
  19. value from 0 to 255.
  20. ***/
  21. int val = analogRead(0) / 4;
  22. /***
  23. Update the particular EEPROM cell.
  24. these values will remain there when the board is
  25. turned off.
  26. ***/
  27. EEPROM.update(address, val);
  28. /***
  29. The function EEPROM.update(address, val) is equivalent to the following:
  30. if( EEPROM.read(address) != val ){
  31. EEPROM.write(address, val);
  32. }
  33. ***/
  34. /***
  35. Advance to the next address, when at the end restart at the beginning.
  36. Larger AVR processors have larger EEPROM sizes, E.g:
  37. - Arduno Duemilanove: 512b EEPROM storage.
  38. - Arduino Uno: 1kb EEPROM storage.
  39. - Arduino Mega: 4kb EEPROM storage.
  40. - Teensy 3.0 & 3.1: 2kb EEPROM storage.
  41. - Teensy-LC: 128b EEPROM storage.
  42. - Teensy 2.0: 1kb EEPROM storage.
  43. - Teensy++ 2.0: 4kb EEPROM storage.
  44. Rather than hard-coding the length, you should use the pre-provided length function.
  45. This will make your code portable to all AVR processors.
  46. ***/
  47. address = address + 1;
  48. if(address == EEPROM.length())
  49. address = 0;
  50. /***
  51. As the EEPROM sizes are powers of two, wrapping (preventing overflow) of an
  52. EEPROM address is also doable by a bitwise and of the length - 1.
  53. ++address &= EEPROM.length() - 1;
  54. ***/
  55. delay(100);
  56. }