-
-
Save laullon/0aae7cec9c7ec4b2003e1eba9be83206 to your computer and use it in GitHub Desktop.
Non-blocking Object-oriented Arduino Arc Welder Simulator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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