prjbrook

Best spud soil temp reader so far

Aug 13th, 2019
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. #include <avr/sleep.h>
  2. #include <avr/power.h>
  3. #include <EEPROM.h>
  4. #include <avr/wdt.h>
  5. #define LED_PIN (13)
  6. #define ENOUGH_READINGS 4 //how many readings til its time to write to eeprom
  7. #define E ENOUGH_READINGS
  8.  
  9. int value =22;
  10. int value2=33;
  11. int wdtCtr,wdCtrLimit=0;
  12. int toEepromArCtr=0;
  13. int sensorPin = 0; //the analog pin the TMP36's Vout (sense
  14. int readingsAr[256];
  15. int toEepromAr[256]; //these values go into eeprom
  16.  
  17. void setup() {
  18. Serial.begin(115200);
  19. Serial.println("Initialising...");
  20.  
  21.  
  22. delay(100); //Allow for serial print to complete.
  23.  
  24. pinMode(LED_PIN,OUTPUT);
  25. MCUSR &= ~(1<<WDRF);
  26.  
  27. /* In order to change WDE or the prescaler, we need to
  28. * set WDCE (This will allow updates for 4 clock cycles).
  29. */
  30. WDTCSR |= (1<<WDCE) | (1<<WDE);
  31.  
  32. /* set new watchdog timeout prescaler value */
  33. // WDTCSR = 1<<WDP0 | 1<<WDP3; /* 8.0 seconds */
  34. WDTCSR = 1<<WDP3; /* 4.0 seconds */
  35. /* Enable the WD interrupt (note no reset). */
  36. WDTCSR |= _BV(WDIE);
  37.  
  38. Serial.println("Initialisation complete.");
  39. delay(100); //Allow for serial print to complete
  40. }
  41.  
  42. void loop() {
  43. delay(550);
  44. // digitalWrite(LED_PIN, !digitalRead(LED_PIN));
  45. digitalWrite(LED_PIN,HIGH); //read some sensor
  46. //delay(500);
  47. int reading = analogRead(sensorPin); //do some sensor work
  48. // Serial.print("Reading>>");
  49. Serial.println(reading);
  50. readingsAr[wdtCtr]=reading;
  51. Serial.println(wdtCtr);
  52. if(wdtCtr>=ENOUGH_READINGS){
  53. Serial.println("Enough");
  54. printReadingsAr();
  55. Serial.println(meanReading());
  56. //toEepromArCtr++; //counts bytes going into eeprom
  57. EEPROM.write(toEepromArCtr++, meanReading());
  58. showEeprom();
  59. wdtCtr = 0;
  60. }
  61. // digitalWrite(LED_PIN,LOW);
  62. //Serial.print(value++);
  63. delay(3); //take out later
  64. enterSleep();
  65. //Serial.println(value2);
  66. }
  67. ISR(WDT_vect) {
  68. value2++;
  69. wdtCtr++;
  70. Serial.println("W");
  71. }
  72. void enterSleep(void)
  73. {
  74. set_sleep_mode(SLEEP_MODE_PWR_SAVE); /* EDIT: could also use SLEEP_MODE_PWR_DOWN for lowest power consumption. */
  75. sleep_enable();
  76.  
  77. /* Now enter sleep mode. */
  78. digitalWrite(LED_PIN, !digitalRead(LED_PIN));//now sleeping (nearly)
  79. sleep_mode();
  80.  
  81. /* The program will continue from here after the WDT timeout*/
  82. sleep_disable(); /* First thing to do is disable sleep. */
  83.  
  84. /* Re-enable the peripherals. */
  85. power_all_enable();
  86. }
  87. void printReadingsAr(void) {
  88. for (int i =0;i<=ENOUGH_READINGS;i++) {
  89. Serial.print(readingsAr[i]);
  90. Serial.println("*");
  91. }
  92. }
  93. int meanReading (void){
  94. int total=0;
  95. for(int i =0;i<E;i++){
  96. total = total + readingsAr[i];
  97. }
  98. return total/E;
  99. }
  100. void showEeprom(void) {
  101. int value;
  102. for (int i =0;i<10;i++) {
  103. value = EEPROM.read(i);
  104. Serial.print(value);
  105. Serial.println("e");
  106. }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment