Skip to content

Instantly share code, notes, and snippets.

@jcsalterego
Created September 5, 2011 06:35
Show Gist options
  • Select an option

  • Save jcsalterego/1194244 to your computer and use it in GitHub Desktop.

Select an option

Save jcsalterego/1194244 to your computer and use it in GitHub Desktop.
/**
* Binary Counter with 4 LEDs
*
* pin 8 -> 220 ohm -> green LED -> GND
* pin 9 -> 220 ohm -> yellow LED -> GND
* pin 10 -> 220 ohm -> red LED -> GND
* pin 11 -> 220 ohm -> blue LED -> GND
* pin 2 <- button <- 10K ohm <- GND
* <- +5V
*/
const int lowestPin = 8;
const int highestPin = 11;
const int numPins = highestPin - lowestPin + 1;
const int max_brightness = 120;
const int counterReset = pow(2, numPins); // numPins;
const int buttonPin = 2;
int lastButtonState = LOW;
int buttonCounter = 0;
void setup()
{
pinMode(buttonPin, INPUT);
Serial.begin(9600);
for (int i = lowestPin; i <= highestPin; ++i) {
pinMode(i, OUTPUT);
}
flash(); flash();
Serial.write("begin party");
}
void flash()
{
for (int i = lowestPin; i <= highestPin; ++i) {
analogWrite(i, 200);
}
delay(500);
for (int i = lowestPin; i <= highestPin; ++i) {
analogWrite(i, 0);
}
delay(500);
}
void led_on(int led)
{
return led_set(led, 255);
}
void led_off(int led)
{
return led_set(led, 0);
}
void led_set(int led, int brightness)
{
analogWrite(lowestPin + led, brightness);
}
void loop()
{
int buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState) {
lastButtonState = buttonState;
if (buttonState == HIGH) {
buttonCounter++;
int i;
for (i = 0; i < numPins; i++) {
if ((buttonCounter >> i) & 1 == 1) {
led_on(i);
} else {
led_off(i);
}
}
}
if (buttonCounter > counterReset) {
buttonCounter = 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment