Being a fairly tech savy lad, when I heard about the Arduino prototyping board about a year ago I knew I wanted one. This summer I finally mounted up and bought a Duemilanove and have been having a great time with it. After playing with some of the sample sketches I decided I would try and make binary clock. My breadboard is rather small so I had to do some substantial soldering in order to get my 13 led's to the 13 output pins of the arduino.
After I had the hardware setup, I started writing the code. I have some basic skills in C and C++ so I started by writing my code in C and having it spit out times to the comand prompt so I could test for bugs. After everything appeared to be working it only took a couple of changes to get the code compiled as an arduino sketch. After some more debugging and fixing of my own stupid mistakes... it worked. I was quite pleased and left it running all night; in the morning the time was still correct (to what degree I don't know).
Well here's the code. Keep in mind that I was too lazy to program in buttons to set the time so you have to set the intial values in the code to the time that you load the sketch.
//Initializes time. Change to current time then upload to arduino i.e. 03:02
int hour_one = 3;
int hour_ten = 0;
int min_one = 0;
int min_ten = 2;
//unsigned long is good for 1193.05 hours
unsigned long last_time;
void setup() {
pinMode(1, OUTPUT);pinMode(2, OUTPUT);pinMode(3, OUTPUT);pinMode(4, OUTPUT);pinMode(5, OUTPUT);
pinMode(6, OUTPUT);pinMode(7, OUTPUT);pinMode(8, OUTPUT);pinMode(9, OUTPUT);pinMode(10, OUTPUT);
pinMode(11, OUTPUT);pinMode(12, OUTPUT);pinMode(0, OUTPUT);
}
void loop(){
last_time = millis(); //keeps track of how long is took to run through loop()
if (min_one < 9){
min_one++; //adds to the ones place in the minutes
}
else {
min_one = 0; //resests the ones place in minutes to 0
min_ten++; //adds the tens place in minutes
if (min_ten == 6){
min_ten = 0; //resets the tens place to 0
hour_one++; //adds the ones place in hours
if (hour_one == 10){
hour_one = 0; //resets ones place in hour to zero the adds on
//to tens place
hour_ten++;
}
if (hour_one == 4 && hour_ten == 2){
hour_one = 0; //resets hour to 0 if 23:59 was last printed time
hour_ten = 0;
}
}
}
display(min_one, min_ten, hour_one, hour_ten); //displays the time to leds
for(;(millis() - last_time) <= 60000;){} //waits a minute taking into
//account the time to run
//through loop()
}
void display(int min_one, int min_ten, int hour_one, int hour_ten){
int hour_t[2] = {0}; //intialized arrays to store binary
//version of time
int hour_o[4] = {0};
int min_t[3] = {0};
int min_o[4] = {0};
int i = 0;
int x = 0;
//Creating arrays that contain binary form of current time
while(hour_ten != 0){
hour_t[i] = hour_ten % 2;
i++;
hour_ten = hour_ten/2;
}
i = 0;
while(hour_one != 0){
hour_o[i] = hour_one % 2;
i++;
hour_one = hour_one/2;
}
i = 0;
while(min_ten != 0){
min_t[i] = min_ten % 2;
i++;
min_ten = min_ten/2;
}
i = 0;
while(min_one != 0){
min_o[i] = min_one % 2;
i++;
min_one = min_one/2;
}
//tens place in hours
if(hour_t[0]) digitalWrite(0, HIGH);
else digitalWrite(0, LOW);
if(hour_t[1]) digitalWrite(1, HIGH);
else digitalWrite(1, LOW);
//ones place in hours
if(hour_o[0]) digitalWrite(2, HIGH);
else digitalWrite(2, LOW);
if(hour_o[1]) digitalWrite(3, HIGH);
else digitalWrite(3, LOW);
if(hour_o[2]) digitalWrite(4, HIGH);
else digitalWrite(4, LOW);
if(hour_o[3]) digitalWrite(5, HIGH);
else digitalWrite(5, LOW);
//tens place in minutes
if(min_t[0]) digitalWrite(6, HIGH);
else digitalWrite(6, LOW);
if(min_t[1]) digitalWrite(7, HIGH);
else digitalWrite(7, LOW);
if(min_t[2]) digitalWrite(8, HIGH);
else digitalWrite(8, LOW);
//ones place in minutes
if(min_o[0]) digitalWrite(9, HIGH);
else digitalWrite(9, LOW);
if(min_o[1]) digitalWrite(10, HIGH);
else digitalWrite(10, LOW);
if(min_o[2]) digitalWrite(11, HIGH);
else digitalWrite(11, LOW);
if(min_o[3]) digitalWrite(12, HIGH);
else digitalWrite(12, LOW);
}
No comments:
Post a Comment