Go Down

Topic: help with code/mechanics (Read 604 times) previous topic - next topic

Nick_H

can someone tell me if I have an error in my code that makes my water valves operate poorly. My problem is that the arduino doesnt send power to my valves when both my IR remote value and limit switch value are the correct parameters to send the signals. The valve coding is at the end whereas the IR remote and limit switch are in the middle. I have checked my serial monitor and they do send the correct values to the valve control coding which is why I am having trouble figuring out what is wrong with the valve signals. I dont know if this helps but I do have my valves controlled by another power source with relays that open and close from the arduino's power signals(valve control). thanks for the help. Here is the code>>

Code: [Select]
#include <LiquidCrystal.h>

#include <boarddefs.h>
#include <IRremote.h>
#include <IRremoteInt.h>
#include <ir_Lego_PF_BitStreamEncoder.h>



const int RECV_PIN = 6;
IRrecv irrecv(RECV_PIN);
decode_results results;
const int WaterSensor1 = A0;
const int WaterSensor2 = A1;
int LimitValveO1 = 9;
int LimitValveO2 = 4;
int LimitValveC1 = 8;
int LimitValveC2 = 5;
int value1;
int value2;
int value3;
int var1 = 0;
int var2 = 0;
int var3 = 0;
int var4 = 0;
int go = 0;
const int rs = A2, en = A3, d4 = A5, d5 = A4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
//int OpenPushButton = 7;
//int ClosePushButton = 6; //dont need buttons anymore
int flag1;
int flag2;
int OpenWValve1 = 10;
int CloseWValve1 = 11;
int OpenWValve2 = 12;
int CloseWValve2 = 13;
//int IRO = 16720605
//int IRC = 16761405
//int PP = 16712445


void setup() {
  Serial.begin(9600);
  irrecv.enableIRIn(); // enable receiver module
  irrecv.blink13(true);
  lcd.begin(16, 2);               //set up lcd colums and rows:
  lcd.print("why"); //print a message to the lcd
//  pinMode(OpenPushButton, INPUT);
  //pinMode(ClosePushButton, INPUT);
  pinMode(LimitValveO1, INPUT);
  pinMode(LimitValveC1, INPUT);
  pinMode(LimitValveO2, INPUT);
  pinMode(LimitValveC2, INPUT);
  pinMode(OpenWValve1, OUTPUT);
  pinMode(OpenWValve2, OUTPUT);
  pinMode(CloseWValve1, OUTPUT);
  pinMode(CloseWValve2, OUTPUT);
  pinMode(WaterSensor1, INPUT);
  pinMode(WaterSensor2, INPUT);

}

