Skip to content

Instantly share code, notes, and snippets.

@kcranley1
Last active May 19, 2016 09:52
Show Gist options
  • Save kcranley1/81eedc31b3e0e31f15cb199310f8ef7e to your computer and use it in GitHub Desktop.
Save kcranley1/81eedc31b3e0e31f15cb199310f8ef7e to your computer and use it in GitHub Desktop.
A method of achieving control of individual LEDs connected to the Arduino utputs
// Written by Adafruit. https://learn.adafruit.com/multi-tasking-the-arduino-part-1/a-classy-solution
// Modified by S&S
// A method of achieving control of individual LEDs connected to the Arduino outputs
//
class Flasher
{
// Class Member Variables
// These are initialized at startup
int ledPin; // the number of the LED pin
long OnTime; // milliseconds of on-time
long OffTime; // milliseconds of off-time
// These maintain the current state
int ledState; // ledState used to set the LED
unsigned long previousMillis; // will store last time LED was updated
// Constructor - creates a Flasher
// and initializes the member variables and state
public:
Flasher(int pin, long on, long off)
{
ledPin = pin;
pinMode(ledPin, OUTPUT);
OnTime = on;
OffTime = off;
ledState = LOW;
previousMillis = 0;
}
void Update()
{
// check to see if it's time to change the state of the LED
unsigned long currentMillis = millis();
if((ledState == HIGH) && (currentMillis - previousMillis >= OnTime))
{
ledState = LOW; // Turn it off
previousMillis = currentMillis; // Remember the time
digitalWrite(ledPin, ledState); // Update the actual LED
}
else if ((ledState == LOW) && (currentMillis - previousMillis >= OffTime))
{
ledState = HIGH; // turn it on
previousMillis = currentMillis; // Remember the time
digitalWrite(ledPin, ledState); // Update the actual LED
}
}
};
Flasher led13(13, 160, 1600);
Flasher led12(12, 150, 1500);
Flasher led11(11, 140, 1400);
Flasher led10(10, 130, 1300);
Flasher led9(9, 120, 1200);
Flasher led8(8, 110, 1100);
Flasher led7(7, 100, 1000);
Flasher led6(6, 90, 900);
Flasher led5(5, 80, 800);
Flasher led4(4, 70, 700);
Flasher led3(3, 60, 600);
Flasher led2(2, 50, 500);
void setup()
{
}
void loop()
{
led2.Update();
led3.Update();
led4.Update();
led5.Update();
led6.Update();
led7.Update();
led8.Update();
led9.Update();
led10.Update();
led11.Update();
led12.Update();
led13.Update();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment