Created
February 11, 2013 01:12
-
-
Save terenced/4751829 to your computer and use it in GitHub Desktop.
3-bit binary counter for Arduino
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 3-bit binary counter (since I only have 3 LEDs [^_-]) | |
// Counts from 1 to 7 | |
// Video: https://plus.google.com/116685752536817026018/posts/8BbTWriKykB | |
int led2 = 5; | |
int led1 = 4; | |
int led0 = 3; | |
void setup() { | |
pinMode(led0, OUTPUT); | |
pinMode(led1, OUTPUT); | |
pinMode(led2, OUTPUT); | |
Serial.begin(9600); | |
} | |
void loop() { | |
reset(); | |
for(int i = 0; i < 8; ++i) | |
{ | |
signal( led2, ((i >> 2) % 2) == 1); // See what I did there? Ninja bit magic! | |
signal( led1, ((i >> 1) % 2) == 1); // Got the idea from | |
signal( led0, ((i >> 0) % 2) == 1); // http://forums.adafruit.com/viewtopic.php?f=20&t=12289 | |
delay(2000); | |
} | |
} | |
// Set all LEDs off to make sure we start at zero | |
void reset() { | |
signal( led2, 0); | |
signal( led1, 0); | |
signal( led0, 0); | |
} | |
void signal(int led, bool on) { | |
if(on) { | |
digitalWrite(led, HIGH); | |
} | |
else { | |
digitalWrite(led, LOW); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment