DIY SpotWelder for Lithium Battery Pack building

DIY SpotWelder for Lithium Battery Pack building
/*
Disclaimers: The author is not responsible for any misuse of the information contained herein and accepts no responsibility
for any damage caused by the use or misuse of this information.
Credits: This code has been taken from http://www.arduino.cc/en/Tutorial/Debounce and modified for Spot Welding purpose.
*/
int Allow_execution = LOW;
const int buttonPin = 3;   
const int RelayPin = 10;    

int RelayState = LOW;       
int buttonState;           
int lastButtonState = LOW;   
int delayontime = 0;

unsigned long lastDebounceTime = 500;  
unsigned long debounceDelay = 200;    

int period = 40; /** 40ms, changing this will keep the output ON for that many milliseconds */
unsigned long time_now = 0;

void setup() {
  Serial.begin(9600);
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(RelayPin, OUTPUT);

  digitalWrite(RelayPin, RelayState);
  delay(500);
  if(digitalRead(buttonPin) == HIGH)
  {
    Allow_execution = HIGH;
    }
}

void loop() {
  if(Allow_execution == HIGH)
  {

  int reading = digitalRead(buttonPin);

  if (reading != lastButtonState) {
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {

    if (reading != buttonState) {
      buttonState = reading;

      if (buttonState == LOW) {
        RelayState = HIGH;
        time_now = millis();
        delayontime = millis();
      }
    }
  }

  digitalWrite(RelayPin, RelayState);

  if(RelayState == HIGH)
  {
    if(millis() > time_now + period)
    {
     RelayState = LOW;
     delayontime = millis() - delayontime;
     Serial.print(delayontime);
    }
  }
  
  lastButtonState = reading;
  }
}
Functional Flow for Spot Welder – millisecond delay

vu3pyn