void loop() {
  value1 = analogRead(WaterSensor1);        // read water sensor 

//LCD SCREEN//

  value2 = analogRead(WaterSensor2);
 //Serial.println(digitalRead(LimitValveC2));
  // Serial.println(flag1);
  //Serial.println(var4);
  //Serial.println(results.value);
  //Serial.println(value1);
  //Serial.println(value2);
  //Serial.print(lcd.);
  Serial.println(digitalRead(CloseWValve2));

  //LCD DISPLAY//


  if (value1 >= 100 & value2 >= 100) {
    lcd.setCursor(0,0);
    lcd.write("1 Wet 2 Wet");       // both wet

  }
  if (value1 >= 100 & value2 <= 99) { //1 wet 2 dry
    lcd.setCursor(0,0);
    lcd.write("1 Wet 2 Dry");
   
  }
    if(value1<=99 & value2>=100){      //1 dry 2 we//t
     lcd.setCursor(0,0);
     lcd.write("1 Dry 2 Wet");
     
   }
   if (value1<=99 & value2<=99){        //both dry
     lcd.setCursor(0,0);
     lcd.write("1 Dry 2 Dry");
     
   }
   
   if(digitalRead(LimitValveO1)==1){
    lcd.setCursor(0,1);
    lcd.write("1 OPEN");
   }
     if(digitalRead(LimitValveC1)==1){
    lcd.setCursor(0,1);
    lcd.write("1 SHUT");
   }
   if(digitalRead(LimitValveO1)==0){
    if(digitalRead(LimitValveC1)!=1){
      lcd.setCursor(0,1);
      lcd.write("1 NULL");
    }
   }

 
 
    if(digitalRead(LimitValveO2)==1){
    lcd.setCursor(7,1);
    lcd.write("2 OPEN");
   }
     if(digitalRead(LimitValveC2)==1){
    lcd.setCursor(7,1);
    lcd.write("2 SHUT");
   }
    if(digitalRead(LimitValveO2)==0){
    if(digitalRead(LimitValveC2)!=1){
      lcd.setCursor(7,1);
      lcd.write("2 NULL");
    }
    }

  //var for IR remote//


  if (irrecv.decode(&results)) {

    irrecv.resume();

    switch (results.value) {
      case 16720605:
        var1 = 1;  //open
        var2 = 1;
        var3 = 0;
        var4 = 0;
        break;
      case 16761405:
        var3 = 1;   //close
        var4 = 1;
        var1 = 0;
        var2 = 0;
        break;
      case 16712445:
        go = 1;
        break;
      case 16753245:
        var1 = 0;
        var2 = 0;
        var3 = 0;
        var4 = 0;
        go = 0;
        break;
    }
  }



  //valve control


  if (var1 == 1) {
    if (digitalRead(LimitValveO1) != 1) {
      digitalWrite(OpenWValve1, HIGH);
    }
    if (digitalRead(LimitValveO1) == 1) {
      digitalWrite(OpenWValve1, LOW);
    }
  }
   if (var2 == 1) {
    if (digitalRead(LimitValveO2) != 1) {
      digitalWrite(OpenWValve2, HIGH);
    }
    if (digitalRead(LimitValveO2) == 1) {
      digitalWrite(OpenWValve2, LOW);
    }
  }
    if ((var1 ==0) || (digitalRead(LimitValveO1) == 1)) {
      digitalWrite(OpenWValve1, LOW);
    }
   if ((var2 ==0) || (digitalRead(LimitValveO2) == 1)) {
      digitalWrite(OpenWValve2, LOW);
    }


   
    ///////////close/////////
      if (var3 == 1) {
    if (digitalRead(LimitValveC1) != 1) {
      digitalWrite(CloseWValve1, HIGH);
    }
    if (digitalRead(LimitValveC1) == 1) {
      digitalWrite(CloseWValve1, LOW);
    }
  }
   if (var4 == 1) {
    if (digitalRead(LimitValveC2) != 1) {
      digitalWrite(CloseWValve2, HIGH);
    }
    if (digitalRead(LimitValveC2) == 1) {
      digitalWrite(CloseWValve2, LOW);
    }
  }
    if ((var3 ==0) || (digitalRead(LimitValveC1) == 1)) {
      digitalWrite(CloseWValve1, LOW);
    }
   if ((var4 ==0) || (digitalRead(LimitValveC2) == 1)) {
      digitalWrite(CloseWValve2, LOW);
    }
   
 


}

Paul_KD7HB

If you are sure everything else is ok, then the only thing left is the power supply to the valves. Measure the voltage going to the valves. Is it the same as your power source when you are NOT trying to operate the valves. Most likely the voltage is dropping way below what the valves require.

Paul

Nick_H

we have a DC power supply that is rated 12V for both of the valves, and the valves are rated 9-24V. Another problem I am running into is that the relays rapidly turn on and off when we try to command one of the valves to work. That might as well be a power issue so we will try to set up two power supplies to make it work. If anything changes let me know, and thank you.

Paul_KD7HB

I am not surprised your relays turn on and off rapidly. I don't see any code to allow time for the valves to operate. Am I wrong?

Paul

Nick_H

I shouldn't need a delay on the valve control because it is using a switch case that leaves them on until a limit switch is hit.

Wawa

if (value1 >= 100 & value2 >= 100)

Been told in another thread that '&&' and 'AND' are the same.
Here's another one. Are '&&' and '&" the same?
Leo..

Nick_H

I am not having any problems with the LCD code, that is right and it will stay like that.

Nick_H

Thank you everyone for helping, I figured it out. One of my ports in the arduino was faulty. I changed the port and it works as of now. Thank you and goodnight!

Wawa


Go Up