Need help with momentary switch


I am trying to make a spot welder using 12V, 360 CCA Car battery. I wish to have a specific welding time of 0.02 sec using a microcontroller (Arduino) for my spot welder. I wrote the code and verified and it checks. However, I am not sure whether the value I have assigned for momentary switch is in second(time). If someone could check my code and make any correction if there's any then that would be much appreciated.

PS: if 0.02 is not in second(time unit) then how do I assign momentary_switch a time unit?

//Global Variables

float momentary_switch; // create variable to store welding time for momentary switch
int switch_pin = 7; // attach relay to pin 7
int led = 13; // attach led to pin 13.

void setup() // Will execute once at the start of the code.
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
pinMode (switch_pin, OUTPUT); // sets switch_pin 7 up as an output
pinMode (led, OUTPUT); // sets the led pin 13 up as an output.

void loop() // code here will continue to replay until powered off.

momentary_switch = analogRead(switch_pin);

if (momentary_switch <= 0.02)
digitalWrite (led, HIGH); // turns on led.
digitalWrite (switch_pin, HIGH);

digitalWrite (led, LOW); // turns off led.
digitalWrite (switch_pin, LOW); // turns of switch
delay(3000); // wait 3 seconds before redoing the loop.

I'm confused. You want that variable to be a time but then you get the value for it from an analogRead so you're putting in a voltage. And you never use it as a time.

Variables don't have units. They're just numbers. It depends how you use them. If you use the same number in a analogWrite statement then it is PWM duty cycle and then you use the same number in a delay call and it's milliseconds and then you use the same in a delayMicroseconds call and it's microseconds.

momentary_switch = analogRead(switch_pin);

What do you envision this line doing? analogRead gets a voltage from an analog input pin. You're calling it on a digital output pin. It returns an integer between 0and 1023. Whole numbers only. So comparing to 0.02 is a bit ridiculous.

You should probably take a bit to go through some of the basic tutorials and learn the basics of Arduino code. Programming isn't something you can guess your way through or learn by the seat of your pants.

int switch_pin = 7;                       // attach relay to pin 7

Is the variable name correct, or is the comment?

  pinMode (switch_pin, OUTPUT);          // sets switch_pin 7 up as an output

It hardly makes sense to declare an input pin as an OUTPUT.

     momentary_switch = analogRead(switch_pin);

Which Arduino are you using? Most do not have an analog pin 7.


It's a little bit more complicated.

The Arduino IDE seems to happily cope with ambiguous values for pins and even analogue inputs which have no connection to the outside world.

This code runs quite happily on an Arduino Nano and reads from analogue input A7 at the same time as outputting LOW or HIGH to digital pin 7

// Testing for ADC7 on a Arduino UNO
// Ian Crowe
// 30-Oct-18

int ambiguousPin = 7;
int potPin = A0;
int potRead, ambiguousRead;

void setup() {
  Serial.println("Starting Test....");
  pinMode(ambiguousPin, OUTPUT);       // Sets digital pin 7 as an output

void loop() {
  potRead = analogRead(potPin);   // Read the voltage from A0
  ambiguousRead = analogRead(ambiguousPin);  // Should read a random value from A7??
  Serial.print("Pot value = ");
  Serial.print("  Ambiguous reading = ");
  if (potRead > 512) {
    digitalWrite(ambiguousPin, HIGH);
  else {
    digitalWrite(ambiguousPin, LOW);


It also ran quite happily on an Arduino UNO with a 28pin DIL packaged ATMEGA328P (which does not have the pins for A6 and A7).

It looks like analogRead() will accept 0 to 7 and map that to A0 to A7.

Also it looks as if there is no difference between the chip in the 28pin DIP package on the UNO and that in the 32pin TQFP package on the Nano. As both behave the same. A7 reads the voltage applied to it or floats to a value near that applied to A0 if not connected (as on the UNO).