SaveRecallScratchPad.ino 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //
  2. // FILE: SaveRecallScratchPad.ino
  3. // AUTHOR: GitKomodo
  4. // VERSION: 0.0.1
  5. // PURPOSE: Show DallasTemperature lib functionality to
  6. // save/recall ScratchPad values to/from EEPROM
  7. //
  8. // HISTORY:
  9. // 0.0.1 = 2020-02-18 initial version
  10. //
  11. #include <OneWire.h>
  12. #include <DallasTemperature.h>
  13. #define ONE_WIRE_BUS 2
  14. OneWire oneWire(ONE_WIRE_BUS);
  15. DallasTemperature sensors(&oneWire);
  16. DeviceAddress deviceAddress;
  17. void setup()
  18. {
  19. Serial.begin(9600);
  20. Serial.println(__FILE__);
  21. Serial.println("Dallas Temperature Demo");
  22. sensors.begin();
  23. // Get ID of first sensor (at index 0)
  24. sensors.getAddress(deviceAddress,0);
  25. // By default configuration and alarm/userdata registers are also saved to EEPROM
  26. // when they're changed. Sensors recall these values automatically when powered up.
  27. // Turn OFF automatic saving of configuration and alarm/userdata registers to EEPROM
  28. sensors.setAutoSaveScratchPad(false);
  29. // Change configuration and alarm/userdata registers on the scratchpad
  30. int8_t resolution = 12;
  31. sensors.setResolution(deviceAddress,resolution);
  32. int16_t userdata = 24680;
  33. sensors.setUserData(deviceAddress,userdata);
  34. // Save configuration and alarm/userdata registers to EEPROM
  35. sensors.saveScratchPad(deviceAddress);
  36. // saveScratchPad can also be used without a parameter to save the configuration
  37. // and alarm/userdata registers of ALL connected sensors to EEPROM:
  38. //
  39. // sensors.saveScratchPad();
  40. //
  41. // Or the configuration and alarm/userdata registers of a sensor can be saved to
  42. // EEPROM by index:
  43. //
  44. // sensors.saveScratchPadByIndex(0);
  45. // Print current values on the scratchpad (resolution = 12, userdata = 24680)
  46. printValues();
  47. }
  48. void loop(){
  49. // Change configuration and alarm/userdata registers on the scratchpad
  50. int8_t resolution = 10;
  51. sensors.setResolution(deviceAddress,resolution);
  52. int16_t userdata = 12345;
  53. sensors.setUserData(deviceAddress,userdata);
  54. // Print current values on the scratchpad (resolution = 10, userdata = 12345)
  55. printValues();
  56. delay(2000);
  57. // Recall configuration and alarm/userdata registers from EEPROM
  58. sensors.recallScratchPad(deviceAddress);
  59. // recallScratchPad can also be used without a parameter to recall the configuration
  60. // and alarm/userdata registers of ALL connected sensors from EEPROM:
  61. //
  62. // sensors.recallScratchPad();
  63. //
  64. // Or the configuration and alarm/userdata registers of a sensor can be recalled
  65. // from EEPROM by index:
  66. //
  67. // sensors.recallScratchPadByIndex(0);
  68. // Print current values on the scratchpad (resolution = 12, userdata = 24680)
  69. printValues();
  70. delay(2000);
  71. }
  72. void printValues() {
  73. Serial.println();
  74. Serial.println("Current values on the scratchpad:");
  75. Serial.print("Resolution:\t");
  76. Serial.println(sensors.getResolution(deviceAddress));
  77. Serial.print("User data:\t");
  78. Serial.println(sensors.getUserData(deviceAddress));
  79. }