I've made some
wonderful progress. I have all my pins blinking and my binary counting for the hour leds and minute leds are working great. I've cleaned up my blink_to_binary function a bit so that it only needs to be called once per array of pins.
#define NUM_ITEMS(arg) ((unsigned int) (sizeof (arg) / sizeof (arg [0])))
int const MINUTES[] = {13, 12, 11, 10, 9, 8};
int const HOURS[] = {7, 6, 5, 4};
int const delayms = 500;
int hours = 1;
void setup() {
for(int led = 4; led < 14; led++){
pinMode(led, OUTPUT);
}
Serial.begin(9600);
}
int array_size(int const a[]) {
return (sizeof(a)/sizeof(int));
}
void blink_binary_number(int number, int const pin_array[], int a_size){
for(int led = 0; led < a_size; led++){
if (bitRead(number, led) == 1){
digitalWrite(pin_array[led], HIGH);
} else {
digitalWrite(pin_array[led], LOW);
}
}
}
void loop() {
for(int x = 0; x < 60; x++){
Serial.print("Minute: ");
Serial.println(x);
Serial.print("Hour: ");
Serial.println(hours);
Serial.println("------");
blink_binary_number(hours, HOURS, 4);
blink_binary_number(x, MINUTES, 6);
if (x == 59) {
hours++;
}
if (hours > 12) {
hours = 0;
}
delay(delayms);
}
}
I did run into a snag with catching array size. I wanted my bit_to_binary function to be able to handle any array size. I also don't want it trying to access the next element in an array if it doesn't exist so I only want to count up to the array size.
I made two array_size functions to accomplish this. NUM_ITEMS and array_size. If I run these inside of my blink_binary_number the array size is always one.
I want to do something like this
void blink_binary_number(int number, int const pin_array[]){
a_size = array_size(pin_array);
for(int led = 0; led < a_size; led++){
if (bitRead(number, led) == 1){
digitalWrite(pin_array[led], HIGH);
} else {
digitalWrite(pin_array[led], LOW);
}
}
}
void loop() {
blink_binary_number(number, HOURS_ARRAY);
}
However every time I run this method inside of the blink_binary_number HOURS_ARRAY always returns 1. If I run it from within void loop it correctly returns a length of 4. For now I just pass the array size into the function but it would be nice if the function could just handle this for me.
I know this is probably due to a lack of understanding in what is going with hour the array is referenced. Can someone help me understand this?
I'll send pictures and perhaps a video of what I have so far in a bit.