Advertisement
naivxnaivet

EEPROM - SIMPLE READ / WRITE

Nov 15th, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. /*
  2. E E P R O M
  3.  
  4. A R D U I N O
  5.  
  6. */
  7.  
  8.  
  9.  
  10. #include <EEPROM.h>
  11. #define ep_limit 30 // change mo to para mataasan limit. 5 = (5 x 255) = 1275 (vouchers)
  12. int epDigits = 0;
  13. int epAddress = 10;
  14.  
  15.  
  16. void setup() {
  17. // put your setup code here, to run once:
  18.  
  19. epDigits = ep_read(epAddress);
  20. Serial.begin(9600);
  21. }
  22.  
  23. void loop()
  24. {
  25. // put your main code here, to run repeatedly:
  26. epDigits++;
  27. ep_write(epAddress, epDigits);
  28. Serial.println(epDigits);
  29. delay(1000);
  30. }
  31.  
  32.  
  33. void ep_write(int add, const int val)
  34. {
  35. int v = val;
  36. unsigned int i;
  37. for (i = 0; i < ep_limit; i++) {
  38. if (255 < v) {
  39. v = v - 255;
  40. EEPROM.write(add, 255);
  41. } else {
  42. EEPROM.write(add, v);
  43. v = 0;
  44. }
  45. add++;
  46. }
  47. }
  48.  
  49.  
  50.  
  51. int ep_read(int add)
  52. {
  53. unsigned int v = 0;
  54. unsigned int i;
  55. for (i = 0; i < ep_limit; i++)
  56. {
  57. v = v + EEPROM.read(add);
  58. add++;
  59. }
  60. return v;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement