Advertisement
AlexShu

ATTINY13 Ground Moisture Meter

Aug 15th, 2021
1,567
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.48 KB | None | 0 0
  1. // Alexshu.com
  2. // ATTINY13 Ground Moisture Meter
  3.  
  4. #include <EEPROM.h>#include <avr/sleep.h> //Needed for sleep_mode
  5. #include <avr/wdt.h> //Needed to enable/disable watch dog timer
  6.  
  7.  
  8. int lowerSoilValue = 0;
  9. int voltage = 0;
  10.  
  11.  
  12. void setup()
  13. {
  14.     // Setup Pins
  15.     pinMode(0, OUTPUT);
  16.     pinMode(3, OUTPUT);
  17.     pinMode(1, INPUT);
  18.    
  19.     // Get Value from EEPROM
  20.     lowerSoilValue = EEPROM.read(0) * 4 + EEPROM.read(1);
  21.  
  22.  
  23.     // Sleep Setup
  24.     set_sleep_mode(SLEEP_MODE_PWR_DOWN); //Power down everything, wake up from WDT
  25.     sleep_enable();
  26. }
  27.  
  28.  
  29. void loop()
  30. {  
  31.      wdt_disable(); //Turn off the WDT!
  32.      ADCSRA |= (1<<ADEN); //Enable ADC
  33.      
  34.      // Battery Voltage Read
  35.      // Switch Analog Reference to 1.1V Internal , take 5 readings that may be innacurate due to switching
  36.     analogReference(INTERNAL);
  37.     for(int i = 0; i<5; i++){ analogRead(A1); }
  38.     voltage = analogRead(A1) * 3; // Final Reading
  39.  
  40.     // Switch back to default and take 5 readings that may be innacurate due to switching
  41.     analogReference(DEFAULT);
  42.     for(int i = 0; i<5; i++){ analogRead(A2); }
  43.  
  44.  
  45.     // Programm Mode
  46.     if (digitalRead(1) == HIGH){
  47.     while(digitalRead(1) == HIGH){   // Blink until button released
  48.         PORTB |= _BV(PB0);
  49.         delay(50);
  50.         PORTB &= ~_BV(PB0);
  51.        delay(50);
  52.       }
  53.       while(digitalRead(1) == LOW){     // Continue blinking until button pressed again to programm
  54.         PORTB |= _BV(PB0);
  55.         delay(50);
  56.         PORTB &= ~_BV(PB0);
  57.        delay(50);
  58.       }
  59.       // Set pin 3 HIGH and take a double reading for accuracy, then set pin 3 LOW
  60.       PORTB |= _BV(PB3);
  61.       lowerSoilValue = analogRead(A2);
  62.       lowerSoilValue = analogRead(A2);
  63.       PORTB &= ~_BV(PB3);
  64.       // Write values to EEPROM
  65.       EEPROM.write(0, lowerSoilValue / 4);
  66.       EEPROM.write(1, lowerSoilValue % 4);
  67.     }  // END of programm mode
  68.  
  69.  
  70.       // Set pin 3 HIGH and take a double reading for accuracy
  71.       PORTB |= _BV(PB3);
  72.       if (analogRead(A2) < lowerSoilValue){  // if reading is lower than progremmed value
  73.         // Blink once
  74.         PORTB |= _BV(PB0);
  75.         delay(50);
  76.         PORTB &= ~_BV(PB0);  
  77.        
  78.         // if voltage is low blink second time
  79.         if(voltage < 2000){
  80.             delay(50);
  81.             PORTB |= _BV(PB0);
  82.             delay(50);
  83.             PORTB &= ~_BV(PB0);
  84.           }
  85.         }
  86.       // Set pin 3 LOW  
  87.       PORTB &= ~_BV(PB3);  
  88.      
  89.       //Disable ADC, saves ~230uA
  90.       ADCSRA &= ~(1<<ADEN);
  91.      
  92.       // Sleep
  93.       setup_watchdog(9); //Setup watchdog to go off after 8sec
  94.       sleep_mode(); //Go to sleep!
  95. }
  96.  
  97.  
  98. //Sets the watchdog timer to wake us up, but not reset
  99. //0=16ms, 1=32ms, 2=64ms, 3=128ms, 4=250ms, 5=500ms
  100. //6=1sec, 7=2sec, 8=4sec, 9=8sec
  101. void setup_watchdog(int timerPrescaler) {
  102.   if (timerPrescaler > 9 ) timerPrescaler = 9; //Limit incoming amount to legal settings
  103.  
  104.  
  105.   byte bb = timerPrescaler & 7;
  106.   if (timerPrescaler > 7) bb |= (1<<5); //Set the special 5th bit if necessary
  107.  
  108.  
  109.   //This order of commands is important and cannot be combined
  110.   MCUSR &= ~(1<<WDRF); //Clear the watch dog reset
  111.   WDTCR |= (1<<WDCE) | (1<<WDE); //Set WD_change enable, set WD enable
  112.   WDTCR = bb; //Set new watchdog timeout value
  113.   WDTCR |= _BV(WDTIE); //Set the interrupt enable, this will keep unit from resetting after each int
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement