Controlling momentary switch with Arduino Uno

Hello. Before I ask my question, just wanna give some heads up. I am a newbie so please dont be too harsh on me and if my question sounds rather stupid that's because I dont know much about the subject matter. I am doing it for my project and using controller is just a small part of it. I tried looking for tutorials and have done my fair share of research (that's what I believe) but was unsuccessful so I am asking question here as a last resort.

So, I want to control the current flow when momentary switch is pressed. To be more precise, when I press the momentary switch I want the current to pass for 20 mill seconds only regardless of how long I press the momentary switch. I will post the code below which I got from watching a tutorial on YT.


unsigned long welder_running_time = 0;
int welding_time = 20;


void setup() {
  Serial.begin(9600);
  delay(150);

}

void loop() {
  welder_running_time = millis();
  while((millis()-welder_running_time) <= welding_time) {
  Serial.println("Welding time is 20 Milli Seconds"); } 
  
  }

I am not sure how I can modify this code to let the switch pass current for 20 ms only when pressed. I know I have to attach pin number and all that but have no idea where to begin. Here's the schematic diagram of the whole project I got from youtube.

What makes you believe we would be harsh on you? Nasty rumors. If you want to make something 'run' for a specified period of time, you need a start time and a duration or end time. Duration is more common as you will learn, but it is pretty much as you have done except welder_running_time is initialized and never changed. When you attach a button to an input pin and continually check to see if that button has been pushed, once it has then you set welder_running_time to millis and then test just as you have. Easy and not harsh, but you still have to think a bit.

So I read some tutorials after I posted my question. Here's what I have done. Does this look about right?

unsigned long welder_running_time = 0;
int welding_time = 20;
int inputPin = 2;   //connected to digital pin 2


void setup() {
  Serial.begin(9600);
  delay(150);
  pinMode(inputPin,INPUT); 

}

void loop() {
  welder_running_time = millis();
  if ((millis()-welder_running_time) <= welding_time) 

  {
    digitalWrite(inputPin,HIGH); // button not pressed
  }
  else 
  {
    digitalWrite(inputPin,LOW); //button pressed 
   }
  }

PS: Thanks for the input @DK

Using a millis() timer to time a 20 millisecond delay is probably overkill.

  digitalWrite(OutputPin, HIGH);
  delay(20);
  digitalWrite(OutputPin, LOW);

If the input is a button you will want to add Debounce and State Change Detection.

const byte ButtonPin = 2;
const unsigned long DebounceTime = 30;

void setup()
{
  Serial.begin(115200);

  pinMode(LED_BUILTIN, OUTPUT);
  pinMode (ButtonPin, INPUT_PULLUP);  // Button between Pin and Ground
}

void loop()
{
  unsigned long currentTime = millis();
  boolean buttonPressed = digitalRead(ButtonPin) == LOW;  // Active LOW
  static boolean buttonAlreadyPressed = false;
  static unsigned long buttonStateChangeTime = 0;

  // Check for button change and do debounce
  if (buttonPressed != buttonAlreadyPressed &&
      currentTime -  buttonStateChangeTime > DebounceTime)
  {
    // Button state has changed
    buttonStateChangeTime = currentTime;
    buttonAlreadyPressed = buttonPressed;

    if (buttonPressed)
    {
      // Button was just pressed
      digitalWrite(LED_BUILTIN, HIGH);
      delay(20);
      digitalWrite(LED_BUILTIN, LOW);
    }
  }
}

Thanks for the reply, John.

Last Question I promise (hopefully). What does debounce = 30 actually do? Also, if I have momentary switch with 5 pins like this

how do I connect the right wire to right port on arduino? Btw, the momentary switch is also connected to negative terminal of battery and solenoid as shown in the following schematic.

here's the product instruction for the switch.

Imgur

My guess is that you switch has a light that runs off the +/- power terminals. The other three terminals are "Common", "Normally Open" (connected to Common only while the button is pressed) and "Normally Closed" (connected to Common only while the button is not pressed).

The picture of the terminal welder is too blurry to read the smaller text. What is that light blue-green box connected to the ground terminal? It appears to have a switch in it... And that switch connects one side of your momentary switch to the '+' side of your battery while the other side of your momentary switch is connected to the '-' side of the battery. If both switches close, something will fry.

Perhaps a pointer to the source of the diagram will help.

johnwasser:
My guess is that you switch has a light that runs off the +/- power terminals. The other three terminals are "Common", "Normally Open" (connected to Common only while the button is pressed) and "Normally Closed" (connected to Common only while the button is not pressed).

The picture of the terminal welder is too blurry to read the smaller text. What is that light blue-green box connected to the ground terminal? It appears to have a switch in it... And that switch connects one side of your momentary switch to the '+' side of your battery while the other side of your momentary switch is connected to the '-' side of the battery. If both switches close, something will fry.

Perhaps a pointer to the source of the diagram will help.

here's is the schematic. The video is only 20 seconds.

As shown in the schematic, one end of the momentary switch is connected to solenoid and the other one to the negative terminal of the battery ( This is the same momentary switch that will be connected to Arduino, too).

OK... So the diagram says "solenoid". Do they mean "solenoid-activated starter motor switch like from a car" or some other kind of solenoid?

To answer a previous question:

const unsigned long DebounceTime = 30;

This sets the debounce time in milliseconds and it's used here:

  // Check for button change and do debounce
  if (buttonPressed != buttonAlreadyPressed &&
      currentTime -  buttonStateChangeTime > DebounceTime)
  {
    // Button state has changed
    buttonStateChangeTime = currentTime;
    buttonAlreadyPressed = buttonPressed;

This is the state change and debounce part. The first part of the 'if' looks for a change in the button state since typically you want to act when the button is pressed or released. When you close mechanical contacts in a button, switch, or relay, it is common for the contacts to bounce against each other, causing several state changes in a short period of time: closed - open - closed - open - closed... The second part of the 'if' ignores the change of state if it has been less than 30 milliseconds since the last recognized change of state. This prevent treating the bounces as multiple presses and releases. If you need to be able to press the button more than 33 times per second you can use a shorter debounce timer.

Yup, its a starter solenoid for a car. 12V, 500 A.

It sounds like you want the Arduino to turn the solenoid on for 20 milliseconds (2 hundredths of a second) each time you press the button. To do that you would use a logic-level N-Channel power MOSFET to ground the start terminal of the solenoid when an Arduino output pin is HIGH.

Set a multimeter for measuring DC Amps and measure the current between the solenoid start terminal and Ground (-). WARNING: This will activate the solenoid. Once you know the current draw of the solenoid coil you will know enough to order an appropriate MOSFET.

Thanks for being so patient with me and pointing me to the right direction. From here on I will figure out things myself. Thanks once again, John. Cheers!!