Last active
August 29, 2015 14:09
-
-
Save jamesmacfie/988b972f088712065641 to your computer and use it in GitHub Desktop.
A small snippet of code that turns on and off three solenoid valves via an Arduino. The test code turns on three valves for two seconds each.
This file contains hidden or 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
/* | |
* Test app that turns three solenoid valves on at set intervals and duration over a | |
* set time period | |
*/ | |
// The time to test the current time against | |
long startTime = 0; | |
// Poorly named variable - the time before the timer resets itself back to '0' | |
long dayLength = 6000; | |
// How long each valve turns on for | |
long duration = 2000; | |
// Info about each valve | |
int valve1= 10; | |
long valve1StartTime = 0; | |
boolean valve1On = false; | |
boolean valve1Finished = false; | |
int valve2= 11; | |
long valve2StartTime = 2000; | |
boolean valve2On = false; | |
boolean valve2Finished = false; | |
int valve3= 12; | |
long valve3StartTime = 4000; | |
boolean valve3On = false; | |
boolean valve3Finished = false; | |
void setup() { | |
Serial.begin(9600); | |
// Set all pins to output only | |
pinMode(valve1, OUTPUT); | |
pinMode(valve2, OUTPUT); | |
pinMode(valve3, OUTPUT); | |
// Initialise our starttime | |
startTime = millis(); | |
} | |
// FOREVER | |
void loop() { | |
// Where we at? | |
long currentTime = millis(); | |
Serial.print("Current time: "); | |
Serial.println(currentTime); | |
Serial.print("Start time: "); | |
Serial.println(startTime); | |
checkLed1(currentTime); | |
checkLed2(currentTime); | |
checkLed3(currentTime); | |
if (currentTime > (startTime + dayLength)) { | |
// Let's start the timer again | |
resetDay(); | |
} | |
delay(100); | |
} | |
// Below are the functions that test when we should turn a valve on or off. Yeah, | |
// I know, this can probably be simplified to a loop and a single function but fuck | |
// it. I don't enjoy this C stuff | |
void checkLed1(long currentTime) { | |
if (currentTime >= (startTime + valve1StartTime) && | |
currentTime <= (startTime + valve1StartTime + duration)) { | |
Serial.println("Turn on 1"); | |
digitalWrite(valve1, HIGH); | |
} | |
if (currentTime >= (startTime + valve1StartTime + duration)) { | |
Serial.println("Turn off 1"); | |
digitalWrite(valve1, LOW); | |
} | |
} | |
void checkLed2(long currentTime) { | |
if (currentTime >= (startTime + valve2StartTime) && | |
currentTime <= (startTime + valve2StartTime + duration)) { | |
Serial.println("Turn on 2"); | |
digitalWrite(valve2, HIGH); | |
} | |
if (currentTime >= (startTime + valve2StartTime + duration)) { | |
Serial.println("Turn off 2"); | |
digitalWrite(valve2, LOW); | |
} | |
} | |
void checkLed3(long currentTime) { | |
if (currentTime >= (startTime + valve3StartTime) && | |
currentTime <= (startTime + valve3StartTime + duration)) { | |
Serial.println("Turn on 3"); | |
digitalWrite(valve3, HIGH); | |
} | |
if (currentTime >= (startTime + valve3StartTime + duration)) { | |
Serial.println("Turn off 3"); | |
digitalWrite(valve3, LOW); | |
} | |
} | |
// Resets our timer | |
void resetDay() { | |
Serial.println("Resetting day"); | |
valve1On = false; | |
valve1Finished = false; | |
// Re-init the start time | |
startTime = millis(); | |
Serial.print("Start set to: "); | |
Serial.println(startTime); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Initially this was created so that I could have my three irrigation lines in my garden turn on/off at set times during the course of a day. Hence the variable names like
dayLength
etc