Software firefly
It's another parts-on-hand project. A white LED connected to an output bit on a microcontroller. Ill use the Arduino Pro Mini because they're cheap and I have a few "in stock" in the garage. It's basically a Microchip (formerly Atmel) ATmega328P single chip microcomputer on a board. They're $4 in five-packs on Amazon, $2 direct from Asia but take your chances with Chinese counterfeit chips.
Oh no! The only white indicator LEDs I have on hand are tiny "0805" (0.08 x 0.05 inch) surface mount dudes. All my attempts to solder wires to these tiny parts fail. They must sit on a board. We have tiny "breakout" boards for that. For scale, the big round holes are 0.1 inch center to center.
The pro mini has a regulator so it can run off a 9V battery. Or it can run off 3 to 5 volts bypassing the regulator. Circuit looks like:
and here's the firefly assembly.
We use the Arduino integrated development system because it's easy with a big support ecosystem. Arduino IDE talks to the Pro Mini board through a USB to 5V serial adapter. The five wires are Ground, +5V power to Pro Mini, serial data from PC, serial data to PC, and "DTR." The DTR signal is no longer used for serial ports, and Arduino uses it to signal a hardware reset to the ATmega328P.
This is a complete hobby scale microcontroller development system. Usable from Windoz, Linux, or Macos. Windoz and mac will make you hunt for drivers. With Linux, it just plugs in and works.
// Arduino Pro Mini with some LEDs tacked on
// The yellow LED is connected between GPIO2 and VCC.
// (Of course with a current limiting resistor.)
// So LOW gives lit up and HIGH gives dark.
#define yellow 2
#define white 4
#define blue 10
char yel; // nonzero means yellow LED is on.
int onesec;
unsigned long delayer; //millis
int enth; // do it the nth time.
int enthb;
char rq; // request a flash,
char rqb; // request a blue flash,
void setup() {
char ix;
cli();
yel =0; // off
for (ix=2; ix < 13; ix++){
pinMode(ix, INPUT_PULLUP);
}
pinMode(yellow, OUTPUT);
digitalWrite(yellow, HIGH);
pinMode(green, OUTPUT);
digitalWrite(green, HIGH);
pinMode(13, OUTPUT);
// timer 1 1Hz
TCCR1A = 0;// set entire TCCR1A register to 0
TCCR1B = 0;// same for TCCR1B
TCNT1 = 0;//initialize counter value to 0
// set compare match register for 1hz increments
OCR1A = 15624;// = (16*10^6) / (1*1024) - 1 (must be <65536)
// turn on CTC mode
TCCR1B |= (1 << WGM12);
// Set CS10 and CS12 bits for 1024 prescaler
TCCR1B |= (1 << CS12) | (1 << CS10);
// enable timer compare interrupt
TIMSK1 |= (1 << OCIE1A);
onesec=0;
rq=0;
rqb=0;
enth=0;
sei();
}
ISR(TIMER1_COMPA_vect){
timer1service();
}
void timer1service(){ // 1 Hz timer
onesec++;
enth++;
enthb++;
if( enth > 5 ) { // blink every five secs.
rq=1;
enth =0;
}
if( enthb > 31 ) { //blink the other one every 31 sec.
rqb=1;
enthb =0;
}
}
void firefly(){
char iy, iz;
for( iy=1; iy<6; iy++){
digitalWrite(yellow, LOW);
delay(20);
digitalWrite(yellow, HIGH);
delay(30);
}
}
void blueflash(){
char iy, iz;
for( iy=1; iy<6; iy++){
digitalWrite(green, LOW);
delay(20);
digitalWrite(green, HIGH);
delay(100);
}
}
void loop() {
// put your main code here, to run repeatedly:
if( rq ){
firefly();
rq=0;
}
if( rqb ){
blueflash();
rqb=0;
}
delay(100); // Need a low power sleep here
}
Comments
Post a Comment