Skip to content

Instantly share code, notes, and snippets.

@k4kfh
Created February 21, 2017 22:38
Show Gist options
  • Save k4kfh/8664a05c0259bb0850df99c5567edcd8 to your computer and use it in GitHub Desktop.
Save k4kfh/8664a05c0259bb0850df99c5567edcd8 to your computer and use it in GitHub Desktop.
Non-blocking Arduino Arc Welder Simulator
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;
const int ledPin = 2;
int timeFlickerOn = 50;
int timeFlickerOff = 170;
bool welding = true;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
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(ledPin, ledState);
}
else {
digitalWrite(ledPin, LOW);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment