help with code (battery tab spot welder)

So im new to Arduino and well code in general. What i have so far is a uno R3 starter kit and also a 2560 mega. Well what i need is to come up with a code so i can press a microswitch and it will only turn on for 333ms then turn off. when its on for that time it will be triggering a relay to on. so all it does is come on for 333ms then off even if you continue to hold the microswitch button down. once you release the button and its kicked the relay on for 333ms it will wait for the button to be pressed and then repeat.(Its for a battery tab spot welder). now i know that thihanks s is most likely simple but its been stumping me for a week. I also know simple delays wont work. I believe millis timer is where i need to be and ive worked on it but just cant figure it out. so ive been using dp 2 for switch and dp9 for relay only because dp13 can cause a relay to fire when you dont want it to so i avoided that pin. Any help will be greatly appeciated. Thanks 01sensei

Also the more i mess with the code the more i seem to mess it up.
Code is really bad i know forgive me im just trying to learn.

const int buttonPin = 2; // the number of the pushbutton pin
const int relayPin = 9; // the number of the relay trigger

// variables will change:
int buttonState = digitalRead(buttonPin);
int lastButtonState = (buttonPin, HIGH);
int relayPinState = digitalRead(ledPin);
int lastRelayPinState = (relayPin, HIGH);

void setup() {
// initialize the LED pin as an output:
pinMode(relayPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}

void loop()
{
if (buttonState != lastButtonState);
{
if (buttonState == HIGH);

if (relayPinState != lastRelayPinState);

if (relayPinState == HIGH);
{
delayMicroseconds(333);
digitalWrite(buttonPin, LOW); // Set buttonpin to low after 333ms
}
}
}

if (buttonState != lastButtonState);

That ; forms the body of the if statement. It's a no-op. Probably not what you want.

Look at the state change detection example. Use a delay() (ugh!) when the switch becomes pressed, after turning the relay on. The relay can't be activated again or deactivated until the time expires. Doing nothing when the switch becomes released is OK (and is what you want).

Edit: You need to read the pin states in loop(), not just once at compile time.

Still cant figure this out wish they made a example code just like it so i could see it and then figure out exactly whats going on. i think i need to maybe start over

It looks like you have ms, milli seconds, confused with micro seconds.
1/1000 second is millisecond (ms)
1/1000000 second is micro second (us)
how long do you actually want the relay held active?

jackwp

i want it to stay active for (333) about 1/3 of a second

i want it to stay active for (333) about 1/3 of a second

OK, that is 333 ms. or you can just use the delay(333), to wait, about 1/3 second.
Many ways to code, but if I was me, I think I would create a function called keyPressed(), and call that function when the key is pressed. Inside that function you can turn on your relay, delay for some time delay(333);, turn the relay off, delay for some more time, then return (back to the main loop) where another key pressed is looked for.
Does that make any sense?

jackwp,
i think i get what your saying. and its true many many ways to code and i think im gonna take your approach and start simple then refine what im doing to the code better. now off to reading alot more examples so i can get this working

i think i need to maybe start over

I think that is a good idea. Save your old sketch, you may want to refer to it, but start a new sketch.
And occasionally, use Auto Format, under Tools on the menu bar.
Let us now how it is going.

So i started over and while its not perfect it does work and it works pretty well.

// set pin numbers:
const int pedalPin = 2;     // the number of the pedal switch pin
const int relayPin =  9;      // the number of the Relay pin

// variables will change:
int pedalState = 0;         // variable for reading the pedal switch status

void setup() {
  // initialize the Relay pin as an output:
  pinMode(relayPin, OUTPUT);      
  // initialize the pedal switch pin as an input:
  pinMode(pedalPin, INPUT);     
}

void loop(){
  // read the state of the pedal switch value:
  pedalState = digitalRead(pedalPin);

  // check if the pedal switch is pressed.
  // if it is, the pedalState is HIGH:
  if (pedalState == HIGH) {
    delay (500);
    // delays button for 500ms to give you time to release foot pedal
    // turn LED on:    
    digitalWrite(relayPin, HIGH);
    delay (333); 
    // sets relay to high for 333ms 
  } 
  else {
    // turn Relay off:
    digitalWrite(relayPin, LOW); 
  }
}

That is great, looks good.
now can I ask another question? Do you think the contacts on the relay will bet pitted, welded? How much current is being required, and what current are the relay contacts rated at? Do you think that maybe a high power mos fet would hold up better than a relay. Also since a relay is a mechanical device, with time delays, that 333 millisecond will probably not be as accurate with a relay. Just some thoughts I had.
Good job coding!

2 comments:

  • You may want to turn on the pull-up for the footswitch if you don't have an external one.
  • Get into the habit of using de-bounce since you may have some when you take your foot off the switch.

I haven't messed with mosfets yet. But i got a 20a relay with 12v coil and also a crydom 10a solid state relay to work with. I was going to use a 2n2222a transistor but scraped that for a tip120 darlington transistor(seems to be a better fit). Its not all set in stone yet but im getting closer. Can you point me in a direction for mosfets and how to wire them for such a application. I do know that mosfets will need to be heat sinked for somethink like this. P.S. Only reason i have crydom relays is i got a awesome deal for them only paid .99 cents each. But kinda want to keep them open for a project that would be better suited for them.( Maybe a coin opperated xbox 360)

Thanks for everyones help i spent a week trying to figure this out and posted here and only took a hour to sort out.

I've got a 10k pull up on my breadboard but will look into internal pull up useage.. Also got to learn De-bounce Thanks for the heads up.

Well, you still didn't include info about the weld current, and voltage needed. That would be helpful to suggest a driver circuit for it. I am not familiar with a (battery tab spot welder).

its just a microwave transformer that has new secondaries wound on it. it takes 110v in and then does 4-6v out at around 9amps.

Oh, It is ac, not dc? About 40 to 60 watts the I guess.
Mos fet will not be good for ac I don't think.

Is your ssr (solid state relay ac or dc)? If it is ac, it may be a good choice for a driver.

the ssr is a 120v relay
Crydom D1210

I've done some reading on just these devices myself in the past few weeks. From what I read, you may not be planning to put enough energy into forming the nugget between the two sheets of metal.

The figure I see commonly quoted is 200 Joules, or to put it another way - Volts * Amps * Seconds = 200
Obviously, this would be useless if the delivery time was (for instance) 200 seconds - 1 Watt for 200 seconds. I've used camera flash capacitors in the past to dump about 100uF or 200uF of ~325v into a 10 ohm load - yeah, it was spectacular - a peak of around 10,000 Watts - The thing would easily vaporize ~8mm of the thin nichrome wire used in electric blankets. Not bad for something powered by a single AA cell, the whole device fitted in a jeans pocket.

You may need to look into either (a) rewinding the secondary on the MOT with fewer turns of thicker wire (I hear car battery jumper leads are ok) or using capacitive discharge. I'd try which ever was going to be the easiest/cheapest while still concentrating the energy transfer enough that the nugget is formed between the two sheets.

Here's the last page I was reading on the things: Zero Emission Vehicles Australia

Well, 120 volt does not really say if it is ac, or dc. There are both kinds, and they can not be switched.
Do you have a URL where it came from?
If not, what are the symbols on the two output terminals? + and - will indicate dc. ~ and ~ will indicate ac.

@enhzflep does the welder need to be low voltage/high amp, or just high watts?
I have some supercapacitors with about 2000 farid, and wired up as 12 volts, they seemed pretty hot. But if the OP is using AC, that would not be usable. Is it different using ac or dc?