fernpoint

Enthused with the recent Arduino control of the staging yard, I decided to strike while the iron was hot and try another Arduino project. I'm not very good at learning stuff for the sake of learning (like C programming) without an end goal, so I needed an idea and an arc welding simulator fits the bill.

Welding simulators are common, but I after a quick study of 'prototype' arc welding, I realized there was more to the project than first appeared.
The cycle consists of first "striking" an arc by stroking the welding rod (rather like striking a match), along the metal on the job to get an arc running. Trickier than it sounds, since the arc should fire up at the point the weld should begin.  This can take a few attempts depending on how expert the operator is. Experts actually "tap" rather than "strike", but its easier to "freeze" the rod to the work using this method

Then the weld takes place with a lot of flashing in both the white and blue spectrums.

Finally when the welding rod is removed and the arc stops, the metal is red hot and cools rapidly.

I strapped 3 LEDs (white, blue and red) together and connected them to Pro Mini Arduino, before stuttering through the creation of a suitable sketch to make it work.
I needed a way to flash both white and blue LEDs simultaneously at different frequencies. I am an Arduino novice so this took a while, but I discovered the joy of a thing called a "finite state machine", which allows an Arduino to multitask. This is very much a first attempt and the experts among you may throw their hands up in horror at the code. But it works and it was great fun creating it and I have learned a lot.

Everything is randomly based, but fully configurable:

  • Length of time between welding sessions
  • Number of "strike" attempts
  • Length of "strikes"
  • Length of welding session
  • Flash rate/intensity of white and blue components
  • Rate of cooling glow

The finished product will find it's way into Paxman Engineering at Mortimer and once installed I'll post a video of the result.
Here's a very short video showing  a couple of welding cycles (intervals shortened from that when installed for "real"):
 


The Sketch looks like this:

// These are the possible states that are used to control how the arc welding flows
#define S_IDLE 1
#define S_STRIKE_ARC 2
#define S_WHITE_ARC 3
#define S_BLUE_ARC 4
#define S_RED_GLOW 5
#define S_TURNOFF 6

static int state_main_control = S_IDLE; // initial state is idle.
static int state_blue_arc_control = S_IDLE; // initial state is idle.
static unsigned long ts;  // To store the "current" time.
static unsigned long wait;  // To store the wait time  for delays.

void setup()
{ //Nothing to set up here!
}
void loop()
{
  static int ledPin_white_arc = 04; // White LED on pin 4
  pinMode(ledPin_white_arc, OUTPUT);
  static int ledPin_blue_arc = 02;  // Blue LED on pin 2
  pinMode(ledPin_blue_arc, OUTPUT);
  static int ledPin_red_glow = 03;  // Red LED on pin 3
  pinMode(ledPin_red_glow, OUTPUT);

  switch (state_main_control)// Main control state manages the arc welding cycle
  {
    case S_IDLE:
      ts = millis();  // Remember the current time
      wait = random(1000, 5000); //Set a wait time before welding cycle starts.
      state_main_control = S_STRIKE_ARC; // Move to the Arc "striking" state.
      break;

    case S_STRIKE_ARC: // Simulates the striking needed between welding rod and work to start an arc
      if (millis()> ts + wait)//Start the arc strikes when the random delay is exceeded
      {
        int strikes = random (2,5);//random number of strikes  to start the arc
        for (int i = 0; i< strikes; i++)
        {
        digitalWrite(ledPin_white_arc, HIGH ); // Turn the white arc LED on for first attempt
        delay(random (80,120)); // strike the arc
        digitalWrite(ledPin_white_arc, LOW ); // Turn the white arc LED off
        delay(random (200,600)); // delay before next attempt
        }
        digitalWrite(ledPin_white_arc, LOW ); // Turn the white arc LED off in preparation for full arc
        ts = millis(); // Remember the current time
        wait = random(5000, 10000); //Set a random time for now long the arc welding cycle to run
        state_main_control = S_WHITE_ARC; // Move on to next state
        state_blue_arc_control = S_BLUE_ARC; // start up a simultaneous blue arc with the white arc
      }
      break;

    case S_WHITE_ARC: // main flashing of white arc.
      if (ts + wait> millis())// provided random time not exceeded, then keep welding.
      {
        digitalWrite(ledPin_white_arc, HIGH); // set the Arc LED off
        delay(random(60));
        digitalWrite(ledPin_white_arc, LOW); // set the Arc LED on
        delay(random(200));
        break;
      }
      ts = millis(); // Remember current time
      wait = random(5000, 10000); //Set a random time for the weld glow to run
      state_main_control = S_RED_GLOW; // Move on to next state
      state_blue_arc_control = S_IDLE;       // Stop the blue element of the arc
      break;

    case S_RED_GLOW:// Simulates the cooling of the work from red hot after the arc is stopped using an analogue write
      if (ts + wait> millis())
      {
        for (int i = 50; i> 0; i--) { //decrease i with 1
          analogWrite(ledPin_red_glow, i);
          delay(100);
        }
        state_main_control = S_TURNOFF;
        break;
      }
    case S_TURNOFF:
      digitalWrite(ledPin_red_glow, LOW); // Kill the last bit of analogue glow
      state_main_control = S_IDLE;
      break;
  }
  switch (state_blue_arc_control)// Separate state machine running blue arc alongside white arc at different flash frequency
  {
    case S_BLUE_ARC:
      digitalWrite(ledPin_blue_arc, HIGH); // set the Arc LED on
      delay(random(10));
      digitalWrite(ledPin_blue_arc, LOW); // set the Arc LED off
      delay(random(100));
      break;
  }
}

Rob Clark
Cornhill & Atherton RR
 

Reply 0
Pelsea

Nicely done!

I like computer programs were the coder actually studies the operation rather than just copy everyone else. Like railroad modeling, really.

pqe

Reply 0
fernpoint

Mr Paxman is busy with his arc welder

Thanks pqe .....

The Arduino unit has been installed in Paxman Engineering :

The transitions from arc striking to welding and cooling show up better here, also the mix of blue and white light during the weld phase.
If I'm honest I guess for a welding simulator it's a bit of overkill, but great fun nonetheless .........

I ordered an SD card socket reader for the Arduino today, so I can experiment with adding sound to projects

Rob Clark
Cornhill & Atherton RR

Reply 0
Analogbeatmaker

I disagree...

The arc welder isn't overkill...it's overcool! 

Reply 0
jeffshultz

If you ever get tired of it being an arc welder....

...it would do well as a mad scientist's laboratory. 

orange70.jpg
Jeff Shultz - MRH Technical Assistant
DCC Features Matrix/My blog index
Modeling a fictional GWI shortline combining three separate areas into one freelance-ish railroad.

Reply 0
splitrock323

Great idea, both welder AND mad scientist.

Neat adaptation of the arduino. Many railroad shops and online industries could use this bit of animation. Plus Jeff brought up the mad scientist, who could use this in any building on the outskirts of town. Would be neat to see it working in just the basement windows. Muuaaah ha ha ha ha...   

Thomas W. Gasior MMR

Modeling northern Minnesota iron ore line in HO.

YouTube: Splitrock323      Facebook: The Splitrock Mining Company layout

Read my Blog

 

Reply 0
Jeff Youst

"Bwahahahahaha..."

"Dr. TzZaPps Mad Engineering...Scienting on Request"

Jeff

Jeff 
Erie Lackawanna Marion Div.
Dayton Sub 1964
ellogo2.gif 
Reply 0
Ron Ventura Notace

Coming from complete ignorance...

I know absolutely nothing about Arduino programming or C. I have however programmed in a number of languages over the years (COBOL, Fortran, Java, even assembler). I have a question about your sketch. At the top of your loop(), you set up the pins for the LEDs. Do you need to do this every time through the loop, or could that code be in the setup()?

This is in no way a criticism. I think what you've done here is really cool. Just interested from a coding perspective. 

Ron Ventura

Melbourne, Australia

Reply 0
fernpoint

Frankenstein and a theoretical sound synchronization question

Well thank you Analogbeatmaker, Thomas and the Jeffs. When my good lady saw this for the first time she also said "Where's the mad scientist?"   I just need an animation function to get a Frankenstein figure staggering out of the door
 
Ron - I too was uncomfortable about the position of the pin definitions, but if I put them in the obvious place (void setup) then I get a compilation error saying that  ledPin_white_arc  not defined in the scope , bizarrely pointing to the third case - S_White_arc.

The current code works, but it's clearly inefficient and  I'm sure there is a better/correct way to do this. Perhaps an expert out there can explain what I am doing wrong?.

Whilst on the subject of coding help ........

As I mentioned earlier, I have an SD card module on order and I want to try adding a welding sound to the unit - obviously synchronized to the LEDs.
The first thing I was going to try was adding an extra "state machine"  to manage the .wav file extraction and speaker send. My concern is that the sound playback will 'stutter' because the Arduino is having to manage white and blue LED flashing in conjunction with the sound output. I may get away with this here because of the naturally rough sound of a weld, but for other projects this probably wouldn't work.

The second strategy would be to manage the sound playback with a second dedicated Arduino, but trigger the sound start and end from the arc welding control Arduino (I don't know how to do this yet, but I'm sure a swift bit of research will reveal the answer.

So to the question - Is there a "standard" way of managing sound and other event synchronization with the Arduino?

Rob Clark

Reply 0
pierre52

Harry's TV

Rob you have progressed way above my pay grade. However, I suspect you might find some clues to your question in Dr Geoff's code for Harry's TV.

Peter

The Redwood Sub

Reply 0
Kevin Rowbotham

Looks very clean...

Quote:

Rob you have progressed way above my pay grade

Peter

You've gone even higher than my pay grade, Rob!  Well done.

I may be way out in left field, but couldn't each case in your switch be turned into a class and used to sync light and sound?

Thanks for sharing!

Regards,

~Kevin

Appreciating Modeling In All Scales but majoring in HO!

Not everybody likes me, luckily not everybody matters.

Reply 0
Ivan I

It's all about the scope!

// These are the possible states that are used to control how the arc welding flows
#define S_IDLE 1
#define S_STRIKE_ARC 2
#define S_WHITE_ARC 3
#define S_BLUE_ARC 4
#define S_RED_GLOW 5
#define S_TURNOFF 6

static int state_main_control = S_IDLE; // initial state is idle.
static int state_blue_arc_control = S_IDLE; // initial state is idle.
static unsigned long ts;  // To store the "current" time.
static unsigned long wait;  // To store the wait time  for delays.

static int ledPin_white_arc = 04; // White LED on pin 4
static int ledPin_blue_arc = 02;  // Blue LED on pin 2
static int ledPin_red_glow = 03;  // Red LED on pin 3

void setup()
{

pinMode(ledPin_white_arc, OUTPUT);
  pinMode(ledPin_blue_arc, OUTPUT);
  pinMode(ledPin_red_glow, OUTPUT);
}
void loop()
{
//loopy stuff here

}

Rob, here's how I would have done it. The issue you're running into is variable scope - when you declare a variable inside a part of a program (like setup() or loop() or other functions you've made) it is limited to just being used inside that part of the program. The way out of that, in arduino world, is to create global variables that are defined above the setup() function. This way you can use those variables throughout the entirety of your program. You could also pass variables as parameters in your function calls (ie: pinMode(variablePin, variableState) ), but that doesn't quite work with the existing functions setup() and loop(). 

With my changes, I've moved the variable declaration up into the global scope. I put the pinMode() initialization lines in the setup - these only have to run once. Just to reiterate, a variable will only be accessible in the scope in which you declare it. For static variables that don't change, it makes sense to declare them globally. 

Hope some of this helps, nice to see more people embracing the fun of microcontroller animation in model railroading.

 

Reply 0
Kevin Rowbotham

Compiled!

Quote:

Sketch uses 2,398 bytes (7%) of program storage space. Maximum is 32,256 bytes.
Global variables use 25 bytes (1%) of dynamic memory, leaving 2,023 bytes for local variables. Maximum is 2,048 bytes.


I'll have to wire this up to test but the original code with suggested changes compiled first time.

Elegant!

Regards to you both,

~Kevin

Appreciating Modeling In All Scales but majoring in HO!

Not everybody likes me, luckily not everybody matters.

Reply 0
Jim at BSME

Not multi-tasking

This is an awesome welder, and perfect use of a state machine as a sequence of events happen, but it is not multi-tasking.

Multitasking allows two or more tasks to happen at the same time, which have nothing to do with each other, e.g. running your word processor and a calculator.

If you look at the loop() the blue arc happens the first time after the white strikes have completed and you are in the white_arc state then each time after the white_arc section of the case has completed, it is just that the times are so small we can't perceive that there one arc happens after the other.

More than one way to skin the cat: since the blue arc only happens with the white arc there is no need for a blue arc state machine or control, one could simply change the white arc case/state to:

case S_WHITE_ARC: // main flashing of white arc.
if (ts + wait> millis()) // provided random time not exceeded, then keep welding.
{
digitalWrite(ledPin_white_arc, HIGH); // set the Arc LED on
delay(random(60));
digitalWrite(ledPin_white_arc, LOW); // set the Arc LED off
delay(random(200)); // and now the blue component of the arc
digitalWrite(ledPin_blue_arc, HIGH); // set the Arc LED on
delay(random(10));
digitalWrite(ledPin_blue_arc, LOW); // set the Arc LED off
delay(random(100));
break;
}
ts = millis(); // Remember current time
wait = random(5000, 10000); //Set a random time for the weld glow to run
state_main_control = S_RED_GLOW; // Move on to next state
state_blue_arc_control = S_IDLE; // Stop the blue element of the arc
break;

In doing this code change I noticed the comments for the white are are reversed, says off when on and on when off, I corrected them here.

- Jim B.
Baltimore Society of Model Engineers, Estd. 1932
O & HO Scale model railroading
Check out BSME on: FacebookInstagram
Reply 0
fernpoint

"The Burbs"

Peter - thanks, I'll take a look at  Harry's TV. I remember seeing it for the first time and being hugely impressed, but the technology flew over my head; I think because at the time I had no obvious use for it. As they say "Necessity is the mother of invention" - in this case learning.

Kevin - yes I think classes/ OO techniques could be used. I just haven't learned how to do it yet. My intention on the next project is to go the OO route and compare the advantages with the state machine approach - the two can probably can be combined - all will unfold as I learn more,I hope

Thanks for the code tip Ivan. It worked first time (as Kevin found) and once I saw what you had done it seemed blindingly obvious - as do many things when someone shows you how to do it!

Jim B - I appreciate what you are saying about multi-tasking. The Arduino appears to be a very simple processor in the grand scheme of things and I would have been more accurate by saying "simulation" of multi-tasking.

As you say, many ways to skin a cat and I could have written the code  in-line since the arc welding is a very simple repeating sequence. It was more of a learning exercise to get "State Machine" concept running to solve an easy problem so I could grasp the principles.
My understanding of FSM is that its power is realized when you have external inputs that can influence the flow of logic through the different states - mapping this first with state diagrams and then following up with code. Baby steps for me at this point ......
Thanks for pointing out the comment error. It made me check and I found a few more .
My excuse is the excitement of getting things running precludes the good practice of cleaning up comments that refer to earlier iterations. Recipe for difficulty - especially if I post the code and others use it - lesson learned.

Thanks to all for your comments and assistance - when the SD card adaptor arrives I will post how I get on with adding sound to the welder.

Rob Clark

PS - Thomas "Would be neat to see it working in just the basement windows. Muuaaah ha ha ha ha..."   
I was wracking my brains trying to remember which film this reminds me of and I just got it - "The Burbs" with Tom Hanks. I just need to change my blue LED for a yellow and I'm there ......

%20Burbs.png 

Reply 0
Kevin Rowbotham

Multitasking, Burb's and other matters...

Quote:

This is an awesome welder, and perfect use of a state machine as a sequence of events happen, but it is not multi-tasking.

Agreed, but we can get closer to that goal with OOP, timers and interrupts, while on the Arduino platform  Still, if it's true multitasking we are after, perhaps a Raspberry pi is the platform we want?

In my experiments to date I've got an Arduino Promini controlling 8 relays and three servos while still accepting input from a switch.  There is no code running in the loop so I could do something else there.  It sure seems like multitasking to me but I am very excited about beginning to understand it so please don't quote me! )

Quote:

Thanks for the code tip Ivan. It worked first time (as Kevin found) and once I saw what you had done it seemed blindingly obvious - as do many things when someone shows you how to do it!

Rob, I had the same aha moment.  I knew something was out of place, but I wasn't sure where it needed to go either.  Ivan made a light bulb go on when he mentioned the global variable!  Thanks indeed!

Quote:

Thanks to all for your comments and assistance - when the SD card adaptor arrives I will post how I get on with adding sound to the welder.

Rob Clark

Thanks for starting this discussion and sharing your work with the rest of us trying to learn advanced coding on the Arduino!  When you go to add sound, don't forget the regular metallic clanging of the hammer as the welder knocks flash off his new welds.

 

The Burb's....great movie.  Bruce Dern was classic!

Best regards,

~Kevin

Appreciating Modeling In All Scales but majoring in HO!

Not everybody likes me, luckily not everybody matters.

Reply 0
fernpoint

It speaks!

Thanks Kevin - I consider this very much an "amateur's journey", so you get to see some failures leading to a hopefully successful conclusion.

Progress has been slow due to multitasking (real world multitasking ) but the next stage has been successfully reached:

Stg44.jpg 
Here we have an Arduino playing sounds from a micro SD card. I can load and access multiple sound samples and manage quality and playback, albeit with very low volume due to direct (actually via a 100 ohm resistor) connection of a speaker.
A LM386 Amplifier module is in the post and that will solve the volume issues.

I have made the decision that attempting to play the sounds directly from the Arc Welder Arduino would be impractical. Sound reproduction uses a fair bit of computing power and expecting the "state machine" to cope with these simultaneous tasks is unrealistic. So the next step is to trial a master/slave relationship between two Arduinos so that a dedicated slave machine can manage the playback tasks under simple file select/start position/end position/playback commands from the master.
It's also a template for other ambient sound projects in the future.

It looks fairly straightforward from the small amount of research I have done. I'll get back to you with the results of the next experiment.

Rob Clark
Cornhill and Atherton RR

Reply 0
Pelsea

Sound card?

Which sound card are you using?

pqe

Reply 0
emdsd9

Pelsea, Might Rob get around

Pelsea,

Might Rob get around the multi tasking of sound and light by moving to a Raspberry?

John

Reply 0
fernpoint

Not a Marshall amp ......

Hi pqe

"Sound card" is probably stretching it - It's one of these:
Stg45.jpg 
Couple of dollars ........

John - I have no doubt that a Raspberry Pi would open up many possibilities and options, but I'm following a technically simpler path for now (and I have quite a few Pro-Minis lying about )

Rob Clark
Cornhill and Atherton RR

Reply 0
Jim at BSME

Pi versus Arduino and sound "card"

John while Rob could use the Pi, the cost would almost be triple, even though a Pi Zero is only $5, there are accessories needed to run it, a mirco SD card, power supply, and a couple cables. The starter kit on Adafruit is $24.95, so much for the $5 computer.

Rob I believe pqe, was asking for which board are you using to play back the sounds, not for the amp.

- Jim B.
Baltimore Society of Model Engineers, Estd. 1932
O & HO Scale model railroading
Check out BSME on: FacebookInstagram
Reply 0
fernpoint

Sound board

Hi Jim (and pqe)
I'm probably about to show a bout of ignorance here, but I was planning to only use the TMRpcm library for simple wav file reproduction - no sound board ......

Rob

Reply 0
Geoff Bunza geoffb

Sound Board

Hi Rob,

Try a DFPlayer for sound generation-- you will be much happier. It has an on board micro SD slot and interface too. This is cheap, easy to use, and Arduino compatible. You can get more details in the upcoming MRH Dec issue.

20Module.jpg 

User Instructions here: /sites/model-railroad-hobbyist.com/files/users/geoffbfiles/DFPlayer%20Sound%20Module%20Manual.pdf

Arduino Library here: /sites/model-railroad-hobbyist.com/files/users/geoffbfiles/DFPlayer_Mini_Mp3.zip

It can drive an 8 Ohm speaker directly (mono out) or a stereo line output to an amplifier.

Have fun! 
Best regards,
Geoff Bunza

Geoff Bunza's Blog Index: https://mrhmag.com/blog/geoff-bunza
More Scale Model Animation videos at: https://www.youtube.com/user/DrGeoffB
Home page: http://www.scalemodelanimation.com

Reply 0
Pelsea

Interesting...

A simple solution for a simple problem. Several Arduino boards triggering each other is a logical way to go, your own private network o' stuff. I eagerly await results.

My own sound experiments were with fancier cards, but not particularly awesome. I've just ordered an Adafruit FX sound board, which seems to have everything needed for a sound cue player, ready to roll.

pqe

Reply 0
jappe

Ok...

...now I need some potato peels for my welders' eyes...

Jappe

CEO, U.P.-Willamette Valley Sub aka U.P.-Eureka & Willamette Valley Branch

----------------------------------Ship it now, Ship it right---------------------------------------------

                                        age(42).jpeg 

Don't ride behind me, I will not lead you, don't ride in front of me, I will not follow you, just ride next to me and be my bro......

Reply 0
Reply