Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Code for the Plant Watering project found here: https://www.manylabs.org/docs/project/plantWatering/
- // Copyright ManyLabs 2013
- // MIT license
- // devices on analog pins
- #define MOISTURE_PIN 0
- #define WATER_PIN 2
- // devices on digital pins
- #define BUTTON_PIN 2
- #define RELAY_PIN 4
- #define BUZZER_PIN 6
- // turn on pump if moisture sensor value is below this number (i.e. the soil is too dry)
- #define MOISTURE_THRESHOLD 100
- // turn on buzzer if water sensor value is below this number (i.e. the water level is too low)
- #define WATER_THRESHOLD 100
- // this sets the number of seconds between reads of the moisture/water sensors
- // longer delays will help prevent corrosion, but will cause the pump to run for longer
- // check the project documentation for more information
- #define SENSOR_UPDATE_DELAY_SECONDS 10
- // this counter is used so that we reduce the frequency of reading the moisture/water
- // sensors (in order to reduce corrosion)
- int sensorCounter = 0;
- // most recent sensor values
- int moisture = 0;
- int water = 0;
- // run once on startup
- void setup() {
- // set up input/output pins
- pinMode( BUTTON_PIN, INPUT );
- pinMode( RELAY_PIN, OUTPUT );
- pinMode( BUZZER_PIN, OUTPUT );
- pinMode( A1 + MOISTURE_PIN, OUTPUT );
- pinMode( A1 + WATER_PIN, OUTPUT );
- // prepare serial connection back to computer
- Serial.begin( 9600 );
- }
- // keep running forever (until power goes out)
- void loop() {
- // read sensors
- int button = digitalRead( BUTTON_PIN );
- if (sensorCounter == 0 || button) {
- moisture = readSensor( MOISTURE_PIN );
- water = readSensor( WATER_PIN );
- }
- // if water level is too low, turn on buzzer
- if (water < WATER_THRESHOLD) {
- digitalWrite( BUZZER_PIN, HIGH );
- } else {
- digitalWrite( BUZZER_PIN, LOW );
- }
- // if there is enough water and moisture level is too low
- // or button is pressed, turn on pump
- if (water > WATER_THRESHOLD && moisture < MOISTURE_THRESHOLD) {
- digitalWrite( RELAY_PIN, HIGH );
- } else {
- digitalWrite( RELAY_PIN, LOW );
- }
- // display current sensor values
- Serial.print( "moisture: " );
- Serial.print( moisture, DEC );
- Serial.print( " water: " );
- Serial.print( water, DEC );
- Serial.println();
- // update counter
- sensorCounter++;
- if (sensorCounter == SENSOR_UPDATE_DELAY_SECONDS)
- sensorCounter = 0;
- // sleep for one second
- delay( 1000 );
- }
- // read the value of a soil moisture sensor
- int readSensor( int analogPin ) {
- digitalWrite( A1 + analogPin, HIGH );
- delay( 1 ); // 1 millisecond
- int value = analogRead( analogPin );
- digitalWrite( A1 + analogPin, LOW );
- return value;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement