Last active
October 29, 2023 17:18
-
-
Save johnwargo/6effb27c2e632674eb3774be9180b88c to your computer and use it in GitHub Desktop.
An even better version of the Arduino random smoke generator
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
#define SMOKE_PIN A0 | |
#define MIN_TIME 1000 | |
#define MAX_TIME 5000 | |
bool isSmoking = false; | |
int timerDuration = 0; | |
unsigned long startTime; | |
void setup() { | |
// configure the output pin to control the smoke generator | |
pinMode(SMOKE_PIN, OUTPUT); | |
// Enable the Arduino device's onboard LED | |
pinMode(LED_BUILTIN, OUTPUT); | |
// start making smoke. You can do this later based | |
// on some other trigger if you want | |
setSmokeState(true); | |
} | |
void loop() { | |
// === | |
// do some stuff | |
// === | |
checkSmokeState(); | |
// === | |
// do some other stuff | |
// === | |
} | |
void setSmokeState(bool smokeState) { | |
uint8_t state = smokeState ? HIGH : LOW; | |
digitalWrite(SMOKE_PIN, state); | |
digitalWrite(LED_BUILTIN, state); | |
isSmoking = smokeState; | |
startTime = millis(); | |
timerDuration = (int)random(MIN_TIME, MAX_TIME); // milliseconds | |
} | |
void checkSmokeState() { | |
// Set `timerDuration` to 0 to stop/disable this process | |
if (timerDuration > 0) { | |
// check to see if enough time's passed since the timer started | |
if ((millis() - startTime) > timerDuration) { | |
// timer expired, so time to switch modes | |
setSmokeState(!isSmoking); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment