Skip to content

Instantly share code, notes, and snippets.

@laullon
Forked from k4kfh/arduinoWelder.ino
Last active May 30, 2018 09:45
Show Gist options
  • Save laullon/0aae7cec9c7ec4b2003e1eba9be83206 to your computer and use it in GitHub Desktop.
Save laullon/0aae7cec9c7ec4b2003e1eba9be83206 to your computer and use it in GitHub Desktop.
Non-blocking Object-oriented Arduino Arc Welder Simulator
class Welder
{
public:
Welder(int pin);
void loop(bool welding);
private:
int welderLedPin;
unsigned long currentMillis = 0;
unsigned long previousMillisArc = 0;
unsigned long previousMillisFlicker = 0;
int timeArcOff = 3000;
int timeArcOn = 3000;
bool arc = false; //whether there's an arc right now or not
int ledState = LOW;
int timeFlickerOn = 50;
int timeFlickerOff = 170;
};
Welder::Welder(int pin) {
welderLedPin = pin;
pinMode(welderLedPin, OUTPUT);
}
void Welder::loop(bool welding) {
if (welding == true) { //make sure the welder is enabled
unsigned long currentMillis = millis();
if (arc == false && currentMillis - previousMillisArc >= timeArcOff) { //if the arc's off, but been off long enough
arc = true;
previousMillisArc = currentMillis;
timeArcOff = random(3000);
}
else if (arc == true && currentMillis - previousMillisArc >= timeArcOn) { //if the arc's on, but been on long enough
arc = false;
ledState = LOW;
previousMillisArc = currentMillis;
timeArcOn = random(3000);
}
if ( arc == true && currentMillis - previousMillisFlicker >= timeFlickerOn && ledState == HIGH) {
previousMillisFlicker = currentMillis;
timeFlickerOn = random(20);
ledState = LOW;
}
if ( arc == true && currentMillis - previousMillisFlicker >= timeFlickerOff && ledState == LOW) {
previousMillisFlicker = currentMillis;
timeFlickerOff = random(100);
ledState = HIGH;
}
digitalWrite(welderLedPin, ledState);
}
else {
digitalWrite(welderLedPin, LOW);
}
}
Welder welder1(13);
Welder welder2(12);
void setup()
{
}
void loop()
{
welder1.loop(true);
welder2.loop(true);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment