I received an Arduino Mega ADK for Christmas. My plan for my first project is to create a very fancy model rocket launcher, but you have to start somewhere, so here’s a basic example that implements a binary clock.
First, the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
/** * Binary counter example, this will work for as many leds as you * have digital output pins to connect them to. The example counts * seconds on a series of leds. To change the number of digits in * the display, set digits equal to the number of leds and set the * pin numbers in the led[] array. The rest is automatic. * John Caswell, 2013 */ // The number of leds in our display const int digits = 4; // Set the pins (in order) which the leds are attached to int led[] = {12, 11, 10, 9}; // Array to hold the states of the leds int state[digits]; // This will be calculated later to represent the largest value // that can be represented with the number of leds we have int maxValue; // Count the seconds int clock = 0; // Set all leds to off void setStates() { for(int i = 0; i < digits; i++) { state[i] = LOW; } } void setup() { // Turn off built-in LED pinMode(13, OUTPUT); digitalWrite(13, LOW); // Set the pins are leds are connected to to output for(int i = 0; i < digits; i++) { pinMode(led[i], OUTPUT); } // Start with all leds off setStates(); // Calculate the largest value we can display, which is // 2 ^ (number of leds we have) - 1. maxValue = (1 << digits) - 1; } void loop() { // Check which lights need to be on by bitwise and with powers // of two. int binaryPos = 1; for(int i = 0; i < digits; i++) { if(clock & binaryPos) state[i] = HIGH; binaryPos = binaryPos * 2; } // Turn all lights on together to reduce flicker. for(int i = 0; i < digits; i++) { digitalWrite(led[i], state[i]); } // Clear the state of all leds, so we start fresh each cycle. setStates(); // Increment and wrap counter. clock++; if(clock > maxValue) { clock = 0; } // Wait one second. delay(1000); } |
To build the circuit, you will need one led and resistor for each digit in your display, plus a method of connecting them. Because this is a throwaway example to me, I used a breadboard and some jumper wires. Connect the resistor to a digital pin on the arduino, and the other side of the resistor to an led. Connect the cathodes of all the leds to gnd on the arduino. See the images for an example of one way to do this with jumpers and a breadboard.
To change this to a real clock that displays the time in binary coded decimal, you’d want 19 leds (20 for military 24h time), and so would need to use an arduino with at least 19 digital pins, or some sort of encoding such as a shift register. You’d probably want to make 3 sets of arrays, for hours, minutes, and seconds, and every time you counted 60 seconds you’d wrap the seconds and increment the minutes, doing the same for hours and minutes every 60 minutes. To improve the accuracy of your clock, you’d want to time how long your loop takes, and subtract that from the delay between cycles, or your clock will run slow. This project is left as an exercise for the reader.
If you have questions / comments about this, feel free to email me at jcaswell at secondstringsamurai dot com.