Go Down

Topic: help with code (battery tab spot welder) (Read 16993 times) previous topic - next topic

01sensei

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
}
}
}

PaulS

Code: [Select]
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.
The art of getting good answers lies in asking good questions.

01sensei

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

jack wp

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?

01sensei

jackwp

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

jack wp

Quote
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?

01sensei

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

jack wp

Quote
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.

01sensei

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


Code: [Select]
// 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);
  }
}



jack wp

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!

larryd

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.
No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

01sensei

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.

01sensei

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.

jack wp

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).

01sensei

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.

Go Up