Go Down

Topic: Need some Help. (Read 636 times) previous topic - next topic

aaron0818

First of all I'm very new to all of this but so far I'm having a lot of fun.  Due to another hobby of mine I maintain an RODI unit for making clean water.  I am wanting to automate this process as much as possible.  The RODI unit has maintenance requirements that if done properly will make the unit and the filters last a lot longer.

I'm using an Arduino Nano for reference
I believe to have the hardware setup correctly only because its working as I wanted it to.  I plan on having 4 solenoid valves that will control the functions of the unit.  One to Supply water Two will be on the two different flush lines and another on the output line.
I have 3 buttons and a float switch.  The three buttons are used as follows.  1 starts the fill procedure 2 starts the flush procedure and 3 stops all processes.  There will be a float switch on a water tank that will register a Full tank signal to the arduino.

The program that I have works as I wanted it to however once its running a sub program as I call them it disregards any button or float switch input. 
I wanted the unit to operate as follows.
I press the start button
It flushes the filters for a set amount of time then starts outputting  water to the water tank.  I want the unit to stop when the float switch is made or I press the stop button.

Currently it will run the program but doesn't pay attention to the float switch or stop button until my delay are met.
I know the code can be cleaned up a lot probably but I'm still learning and very open to suggestion.  I used the debounce code and added to it as I don't want any false readings from button presses.  I have a feeling I need to use some sort of interrupt or not use the delay and use timers instead. 

I have attached my current code for anybody to look over and maybe suggest what it will take to make it work the way i need it to.
Thanks
Aaron
Code: [Select]
// constants
const int button1Pin = 10;     // Start Button
const int button2Pin = 9;      // Stop Button
const int button3Pin = 8;      // Flush Button
const int button4Pin = 2;      // ATO Top Float Switch
const int supplyVlv = A0;      // ATO Top Float Switch
const int flush1Vlv = A1;      // ATO Top Float Switch
const int flush2Vlv = A2;      // ATO Top Float Switch
const int outputVlv = A3;      // ATO Top Float Switch

// Variables
int button1State;             // Start Button
int button2State;             // Stop Button
int button3State;             // Flush Button
int button4State;             // ATO Top Float Switch
int lastButton1State = LOW;
int lastButton2State = LOW;
int lastButton3State = LOW;
int lastButton4State = LOW;
long lastDebounceTime = 0;
long debounceDelay = 50;

//Void Setup
void setup() {
  pinMode(button1Pin, INPUT);
  pinMode(button2Pin, INPUT);
  pinMode(button3Pin, INPUT);
  pinMode(button4Pin, INPUT);
  pinMode(supplyVlv, OUTPUT);
  pinMode(flush1Vlv, OUTPUT);
  pinMode(flush2Vlv, OUTPUT);
  pinMode(outputVlv, OUTPUT);
  Serial.begin(9600);
  digitalWrite(supplyVlv, HIGH);
  digitalWrite(flush1Vlv, HIGH);
  digitalWrite(flush2Vlv, HIGH);
  digitalWrite(outputVlv, HIGH);
  }

//Void Loop
void loop() {
  int reading1 = digitalRead(button1Pin);
  int reading2 = digitalRead(button2Pin);
  int reading3 = digitalRead(button3Pin);
  int reading4 = digitalRead(button4Pin);
  if (reading1 != lastButton1State)
  if (reading2 != lastButton2State)
  if (reading3 != lastButton3State)
  if (reading4 != lastButton4State){
  lastDebounceTime = millis();
  }
 
  if ((millis() - lastDebounceTime) > debounceDelay) {
    button1State = reading1;
    button2State = reading2;
    button3State = reading3;
    button4State = reading4;
  }
 
  if(button1State==1)
     Start ();
  if(button2State==1)
     Flush ();
  if(button3State==1)
     Cancel ();
  if(button4State==1)
     Stop ();
 
  lastButton1State = reading1;
  lastButton2State = reading2;
  lastButton3State = reading3;
  lastButton4State = reading4;
  }

//Subprograms
void Start(){
  Serial.println("Flushing the System");
  digitalWrite(supplyVlv, LOW);
  digitalWrite(flush1Vlv, LOW);
  digitalWrite(flush2Vlv, LOW);
  //delay(120000);
  delay(10000);
  Serial.println("Flushing the DI");
  digitalWrite(flush1Vlv, HIGH);
  //delay(120000); 
  delay(10000);
  Serial.println("Flushing Complete");
  delay(500);
  digitalWrite(flush2Vlv, HIGH);
  Serial.println("Outputting Water");
  digitalWrite(outputVlv, LOW);
  //delay(14400000);
  delay(10000);
  digitalWrite(outputVlv, HIGH);
  Flush();
  Serial.println("Fill Complete");
}

void Flush(){
  Serial.println("Flushing the System");
  digitalWrite(supplyVlv, LOW);
  digitalWrite(flush1Vlv, LOW);
  //delay(120000);
  delay(10000);
  digitalWrite(flush1Vlv, HIGH);
  digitalWrite(supplyVlv, HIGH);
  Serial.println("Flushing Complete");
  delay(500);
}

void Stop(){
  Serial.println("Stopping the System Nicely");
  digitalWrite(supplyVlv, LOW);
  digitalWrite(outputVlv, HIGH);
  digitalWrite(flush2Vlv, HIGH);
  //delay(500);
  digitalWrite(flush1Vlv, LOW);
  //delay(120000);
  delay(10000);
  digitalWrite(supplyVlv, HIGH);
  digitalWrite(flush1Vlv, HIGH);
  Serial.println("Complete");
}

void Cancel(){
  Serial.println("Stopping the System Now");
  digitalWrite(outputVlv, HIGH);
  delay(500);
  digitalWrite(supplyVlv, HIGH);
  delay(500);
  digitalWrite(flush1Vlv, HIGH);
  digitalWrite(flush2Vlv, HIGH);
  Serial.println("Complete");
}

Delta_G

None of those functions ever make any attempt to read any of those switches.  When you call delay you basically put the processor to sleep.  It won't do anything else while it is in a delay.

What you need is a state machine.  You'll have probably an idle state, a filling state, and a flushing state.  Google finite state machine and see if that helps to re-organize this. 
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

Go Up