Last active
May 28, 2020 16:42
-
-
Save polluxlabs/fff31af56744635c81e0afc3daa5cd22 to your computer and use it in GitHub Desktop.
Kompakter Pflanzenwächter mit ATtiny85 und Batterie
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Code for the ATtiny85 | |
https://polluxlabs.net | |
Copyright: CC BY-NC-ND | |
*/ | |
#include <avr/sleep.h> | |
#include <avr/wdt.h> | |
#define calibrationPin 1 | |
#define sensorPin 2 | |
#define sensorValuePin 3 | |
#define ledPin 4 | |
void myWatchdogEnable(const byte interval) | |
{ | |
MCUSR = 0; // reset various flags | |
WDTCR |= 0b00011000; // see docs, set WDCE, WDE | |
WDTCR = 0b01000000 | interval; // set WDIE, and appropriate delay | |
wdt_reset(); | |
set_sleep_mode (SLEEP_MODE_PWR_DOWN); | |
sleep_mode(); // now goes to Sleep and waits for the interrupt | |
} | |
void setup() { | |
pinMode(calibrationPin, INPUT); | |
pinMode(sensorPin, OUTPUT); | |
pinMode(sensorValuePin, INPUT); | |
pinMode(ledPin, OUTPUT); | |
} | |
void loop() { | |
//wenn am calibrationPin Strom anliegt, kann der Sensor kalibriert werden | |
if (digitalRead(calibrationPin) == HIGH) { | |
digitalWrite(sensorPin, HIGH); | |
if (digitalRead(sensorValuePin) == 1) { // 1 = trocken | |
digitalWrite(ledPin, HIGH); | |
} else { | |
digitalWrite(ledPin, LOW); | |
} | |
//wenn kein Strom anliegt, geht er in den Messmodus | |
//Code, wenn die Erde zu trocken ist: | |
} else { | |
digitalWrite(sensorPin, HIGH); | |
delay(100); | |
if (digitalRead(sensorValuePin) == 1) { | |
digitalWrite(sensorPin, LOW); | |
for (byte i = 0; i < 10; i++) { | |
digitalWrite(ledPin, HIGH); | |
delay(200); | |
digitalWrite(ledPin, LOW); | |
delay(200); | |
} | |
myWatchdogEnable (0b100001); | |
} | |
//Code, wenn die Erde noch feucht genug ist: | |
else { | |
digitalWrite(ledPin, LOW); | |
digitalWrite(sensorPin, LOW); | |
for (byte j = 0; j <= 225; j++) { | |
myWatchdogEnable (0b100001); // 8 seconds -- verlängern auf viel Zeit | |
} | |
} | |
} | |
} | |
ISR(WDT_vect) | |
{ | |
wdt_disable(); // disable watchdog | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment