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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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