Skip to content

Instantly share code, notes, and snippets.

@Krelborn
Created February 12, 2014 19:48
Show Gist options
  • Save Krelborn/8963140 to your computer and use it in GitHub Desktop.
Save Krelborn/8963140 to your computer and use it in GitHub Desktop.
My first Arduino program
/*
Knight Rider
Strobe a set of LEDs like the Knight Rider car!
*/
// Number of LED in our array
enum { kLedCount = 8 };
// Array of pins controlling LEDs
int ledArray[] = { 6, 7, 8, 9, 10, 11, 12, 13 };
// Currently lit LED
int currentLed = 0;
// Increment to move to the next LED
int increment = 1;
// Delay for moving between LED. Set to cycle once a second.
int delayMilliseconds = 1.0/2.0 * 1000.0/8.0;
//
// setup
//
void setup()
{
// Initialize the LED pins as outputs
for(int i = 0; i < kLedCount; i++)
{
pinMode(ledArray[i], OUTPUT);
}
}
//
// loop
//
void loop()
{
// Set the current LED to HIGH, wait for the delay before going low and
// moving onto the next LED in the sequence.
digitalWrite(ledArray[currentLed], HIGH);
delay(delayMilliseconds);
digitalWrite(ledArray[currentLed], LOW);
currentLed += increment;
// If we're at the first or last LED then switch direction by inverting
// the increment.
if ((currentLed == (kLedCount - 1)) || (currentLed == 0))
{
increment = -increment;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment