Controlling the extension time of a solenoid

Hi everyone,

I am doing a vending machine project and have been trying to program the complete codes for it. Everything seems to work fine with the codes except for the solenoid. I am using 3 12V/300mA solenoids controlled by a relay module connected to a UNO Arduino board. My main purpose for using the solenoids is to simply turn them on and off with a little delay time in between those states when the machine "knows" whether if there is a $2, $1 or 25cent is inserted in. My first assumption is to use the "delay" function in between to have it remained activated for 2 seconds and deactivate it.

I am only trying connecting 1 solenoid to the circuit at a time. When I tested it, it only "flinched" for a split second, not fully extended nor remained extended for 2 seconds. It confused me because when I tested the solenoid separately with a short, simple code turning it on and off with delay function and it worked as expected. But, when assembled it with the rest of the code, the delay function was just simply being ignored and the solenoid only "flinched" a bit. There might be some silly, clumsy newbies programming errors that I made in the code since I am not an expert in programming.

I only post a piece of code contains the solenoid control part in it since the whole program is very long and eye-tiring to read. I will post more codes if requested for further discussion. I have also provided links below to the solenoids and relay module that I am using for this project for more information.

All help and responses are greatly appreciated. Thank you. :slight_smile:

else if((36 < delta_T) && (delta_T < 50) && (y == 3))   // If calculated delta time falls in 36ms - 50ms range
    {                                                       
      Serial.print("25c slot open");                    // Then prints out "25c slot open" to Serial monitor
      Serial.println();
      Serial.println();  

      digitalWrite(slot_25c, HIGH);                  // Activate 25c solenoid to open the slot
      delay(2000);                                   // Wait for 2s for the coin to fall into the slot
      digitalWrite(slot_25c, LOW);                   // Deactivate 25c solenoid  
  
     
      lcd.clear();                                   // Print out on LCD how much money has been inserted  
      lcd.setCursor(0,0);
      lcd.print("$");
      lcd.setCursor(1,0);
      lcd.print(total);
      lcd.setCursor(6,0);
      lcd.print("Inserted");

      total = total + 0.25;                          // Calculate the total money has been inserted

      y = 1;                                         // Reset the condition
      
      delay(500);
    }

12V/300mA Solenoid Actuator

Relay module

You did not post the whole code.

Does the corresponding led "DSx" on the relay module stay lit for 2 sec? (so you can say if command is ok).

if DSx is ok : Do you hear the relay "clik" on, and then "clik" off after 2 sec? (so you can say relay followes command ok)

Is power supply ok?
Try use more delay (just to see if any difference)

GRuser:
You did not post the whole code.

Does the corresponding led "DSx" on the relay module stay lit for 2 sec? (so you can say if command is ok).

if DSx is ok : Do you hear the relay "clik" on, and then "clik" off after 2 sec? (so you can say relay followes command ok)

Is power supply ok?
Try use more delay (just to see if any difference)

Hi GRuser,

Thank you for your response. The power supply should be ok because I have this wall-wart connected to the outlet and is capable of supplying 12V/10A for the whole circuit. Neither the relay led nor the clicking sound stayed for 2 seconds. They only came on for a split second even though I already tried increasing the delay and add more delays to it. Could it be the "If" instruction itself that caused the problem or the way my program is not very well coded?

I have also connected the solenoids on to a separate board and wrote a simple code and they worked just fine. But, somehow when I assembled that piece of code to the bigger code, they behaved differently. Here is more code of that specific routine:

void Slot_Open()
{
    if((15 < delta_T) && (delta_T < 22) && (y == 3))        // If calculated delta time falls in 15ms - 22ms range
    {                                                       
      Serial.print("$2 slot open");                         // Then prints out $2 to Serial monitor
      Serial.println();
      Serial.println();
     
    /*  digitalWrite(slot_2, HIGH);                      // Activate $2 solenoid to open the slot
      delay(2000);                                     // Wait for 2s for the coin to fall into the slot
      digitalWrite(slot_2, LOW);                       // Deactivate $2 solenoid once the coin falls into the slot
      delay(2000);
    */

      total = total + 2;                               // Caculate the total money has been inserted
      
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("$");
      lcd.setCursor(1,0);
      lcd.print(total);
      lcd.setCursor(6,0);
      lcd.print("Inserted");
      
      y = 1;                                           // Reset the condition

      delay(500);   
    }

    else if((24 < delta_T) && (delta_T < 33 ) && (y == 3))  // If calculated delta time falls in 24ms - 33ms range
    {                                                       
      Serial.print("$1 slot open");                         // Then prints out $1 to Serial monitor
      Serial.println();
      Serial.println();
     
     /* digitalWrite(slot_1, HIGH);                          // Activate $1 solenoid to open the slot
      delay(2000);                                         // Wait for 2s for the coin to fall into the slot
      digitalWrite(slot_1, LOW);                           // Deactivate $1 solenoid once the coin falls into the slot
      delay(2000);
    */
          
      total = total + 1;                                   // Caculate the total money has been inserted
      
      lcd.clear();                                         // Print out how much money has been inserted to the LCD
      lcd.setCursor(0,0);
      lcd.print("$");
      lcd.setCursor(1,0);
      lcd.print(total);
      lcd.setCursor(6,0);
      lcd.print("Inserted");

      y = 1;                                               // Reset the condition

     delay(500);       
    }
  
    else if((36 < delta_T) && (delta_T < 50) && (y == 3))   // If calculated delta time falls in 36ms - 50ms range
    {                                                       
      Serial.print("25c slot open");                        // Then prints out "25c slot open" to Serial monitor
      Serial.println();
      Serial.println();  

      digitalWrite(slot_25c, HIGH);                        // Activate 25c solenoid to open the slot
      delay(3000);                                         // Wait for 2s for the coin to fall into the slot
      digitalWrite(slot_25c, LOW);                         // Deactivate 25c solenoid once the coin falls into the slot      
      delay(2000);

      total = total + 0.25;                               // Calculate the total money has been inserted

     
      lcd.clear();                                         // Print out on LCD how much money has been inserted  
      lcd.setCursor(0,0);
      lcd.print("$");
      lcd.setCursor(1,0);
      lcd.print(total);
      lcd.setCursor(6,0);
      lcd.print("Inserted");
      
      y = 1;                                              // Reset the condition
      
      delay(500);
    }

    else
    {
      Serial.print("Try again");                          // Print out "Try again" to Serial Monitor
      Serial.println();
      Serial.println(); 
      
      lcd.clear();                                        // Print out "Try again" to the LCD
      lcd.setCursor(0,0);
      lcd.print("Try again");
      
      y = 1;                                              // Reset the condition
      
      delay(500);
    }
}

Notice I only tested the "Slot_25" solenoid and commented out the other solenoids. I just want to be clear on that one. I am also thinking of rewriting the code by using "switch and cases" instruction instead.

All help and suggestions are appreciated. Thank you.