I am having trouble with a code that I am writing for a mixer/auto watering system that I have designed. With the code uploaded to my Trinket 5V board and my prototype board all wired up, my code is not functioning properly.
My plan is fairly straight forward, the steps of the logic should go as followed:
1: Start button is pressed
2: Solenoid 1 opens
3: 2 second delay
4: Pump starts
5: Solenoid 1 and pump stay HIGH for 30 minutes
6: Solenoid 2 opens
7: Solenoid 1 closes
8: Solenoid 2 and pump stay HIGH until FLOAT switch is HIGH
9: Once FLOAT switch is HIGH, Solenoid 1(included for safety), Solenoid 2, and Pump all go LOW
This is my first project working with within the Arduino IDE environment as I myself am used to ladder logic and the PLC world. Any help or tips will be greatly appreciated
Here is the code that I have written for this project. I am not sure if I am taking the right approach at this.
Best Regards,
Kyle
void setup() {
// put your setup code here, to run once:
#define Start 0
#define Pump 1
#define Sol1 2
#define Sol2 3
#define Float 4
//Pin Declaration
pinMode(Start, INPUT);
pinMode(Pump, OUTPUT);
pinMode(Sol1, OUTPUT);
pinMode(Sol2, OUTPUT);
pinMode(Float, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
if (digitalRead(Start))
{ //if button is pressed
digitalWrite(Sol1, HIGH); //Activate Sol 1
delay(2000); //2 Second Delay Between Pump Activation
digitalWrite(Pump, HIGH); //Activate Pump
delay(10000); //30 Minute Delay To Mix (1800000) 10000 for testing
digitalWrite(Sol2, HIGH); //Activate Sol 2
digitalWrite(Sol1, LOW); //Deactivate Sol 1
}
else (digitalRead(Float)); { //if float is activated
digitalWrite(Pump, LOW); //Deactivate Pump
delay(500); //half second delay before valves close
digitalWrite(Sol2, LOW); //Deactivate Sol 2
digitalWrite(Sol1, LOW); //Deactivate Sol 1 SAFTEY
}
}
Well since you are dealing with buttons and sensors, it is usually better not to use delay( for timing but to use millis() for timing. But you may get away with using delay(), your plan is fairly simple.
We will need a schematic as well to check if you have made the connections properly.
this part should work as you intend.
this should for sure be different.
else (digitalRead(Float)); { //if float is activated
this does not do what you want. In fact the else statement is incorrect and there is no condition that is tested.
void loop() {
// put your main code here, to run repeatedly:
if (digitalRead(Start))
{ //if button is pressed
digitalWrite(Sol1, HIGH); //Activate Sol 1
delay(2000); //2 Second Delay Between Pump Activation
digitalWrite(Pump, HIGH); //Activate Pump
delay(10000); //30 Minute Delay To Mix (1800000) 10000 for testing
digitalWrite(Sol2, HIGH); //Activate Sol 2
digitalWrite(Sol1, LOW); //Deactivate Sol 1
while ( !digitalRead(Float) ) ; // wait until float is activated
digitalWrite(Pump, LOW); //Deactivate Pump
delay(500); //half second delay before valves close
digitalWrite(Sol2, LOW); //Deactivate Sol 2
digitalWrite(Sol1, LOW); //Deactivate Sol 1 SAFTEY
}
}
Should do what you want, but there is no way to interfere once the process has started.
Good start. If what you have done works as intended and you are happy with it then good. However if you want to learn more advanced ways of programming, ways that make dealing with more complex scenarios easy, then I suggest these 2 tutorials are a good place to start. Enjoy.