Skip to content

Instantly share code, notes, and snippets.

@chrwei
Last active March 19, 2018 21:35
Show Gist options
  • Select an option

  • Save chrwei/0f008e922d04369f39b283c91912ddb5 to your computer and use it in GitHub Desktop.

Select an option

Save chrwei/0f008e922d04369f39b283c91912ddb5 to your computer and use it in GitHub Desktop.
Bright Ideas 2017 Arduino code
int pos = 0, dir = 1; // Position, direction of "eye"
unsigned long lastMillis = 0;
int leds[] = {3,5,6,9,10,11};
#define NUMBER_OF_LEDS (sizeof(leds)/sizeof(int))
void setup(){
//configure LED outputs and turn them off
for(int i=0; i < NUMBER_OF_LEDS; i++) {
pinMode(leds[i], OUTPUT);
digitalWrite(leds[i], LOW);
}
pinMode(2, INPUT_PULLUP);
}
void loop(){
if (lastMillis + 150 <= millis() && digitalRead(2) == HIGH) {
pos += dir;
if(pos == -2) {
dir = -dir;
} else if(pos == NUMBER_OF_LEDS+1) {
dir = -dir;
}
for(int i=0; i < NUMBER_OF_LEDS; i++) {
if(i==pos-1 || i==pos+1) {
analogWrite(leds[i], 16);
} else if(i==pos) {
analogWrite(leds[i], 255);
} else {
digitalWrite(leds[i], LOW);
}
}
lastMillis = millis();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment