A second slice of LED-based Halloween goodness.
Blinking bats from nikkipugh on Vimeo.
These bats will be part of my attire for later today, but they’re really quite pleasing just by themselves!
A really simple build using an Arduino-based Real Bare Bones Board, some cardboard, some insulation tape some wire and 14 LEDs.
Here’s the code:
/*
blinking bats
nikki pugh 30th October, 2010
Attribution-NonCommercial-ShareAlike Creative Commons
Powers LED eyes for a colony of bats, blinking them in a random sequence at random intervals
http://npugh.co.uk/blog/blinking_bats/
LED pairs with 330ohm resistors in series, connected between the output pins and ground.
*/
int colonySize = 7; // How many bats do you have?
//control pins for each bat
int bat1 = 2; // bat1
int bat2 = 3; // bat 2
int bat3 = 4; // bat 3
int bat4 = 5; // bat 4
int bat5 = 6; // bat 5
int bat6 = 7; // bat 6
int bat7 = 8; // bat 7
int bat8 = 9; // bat 8
int bat9 = 10; // bat 9
int bat10 = 11; // bat 10
int bat11 = 12; // bat 11
int gap = 3; // gap before selecting next bat to blink
int colony[] = {bat1, bat2, bat3, bat4, bat5, bat6, bat7, bat8, bat9, bat10, bat11}; // Put bat IDs into an array
int batSelect = 1; // your bat selection variable - used for selecting a bat ID from the above array
int blinker = bat1; // the bat selected to blink
int i = 0; // counter for start-up blinks
void setup() {
randomSeed (analogRead (0)); //read from the (unused) analogue pin to get a value to seed the "pseudo-random number generator"
pinMode(bat1, OUTPUT); // set pins to be outputs
pinMode(bat2, OUTPUT);
pinMode(bat3, OUTPUT);
pinMode(bat4, OUTPUT);
pinMode(bat5, OUTPUT);
pinMode(bat6, OUTPUT);
pinMode(bat7, OUTPUT);
pinMode(bat8, OUTPUT);
pinMode(bat9, OUTPUT);
pinMode(bat10, OUTPUT);
pinMode(bat11, OUTPUT);
}
void loop() {
for (i = 0; i< 3; i ++) { //blink all bats' eyes at start-up
digitalWrite(bat1, HIGH);
digitalWrite(bat2, HIGH);
digitalWrite(bat3, HIGH);
digitalWrite(bat4, HIGH);
digitalWrite(bat5, HIGH);
digitalWrite(bat6, HIGH);
digitalWrite(bat7, HIGH);
digitalWrite(bat8, HIGH);
digitalWrite(bat9, HIGH);
digitalWrite(bat10, HIGH);
digitalWrite(bat11, HIGH);
delay (200);
digitalWrite(bat1, LOW);
digitalWrite(bat2, LOW);
digitalWrite(bat3, LOW);
digitalWrite(bat4, LOW);
digitalWrite(bat5, LOW);
digitalWrite(bat6, LOW);
digitalWrite(bat7, LOW);
digitalWrite(bat8, LOW);
digitalWrite(bat9, LOW);
digitalWrite(bat10, LOW);
digitalWrite(bat11, LOW);
delay (200);
}
digitalWrite(bat1, HIGH); //all eyes back on again
digitalWrite(bat2, HIGH);
digitalWrite(bat3, HIGH);
digitalWrite(bat4, HIGH);
digitalWrite(bat5, HIGH);
digitalWrite(bat6, HIGH);
digitalWrite(bat7, HIGH);
digitalWrite(bat8, HIGH);
digitalWrite(bat9, HIGH);
digitalWrite(bat10, HIGH);
digitalWrite(bat11, HIGH);
while (true){ // infinite loop
gap = random(1, 5); //wait some seconds before selecting next bat
delay(gap*1000);
batSelect = random (0, colonySize-1); //select a bat at random
blinker = colony[batSelect];
digitalWrite(blinker, LOW); //blink
delay(200);
digitalWrite(blinker, HIGH);
}
}