/** * Dripper * by Andres Colubri. December 2007 * * Controls a solenoid valve through a micro relay connected * to the selected output pin. The valve is kept open openTime * microseconds, and the interval of operation is given by * waitTime. Two potentiometers can be attached to control * these two variables. * */ /* License (based on zlib/libpng): Copyright (c) 2007 Andres Colubri. This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely. */ boolean DEBUG = false; boolean READ_OPEN_TIME = false; boolean READ_WAIT_TIME = false; // Inut pin to read open time. int inputPin0 = 0; // Inut pin to read wait time. int inputPin1 = 1; // Pin to which the relay is connected to. int outputPin = 13; unsigned long initTime = 60000; // 1 minute: 60 * 1000 //unsigned long initTime = 10000; // 10 seconds int openTimeMin = 0; int openTimeMax = 250; int openTime = 50; unsigned long waitTimeMin = 0; unsigned long waitTimeMax = 1200000; // 20 minutes: 20 * 60 * 1000 unsigned long waitTime = 120000; // 2 minutes: 2 * 60 * 1000 //unsigned long waitTime = 10000; // 10 seconds int readVal0 = 0; int readVal1 = 0; float convFactor; float inv1023 = 0.0009765625; boolean initPause; unsigned long lastDropTime = 0; void setup() { Serial.begin(9600); pinMode(outputPin, OUTPUT); if (DEBUG) Serial.print("Initializing..."); initPause = true; } void loop() { if (READ_OPEN_TIME) { readOpenTime(); calculateOpenTime(); } if (READ_WAIT_TIME) { readWaitTime(); calculateWaitTime(); } if (initPause && (initTime < millis())) { initPause = false; if (DEBUG) Serial.println("DONE."); } if (!initPause) { if (waitTime < millis() - lastDropTime) { makeDrop(); } delay(1000); Serial.print(0); } } void readOpenTime() { readVal0 = analogRead(inputPin0); if (DEBUG) { Serial.print("input0: "); Serial.println(readVal0); } } void calculateOpenTime() { convFactor = inv1023 * readVal0; openTime = convFactor * (openTimeMax - openTimeMin) + openTimeMin; if (DEBUG) { Serial.print("open time: "); Serial.println(openTime); } } void readWaitTime() { readVal1 = analogRead(inputPin1); if (DEBUG) { Serial.print("input1: "); Serial.println(readVal1); } } void calculateWaitTime() { convFactor = inv1023 * readVal1; waitTime = convFactor * (waitTimeMax - waitTimeMin) + waitTimeMin; if (DEBUG) { Serial.print("wait time: "); Serial.println(waitTime); } } void makeDrop() { digitalWrite(outputPin, HIGH); delay(openTime); digitalWrite(outputPin, LOW); Serial.print(1); lastDropTime = millis(); }