Code Running "Automatically"

Hello!

I am having trouble with this school project. I am running some pumps and motorized ball valves with the Arduino Uno and a 4-Channel Relay. For some reason Pump_1 starts up on its own and completes its cycles. Any ideas on why it is doing this?

Also new to coding so hope this code makes sense. TIA

const int BUTTON_PIN_2 = 7; //Pump 1 Button 
const int BUTTON_PIN_3 = 6; //Pump 2 Button 
const int RELAY_PIN_2  = 2; //Pump 1 Relay Pin
const int RELAY_PIN_3  = 3; //Pump 2 Relay Pin
const int RELAY_PIN_4  = 4; //Valve 1 Pin
const int RELAY_PIN_5  = 5; //Valve 2 Pin 

void setup() {
  pinMode(BUTTON_PIN_2,INPUT_PULLUP);
  pinMode(RELAY_PIN_2, OUTPUT);
  pinMode(RELAY_PIN_4, OUTPUT);

  pinMode(BUTTON_PIN_3, INPUT_PULLUP);
  pinMode(RELAY_PIN_3, OUTPUT);
  pinMode(RELAY_PIN_5, OUTPUT);

  Serial.begin(9600);
}

void loop() {
  PUMP_1();
  PUMP_2();
}

void PUMP_1() {
  int buttonState_2 = digitalRead(BUTTON_PIN_2); 
  if (buttonState_2 == LOW) {
    digitalWrite(RELAY_PIN_2, HIGH); 
    delay(12000); // Change pump 1 time here
    digitalWrite(RELAY_PIN_2, LOW);
    digitalWrite(RELAY_PIN_4, HIGH);
    delay(27000); // FLOW TIME 
    digitalWrite(RELAY_PIN_4, LOW);
    delay(10000); // Valve Close time
  }
  else {
  if (buttonState_2 == HIGH) {
    digitalWrite(RELAY_PIN_2, LOW);  
  }
  }
}

void PUMP_2() {
  int buttonState_3 = digitalRead(BUTTON_PIN_3); 
  if (buttonState_3 == LOW) {
    digitalWrite(RELAY_PIN_3, HIGH); 
    delay(12000); // Change pump time here 
    digitalWrite(RELAY_PIN_3, LOW);
    digitalWrite(RELAY_PIN_5, HIGH);
    delay(27000); // FLOW TIME
    digitalWrite(RELAY_PIN_5, LOW);   
    delay(10000); //Valve close time 
}
  
  else {
  if (buttonState_3 == HIGH) {
    digitalWrite(RELAY_PIN_3, LOW);  
  }
}
}

Let’s see the actual wiring ?

Yeah it will not let me add a picture. Any ideas what is going wrong?

Strange. Do as You did posting the first picture. (Paper drawing, camera, file -> submit....)

It looks like some spam protection software is overenthusiastic.

1 Like

Probably not related to your issue, but if you've tested the result of a digitalRead to see if it is LOW (and it isn't), you don't have to test it again to see if it is HIGH :wink:

1 Like

Read some other posts/threads. The forum software gives you more privilege when you do. Allegedly, ten minutes or so will earn you a promotion.

1 Like

I really can't make head or tail of that picture - too many wires the same colour and, for fun, changing to a different one along the way.

I'd do some simpler experiments with code that just turns one relay on & off & prove that the wiring for each is right. And to be clear, I'd have a sketch that runs only one relay and adjust the pin numbers it uses between tests.

Actually, if many new posters read the forum for 10 minutes, they would make far better new posts.

Actually pretty neat code!
Can still be improved, but seems to code for what you want it to do...
What kind of buttons do you use?
Does it have 3 contacts?
Are they identical?

You forgot using pen and paper. That bird nest is not of much help.

You have really set yourself up for confusion and pain.... why such useless names?

Why not ...

const int PUMP_1_BUTTON_PIN = 7;

... for example?

The good news - names like that at least enable a simple global find and replace in the editor... go try doing that with "3". :slight_smile:

Haha that's so true! Thanks for that :slight_smile:

So I have been playing around with the wiring to see if everything is connected and I have found two things:

(1) The momentary switch will activate by touching the non-ground wire (so wires from pin 6 and 7). Im curious, why is this the case?

(2) I attempted to organize the wires with zip-ties. I had them very tightly bound together. Once I cut the ties off my problem seems to have been fixed. The program is not running until the button is pressed. Thoughts on this?

Single "Dupont" wires wandering from point to point in a spider web are probably not reliable and completely inappropriate for a fully working system. Chinese-made ones as commonly sold, require full testing.

So what would you do instead? Get a shield with more permanent connections?

Something like this?

One thing I've done and seen recommended, is to solder a Nano to a proto board. Then either short wires point to point flat to the board, or a custom PCB with traces and wire/plug connectors of your choice. All wires should be shielded if they are not already in some shielded conductive enclosure.

The Uno doesn't have any good connection options for a permanent installation. The closest thing is a proto shield with your choice of connectors custom soldered to it, or as above, a custom shield PCB.

Well, that would provide more reliable connections, but is seriously "over the top". :grin:

Really, the UNO is a very poor choice. With a Nano, you can either solder directly to the board, or solder pins and mount into a(n inexpensive) "screw shield" or solder it to stripboard to facilitate multiple connections.

It seems for the relay board you would still have to use the "Dupont" connectors but you can get multi-way "shells" that hold the pins together and prevent them individually dislodging.

I note you have powered it correctly, but have something plugged into the "barrel jack" of the UNO. This is not a good way of powering it; there are complications. :roll_eyes: