Hi,
I'm new to Arduino and recently encountered an opportunity to experiment with it. I have a microgreen grow unit from Gathera that unfortunately no longer powers on. The unit features LED lights operating on a 12-hour on/12-hour off cycle and a water pump running for 10 minutes every hour. The device is powered by a 24V plug.
After testing the switch and voltages on the board, I suspect the issue lies with the board itself. So I thought I would try replacing the board with an Arduino device with some extra functionality.
I have created a Tinkercad project simulation using an Arduino Uno R3, but I haven't implemented it on the actual device yet.
The desired functionality for the Arduino project is:
-
The push button sets different states indicated by the LEDs.
-
Each state has a different on/off cycle time for the lights (currently set to a couple of seconds for testing).
-
The pump function should have the same on/off time for every state.
Some questions I have are:
-
How can I modify the code to allow the LED lights and motor to run on independent on/off cycles? I used ChatGPT to generate code, but it generates some whacky stuff when integrating the two cycles. I'm wondering if there's a more efficient approach.
-
Is the buck converter in the circuit suitable for a 24V to 5V conversion, or would an expansion board for voltage step-down be a better choice?
-
I would appreciate any additional feedback or suggestions regarding the project.
The code is in the Tinkercad project but I have repeated it below for reference.
const byte LED1[] = {8, 9, 10, 11};
const int P1 = 2;
const int debaunceDelay = 100;
byte stato = 1;
volatile bool buttonPressed = false;
const int motorPin = 7; // NMOS transistor light control pin
const int PumpMotorPin = 6; // NMOS transistor pump control pin
// Define on and off times (in milliseconds) for each state
const unsigned long stateTimes[][2] = {
//{43200000, 43200000}, // 12h on, 12h off for state 1
{2000, 2000}, // Light cycle duration for state 1
{5000, 5000}, // Light cycle duration for state 2
{2000, 2000}, // Light cycle duration for state 3
{5000, 5000}, // Light cycle duration for state 4
{5000, 5000}, // Light cycle duration for state 5
{1000, 2000} // Pump cycle duration for all states
};
void setup()
{
pinMode(LED1[0], OUTPUT);
pinMode(LED1[1], OUTPUT);
pinMode(LED1[2], OUTPUT);
pinMode(LED1[3], OUTPUT);
pinMode(P1, INPUT);
pinMode(motorPin, OUTPUT); // Set the motorPin as OUTPUT
pinMode(PumpMotorPin, OUTPUT); // Set the PumpMotorPin as OUTPUT
// Attach an interrupt to the button pin
//attachInterrupt(digitalPinToInterrupt(P1), buttonInterrupt, RISING);
attachInterrupt(digitalPinToInterrupt(P1), buttonInterrupt, CHANGE);
}
// Pump Control Section
//PumpControl(stateTimes[6][0], stateTimes[6][1]);
void loop()
{
if (buttonPressed)
{
buttonPressed = false; // Reset the flag
if (stato < 5)
stato = stato + 1;
else
stato = 1;
}
switch (stato)
{
case 1:
digitalWrite(LED1[0], LOW);
digitalWrite(LED1[1], LOW);
digitalWrite(LED1[2], LOW);
digitalWrite(LED1[3], LOW);
break;
case 2:
digitalWrite(LED1[0], HIGH);
digitalWrite(LED1[1], LOW);
digitalWrite(LED1[2], LOW);
digitalWrite(LED1[3], LOW);
// Motor Control Section
motorControl(stateTimes[stato - 1][0], stateTimes[stato - 1][1]);
// Pump Control Section
//PumpControl(stateTimes[6][0], stateTimes[6][1]);
break;
case 3:
digitalWrite(LED1[0], LOW);
digitalWrite(LED1[1], HIGH);
digitalWrite(LED1[2], LOW);
digitalWrite(LED1[3], LOW);
// Motor Control Section
motorControl(stateTimes[stato - 1][0], stateTimes[stato - 1][1]);
// Pump Control Section
//PumpControl(stateTimes[6][0], stateTimes[6][1]);
break;
case 4:
digitalWrite(LED1[0], LOW);
digitalWrite(LED1[1], LOW);
digitalWrite(LED1[2], HIGH);
digitalWrite(LED1[3], LOW);
// Motor Control Section
motorControl(stateTimes[stato - 1][0], stateTimes[stato - 1][1]);
// Pump Control Section
//PumpControl(stateTimes[6][0], stateTimes[6][1]);
break;
case 5:
digitalWrite(LED1[0], LOW);
digitalWrite(LED1[1], LOW);
digitalWrite(LED1[2], LOW);
digitalWrite(LED1[3], HIGH);
// Motor Control Section
motorControl(stateTimes[stato - 1][0], stateTimes[stato - 1][1]);
// Pump Control Section
//PumpControl(stateTimes[6][0], stateTimes[6][1]);
break;
}
delay(debaunceDelay);
}
void buttonInterrupt()
{
buttonPressed = true;
}
void motorControl(unsigned long onTime, unsigned long offTime)
{
// Motor Control Section
digitalWrite(motorPin, HIGH); // Turn on the motor
delay(onTime);
digitalWrite(motorPin, LOW); // Turn off the motor
delay(offTime);
}
//void PumpControl(unsigned long onTime, unsigned long offTime)
//{
// Pump Control Section
// digitalWrite(PumpMotorPin, HIGH); // Turn on the motor
// delay(onTime);
// digitalWrite(PumpMotorPin, LOW); // Turn off the motor
// delay(offTime);
//}
Thanks,
Swales