LED grow garden query

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:

  1. The push button sets different states indicated by the LEDs.

  2. Each state has a different on/off cycle time for the lights (currently set to a couple of seconds for testing).

  3. The pump function should have the same on/off time for every state.

Some questions I have are:

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

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

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

Maybe ChatGPT generates good code sometimes. But we never see that on the forum. We only see the whacky stuff it generates, so it has a very low reputation here! Often those asking for help don't have enough knowledge or experience to realise it's whacky, so it's good that you did!

Thanks for posting your code here and using code tags. I'll take a look and I'm sure others will too.

What's the difference? To my mind, "board for voltage step-down" == buck converter

Hi Swales,

Don't use delay(). That's what prevents the Arduino running independent cycles. Use millis() instead. But it's not simply a matter of replacing delay() with millis(). You have to re-structure your code to be able to use millis()

Don't use interrupts for buttons, unless the intention is to wake a sleeping arduino, which you are not doing here. I suspect you found the need to use interrupts because you are using delay() and the presses are getting ignored during delay(). But, if you don't use delay(), there is no need to use interrupts for buttons. With no delay() in the code, loop() will execute hundreds of times per second, so you can check for button presses in loop() with no danger of missing them.

I can't open it. I can only see a fuzzy wiring diagram with the edges cut off, so I can't check all the circuit. Can you post a schematic (your wiring diagram isn't a schematic) on this forum please?

What I could see alarmed me for several reasons.

  • No flyback diodes on the motor and pump. This could damage other components, including the Arduino
  • No resistors connected to the transistors. Again, damage could occur.
  • Motors/pumps wired through breadboard. Breadboards are designed for low currents and could be damaged.
  • You mentioned a buck converter, but I can't see it. I can see what might be a linear regulator. It's missing a capacitor.
  • No resistor is needed for the button. Connect the button between the Arduino pin and ground and use INPUT_PULLUP.
1 Like

I will wait for the annotated schematic, the picture is missing to much. If you need a CAD (Computer Aided Design) program you can get KiCad for the download and a voluntary donation (donation not necessary). This will take a few days to learn. Also start by writing code to control each section of your project. If you want to burn lots of time use the delay(), other wise the best is to use the mills function. When using the delay the Arduino just counts off time and does nothing else stoping your code at that point.

Solve this.

Hi Paul,

Thanks for your response, replacing the delay() function with Millis() function sounds like the way to go. As you said I included the interupt on the push button due to the use of the delay() function.

Regards,

Swales

Hi Gil,

Thanks for your input, I purchased Fritzing and once I get my head around it I will replicate the design in there as it can automatically generate a circuit diagram once the design has been implemented.

Regards,

Swales

Most of us do the design first in CAD then build it. It is surprising how much you find while doing the design.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.