Hi all, I am very green to arduino and programming. So I'll try to be as detailed as possible. I have an arduino UNO and a sainsmart 8channel relay board in a water proof project box. Hanging out of the box is 8 110v pigtails corisponding to each relay. So essentially 8 arduino controlled outlets. I need to be able to control timing of on and off in seconds for each individual outlet. Actual hours are not important so won't be using a RTC. Also each outlet will be controlling a water pump that will be filling up a container when it gets to a certain point it will be triggered to turn off via a float switch that is naturally closed (on when not floating). So I need to be able to toggle modes. From timed on and off to on then shut off by float switch for each outlet. I'm using digitalpins 4-11 as my OUTPUTS then the 5v and GRN for the relay panel. Hardware works just need help with the float swMitch installation and the sketch itself. Thanks for any help you geniuses could give me.
With 8 things to keep track of, you're going to want to use the technique shown in the "Blink Without Delay" example. This type of code is called "non-blocking", which allows you to multi-task.
Killarado:
Hi all, I am very green to arduino and programming. So I'll try to be as detailed as possible. I have an arduino UNO and a sainsmart 8channel relay board in a water proof project box. Hanging out of the box is 8 110v pigtails corisponding to each relay. So essentially 8 arduino controlled outlets. I need to be able to control timing of on and off in seconds for each individual outlet. Actual hours are not important so won't be using a RTC. Also each outlet will be controlling a water pump that will be filling up a container when it gets to a certain point it will be triggered to turn off via a float switch that is naturally closed (on when not floating). So I need to be able to toggle modes. From timed on and off to on then shut off by float switch for each outlet. I'm using digitalpins 4-11 as my OUTPUTS then the 5v and GRN for the relay panel. Hardware works just need help with the float swMitch installation and the sketch itself. Thanks for any help you geniuses could give me.
OK a couple of things here
-
Need to doublecheck about the Sainsmart modules - i assume you are controlling pumps and valves - the pumps can generate a :LOT of noise and inrush current (and spike) when starting and stopping - you need to ensure the Relay board is optisolated from the Arduino inputs so that the collapsing fields do not damage your arduino
-
Are you only going to have a single float switch ? I take it this will be on the sump and will single when a drain and flood bed is ready to release ? If you have done aquaponics for any period of time you will know that things will fail (usually from build up of gunk etc), you should have two float switches on the sump and test for either of them.
-
All you need is to have 5v driving through the float switch and then measure that back in on an input pin on the arduino and check for state change on that pin.
You will definitely want to do this with delays in your code. The ultimate way as Tyler says is to use the concepts in BLink Without Delay examples - however this is hard for a newbie to understand so instead look at the SimpleTimer library that uses these concepts but wraps them up nice and easily for you to use and understand.
Craig
Your project lends itself to implementation by finite state machines.
I have written a library for that. It's available here: http://playground.arduino.cc/Code/SMlib
You start by drawing a state diagram decribing the working and interaction of the pumps and switches. Once the diagram is completet it's quite easy to tranform it into a working program. PM me if you want some more detailed help.
nilton61:
Your project lends itself to implementation by finite state machines.
I have written a library for that. It's available here: Arduino Playground - HomePageYou start by drawing a state diagram decribing the working and interaction of the pumps and switches. Once the diagram is completet it's quite easy to tranform it into a working program. PM me if you want some more detailed help.
I think we sometimes forget how hard all of this is when getting started - i definitely would not be recommended a State Machine to an absolute newbie to begin with - it is just too much for them to understand. (but i have checked out your stuff and it does look good !)
Craig
On the contrary. State machines are actually easier to both model and understand than linear programming. Especially for tasks like these. The problem is that most people do not either know about the state machinesor have preconceived opions about it.
What im talking about is using state machines primarily as a description and modelling method. I see too many times that linear programming leads to a alter and retry method of development that leads to erroneous and hard to maintain programs.
Let me give you an easy example related to this post. An Arduino automated toilet. First you analyse the model:
- 2 outputs: one fill valve and one flush valve
- 2 inputs: one for activating the flush and one for signalling a full tank
- 3 states: filling the tank(start state), waiting and flushing
This can very easily be translated into a state diagram (attached) and then into this code:
#include <SM.h>
SM Toilet(Filling);
const int Flush = 2;//pushbutton
const int TankFull = 3;//sensor
const int FillValve = 10;
const int FlushValve = 11;
void setup(){
pinMode(FillValve, OUTPUT);
pinMode(FlushValve, OUTPUT);
}//setup()
void loop(){
EXEC(Toilet);
}//loop()
State Filling(){
digitalWrite(FlushValve, 0);//Finished flushing
digitalWrite(FillValve, 1);//Start filling
if(digitalRead(TankFull)) Toilet.Set(Waiting);
}//Filling()
State Waiting(){
digitalWrite(FillValve, 0);//Tank is full, stop filling
if(digitalRead(Flush)) Toilet.Set(Flushing);//Flushbutton pressed, start flushing
}//Waiting()
State Flushing(){
digitalWrite(FlushValve, 1);//Start flushing
if(Toilet.Timeout(5000)) Toilet.Set(Filling);//Flush for 5 sek
}//Flushing()
Can it be much easier??
The beauty of this is that it is totally non blocking and easily entendable. You can control many Toilets with the same arduino and an extension of this code given enoigh I/O pins
nilton61:
On the contrary. State machines are actually easier to both model and understand than linear programming. Especially for tasks like these. The problem is that most people do not either know about the state machinesor have preconceived opions about it.
What im talking about is using state machines primarily as a description and modelling method. I see too many times that linear programming leads to a alter and retry method of development that leads to erroneous and hard to maintain programs.Let me give you an easy example related to this post. An Arduino automated toilet. First you analyse the model:
- 2 outputs: one fill valve and one flush valve
- 2 inputs: one for activating the flush and one for signalling a full tank
- 3 states: filling the tank(start state), waiting and flushing
This can very easily be translated into a state diagram (attached) and then into this code:
#include <SM.h>
SM Toilet(Filling);
const int Flush = 2;//pushbutton
const int TankFull = 3;//sensor
const int FillValve = 10;
const int FlushValve = 11;
void setup(){
pinMode(FillValve, OUTPUT);
pinMode(FlushValve, OUTPUT);
}//setup()
void loop(){
EXEC(Toilet);
}//loop()
State Filling(){
digitalWrite(FlushValve, 0);//Finished flushing
digitalWrite(FillValve, 1);//Start filling
if(digitalRead(TankFull)) Toilet.Set(Waiting);
}//Filling()
State Waiting(){
digitalWrite(FillValve, 0);//Tank is full, stop filling
if(digitalRead(Flush)) Toilet.Set(Flushing);//Flushbutton pressed, start flushing
}//Waiting()
State Flushing(){
digitalWrite(FlushValve, 1);//Start flushing
if(Toilet.Timeout(5000)) Toilet.Set(Filling);//Flush for 5 sek
}//Flushing()
Can it be much easier?? The beauty of this is that it is totally non blocking and easily entendable. You can control many Toilets with the same arduino and an extension of this code given enoigh I/O pins
I agree with you (i have however been involved in systems and programming all my working life) however i do not think a newbie can model something like this and then put it out into code - there is a reason that nearly all arduino intros begin with blinking lights etc and move on from there - it is the way the human mind is hardwired.
Having said all of that - wouldn't the initial state of your Toilet machine be more accurately set to waiting when you first initialise rather than an initial state of filling ?
Craig
Setting the initial state to waiting would imply the assumption that the tank already is filled at system start. If you instead set the initial state to filling you cover both situations. If the tank is not full it will be filled directly at system start. If the tank is full at system start the state will change to waiting immediately.
I think the reasom why state machines not are used to greater extend is a lack of knowledge in combination that very much of the litterature on state machines is very academic in nature. This is one of the reasons i wrote the library, to make state machines more accesible.
I have a background in industrial automation where the state machine is the dominant concept. And the very big advatage of state machines is that they force the programmer to model the problem before starting to code. And thats a very good thing. I raises the treshold somewhat, but it gives you a quicker, cleaner, better documented (especially if you include the state diagrams) and easier to debug solution.
But understand what you mean and i am seriously considering writing a book on usin arduino in automation projects.
You guys are awesome! Thanks so much for the help. I definitely have a lot of studying to do but I feel like I have a few great concepts to build on. I especially like the concept of state machine. Thanks
nilton61:
Setting the initial state to waiting would imply the assumption that the tank already is filled at system start. If you instead set the initial state to filling you cover both situations. If the tank is not full it will be filled directly at system start. If the tank is full at system start the state will change to waiting immediately.I think the reasom why state machines not are used to greater extend is a lack of knowledge in combination that very much of the litterature on state machines is very academic in nature. This is one of the reasons i wrote the library, to make state machines more accesible.
I have a background in industrial automation where the state machine is the dominant concept. And the very big advatage of state machines is that they force the programmer to model the problem before starting to code. And thats a very good thing. I raises the treshold somewhat, but it gives you a quicker, cleaner, better documented (especially if you include the state diagrams) and easier to debug solution.
But understand what you mean and i am seriously considering writing a book on usin arduino in automation projects.
Very Nice - i just went to the page you linked to and read it and must take my hat off to you. It is a very nice looking SM implementation
Craig
Thank You. And feel free to spread it