Skip to content

Instantly share code, notes, and snippets.

@urish
Created December 10, 2018 05:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save urish/005d29dcb213bc6365d95b8295ce5551 to your computer and use it in GitHub Desktop.
Save urish/005d29dcb213bc6365d95b8295ce5551 to your computer and use it in GitHub Desktop.
#include <Adafruit_NeoPixel.h>
#define NUM_LEDS 6
#define LEDS_PIN 9
/* define pins for the switches */
#define SWITCH1 4
#define SWITCH2 5
Adafruit_NeoPixel leds = Adafruit_NeoPixel(NUM_LEDS, LEDS_PIN, NEO_GRB | NEO_KHZ800);
long randNum;
void setup()
{
Serial.begin(9600);
leds.begin();
leds.show(); // Turn all LEDs off
// Configure switches to pull-up
pinMode(0, INPUT);
// Analog input pin 0 is unconnected
randomSeed(analogRead(0));
pinMode(SWITCH1, INPUT);
digitalWrite(SWITCH1, HIGH);
pinMode(SWITCH2, INPUT);
digitalWrite(SWITCH2, HIGH);
}
int counter = 0;
int prevLed = 0;
void loop()
{
int ledIndex = counter++ % NUM_LEDS;
bool switch1State = !digitalRead(SWITCH1);
bool switch2State = !digitalRead(SWITCH2);
randNum = random(300);
Serial.println(randNum);
if (switch1State) {
leds.setPixelColor(ledIndex, 0xff, 0, 0); // red
leds.show();
delay(200);
}
if (switch2State) {
leds.setPixelColor(ledIndex, 0, 0xff, 0); // green
} else {
leds.setPixelColor(ledIndex, 0xff, 0xff, 0); // yellow
}
leds.setPixelColor(prevLed, 0, 0, 0);
leds.show();
delay(200);
prevLed = ledIndex;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment