Skip to content

Instantly share code, notes, and snippets.

@futureshocked
Created April 20, 2016 21:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save futureshocked/d9b84613e0c14230369afd0bade9f5c5 to your computer and use it in GitHub Desktop.
Save futureshocked/d9b84613e0c14230369afd0bade9f5c5 to your computer and use it in GitHub Desktop.
/*
* multitasking_demo
* A sketch written to help people understand how to implement basic multitasking on the Arduino
* Written by Peter Dalmaris, April 21 2016.
* Arduino Tips and Tricks
* txplore.com
*
* Requirements: An Arduino, two LEDs, two 220Ohm resistors, a breadboard, wires.
* Play around with the variables in lines 18 to 26 to create different light show patterns.
*/
const int ledPinA = 9; // the number of the LED A pin
const int ledPinB = 8; // the number of the LED B pin
int ledStateA = LOW; // ledState used to set the LEDA
int ledStateB = LOW; // ledState used to set the LEDB
unsigned long previousMillisA = 0; // will store last time was updated for LEDA
unsigned long previousMillisB = 0; // will store last time was updated for LEDB
const long start_cycle_A = 0; // This is the start of the ON/OFF cycle for LEDA
const long start_cycle_B = 100; // This is the start of the ON/OFF cycle for LEDB,
// set to 5ms after the start of LEDA cycle.
// You can also think of these variables as the offset
// from the millis "start".
const long intervalA_ON = 500; // interval at which to turn LEDA ON (milliseconds)
const long intervalA_OFF = 700; // interval at which to turn LEDA OFF (milliseconds)
const long intervalB_ON = 98; // interval at which to turn LEDB ON (milliseconds)
const long intervalB_OFF = 250; // interval at which to turn LEDB OFF (milliseconds)
void setup() {
pinMode(ledPinA, OUTPUT);
pinMode(ledPinB, OUTPUT);
}
void loop() {
unsigned long currentMillis = millis();
// Is it time start the LEDA cycle? If yes, turn the LED on
// and update the previousMillisA variable.
if (ledStateA == LOW && (currentMillis - previousMillisA >= intervalA_OFF + start_cycle_A)) {
previousMillisA = currentMillis;
ledStateA = HIGH;
digitalWrite(ledPinA, ledStateA);
}
// Is it time to turn LEDA off? if yes, turn it off an update the previousMillisA variable
if (ledStateA == HIGH && (currentMillis - previousMillisA >= intervalA_ON + start_cycle_A)) {
previousMillisA = currentMillis;
ledStateA = LOW;
digitalWrite(ledPinA, ledStateA);
}
// Is it time start the LEDB cycle? If yes, turn the LED on
// and update the previousMillisB variable.
if (ledStateB == LOW && (currentMillis - previousMillisB >= intervalA_OFF + start_cycle_B)) {
previousMillisB = currentMillis;
ledStateB = HIGH;
digitalWrite(ledPinB, ledStateB);
}
// Is it time to turn LEDB off? if yes, turn it off an update the previousMillisB variable
if (ledStateB == HIGH && (currentMillis - previousMillisB >= intervalA_ON + start_cycle_B)) {
previousMillisB = currentMillis;
ledStateB = LOW;
digitalWrite(ledPinB, ledStateB);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment