Help with juice dispenser

I've been working on a project for a few weeks for a juice dispenser. I have all the hardware I require (will list below) I just can't seem to get the code right. My coding experience is basically zero and I've been trying with the help of AI to get it right but I just can't get it working properly. Here is how I want the system to work.

  1. Push button one (connected to pin 5) to trigger relay 1 (connected to pin 2) for 1 second then trigger relay 2 (connected to pin 3) for 9 seconds.

  2. Push button two (connected to pin 6) to trigger relay 3 (connected to pin 4) for 1 second then trigger relay 2 (connected to pin 3) for 8 seconds.

  3. Push button three (connected to pin 7) to trigger relay 1 (connected to pin 2) for 1.5 second then trigger relay 2 (connected to pin 3) for 12 seconds.

  4. Push button four (connected to pin 8) to trigger relay 3 (connected to pin 4) for 1.5 second then trigger relay 2 (connected to pin 3) for 12 seconds.

  5. Push button five (connected to pin 9) to trigger relay 1 (connected to pin 2) for 3 second then trigger relay 2 (connected to pin 3) for 18 seconds.

  6. Push button six (connected to reset pin) to reset the whole system in case of a fault.

The main issue I have ATM is other relays triggering after the correct relays have finished their sequence.

This is the code I have at present.

// Define button pins
const
int button1Pin =
5;
const
int button2Pin =
6;
const
int button3Pin =
7;
const
int button4Pin =
8;
const
int button5Pin =
9;

// Define relay pins
const
int relay1Pin =
2;
const
int relay2Pin =
3;
const
int relay3Pin =
4;

// Define pump run times (in milliseconds)
const
int pump1Time =
1000;
// 1 second
const
int pump2Time =
9000;
// 9 seconds
const
int pump3Time =
1500;
// 1.5 seconds
const
int pump4Time =
8000;
// 8 seconds
const
int pump5Time =
3000;
// 3 seconds
const
int pump6Time =
18000;
// 18 seconds

// Delay after the second pump finishes (in milliseconds)
const
int delayAfterSecondPump =
3000;
// 3 seconds

void
setup()
{
// Initialize buttons and relays

pinMode(button1Pin,
INPUT_PULLUP);

pinMode(button2Pin,
INPUT_PULLUP);

pinMode(button3Pin,
INPUT_PULLUP);

pinMode(button4Pin,
INPUT_PULLUP);

pinMode(button5Pin,
INPUT_PULLUP);

pinMode(relay1Pin,
OUTPUT);

pinMode(relay2Pin,
OUTPUT);

pinMode(relay3Pin,
OUTPUT);

// Initially, turn off all relays

digitalWrite(relay1Pin,
LOW);

digitalWrite(relay2Pin,
LOW);

digitalWrite(relay3Pin,
LOW);
}

void
loop()
{
// Check if buttons are pressed

if
(digitalRead(button1Pin)
== LOW)
{

runDispenser(1);

}

if
(digitalRead(button2Pin)
== LOW)
{

runDispenser(2);

}

if
(digitalRead(button3Pin)
== LOW)
{

runDispenser(3);

}

if
(digitalRead(button4Pin)
== LOW)
{

runDispenser(4);

}

if
(digitalRead(button5Pin)
== LOW)
{

runDispenser(5);

}
}

void
runDispenser(int
dispenserNumber)
{

switch
(dispenserNumber)
{

case
1:

activateRelay(relay1Pin,
pump1Time);

activateRelay(relay2Pin,
pump2Time);

break;

case
2:

activateRelay(relay3Pin,
pump3Time);

activateRelay(relay2Pin,
pump4Time);

delay(delayAfterSecondPump);
// Wait for a delay after the second pump finishes

break;

case
3:

activateRelay(relay1Pin,
pump3Time);

activateRelay(relay2Pin,
pump4Time);

break;

case
4:

activateRelay(relay3Pin,
pump3Time);

activateRelay(relay2Pin,
pump4Time);

delay(delayAfterSecondPump);
// Wait for a delay after the second pump finishes

break;

case
5:

activateRelay(relay1Pin,
pump5Time);

activateRelay(relay2Pin,
pump6Time);

break;

}
}

void
activateRelay(int
relayPin,
int
runTime)
{

digitalWrite(relayPin,
HIGH);
// Turn on the relay

delay(runTime);
// Run the pump for the specified time

digitalWrite(relayPin,
LOW);
// Turn off the relay
}

Here is a list of hardware I have.

1 Arduino uno
3 5v relays
3 12v pumps
1 12v power supply
6 normally open buttons

Buttons will be wired to gnd and pins 5-9 respectively.
Reset button is wired to gnd and reset pin.
Relays are wired to 5v gnd and pins 2-4 respectively.
12v is supplied to the com of each relay.
Negative of the power supply is wired to the negative of each pump.
The positive of each pump is wired to the normally open of each relay.
Arduino will be powed by a 5v power supply

Thanks in advance for the help :slight_smile:

have a read of how-to-get-the-best-out-of-this-forum
in particular

  1. use Tools>Auto Format to make the code more readable
  2. use code tags when uploading code, e.g. select < CODE/ > and paste the code or text where it says “type or paste code here”, also to make the code readable

Welcome to the forum

Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

Tell us what Arduino board you are using and how the project is powered, particularly the relays, and how the buttons are wired

How will this be wired?

Buttons will be wired to gnd and pins 5-9 respectively.
Reset button is wired to gnd and reset pin.
Relays are wired to 5v gnd and pins 2-4 respectively.
12v is supplied to the com of each relay.
Negative of the power supply is wired to the negative of each pump.
The positive of each pump is wired to the normally open of each relay.

Your other topic on the same subject deleted.

Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.

Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum

It will help you get the best out of the forum in the future.

Thank you.

It also needs to be connected to your Arduino GND. Everything needs a common voltage reference (e.g. ground)

Even though the power supplies voltages are different?

Please use pen and paper and creat schematisk. Word sallad doesn't work. Lots is missing.

Thanks! How is the UNO powered?
If it is powered via 12 volt to Vin it's not safe to feed the relays with the UNO 5 volt pin. The built in 5 converter will overheat.

I guess the purple pins are used for buttons.....

The uno will be powered by it's own 5v power supply. Yes the purple is the buttons

Okey. Check the current consumption of the realays. Make sure 3 x that current plus the UNO's own current doesn't exceed the limit of the UNO supply. Also check that 3 x relay current doesn't exceed the specs for the 5 volt pin. The conducting strips on the board are very tiny and might get overloaded and the board is damaged.

@aaron248, ask you AI to help you add some serial printing to your program to see the values of the variables used and confirm they are i forming the flow through the code properly.

If you can, a quick experiment would be to temporarily remove the relays and replace them with LEDs (be sure to use a serial current limiting g resistor with each) on the pins where the relays were, and watch them to further see if (or that) you have the logic correct.

Ask your AI to make sure to print a message in the setup() function. It may be that your sketch is resetting without you noticing.

a7

GND == GND == GND == GND. It doesn't matter what the voltage (V+) level is. So yes, you want to connect the 12V ground to the 5V ground to the Arduino GND

1 Like

Trigger for the relays is 5mA. But I'm pretty sure the issue I'm experiencing with pumps operating when they shouldn't be is rooted in the code.

Isn't. I ran your code. I can't swear to the exact timing, but I see no

The main issue I have ATM is other relays triggering after the correct relays have finished their sequence.

I hit a button, I get some relays on and off and maybe a delay before I can hit another button.

Try it here

Your code, plus a thing to see when your code goes blind to button presses. The @LarryD Heartbeat™ stops flashing when your code goes blind during the delays.

a7

I'm not what would be causing the faults I'm experiencing then. Do you have any ideas?

You cannot power the relays from the 5 volt pin. Did you miss @Railroader's post?

Did you try using serial print to see if

your sketch is resetting without you noticing.

?

Did you gain confidence in the software by trying the simulation I posted?

Did you ask your AI for help?

a7

My post was about the coil current supply. Try again. From where do they get the current?