Go Down

Topic: Cannot blink pins 7 through 5 (NEWBIE) (Read 1 time) previous topic - next topic

crosson

Quote from: HazardsMind

...

Quote from: PeterH

...


I am troubleshooting this now. I got pins 5-7 blink after disconnecting everything. I'll keep you guys posted. Thanks again for the help.

crosson

I got all 3 pin, 5-7, to blink individually. I think I made some wiring mistakes. Apparently some of my LEDS are 3.0-3.2V and they didn't work on the digital pins. I'll continue in the morning with more information. Thanks guys.

crosson

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.

Code: [Select]
#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

Code: [Select]

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.

HazardsMind

Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

PeterH

You're running into the difference between an array and a pointer.

When you deal directly with the array variable, the compiler knows its size.

When you are only dealing with a reference to the array, the compiler knows the element type and the address of the first element but does not know how long the array is. Hence for all practical purposes the value you have is just a pointer to the first element in the array, and it is up to you to provide some other way to know how long the array is if your code requires that. For example, it's common to pass an array around as a pointer and a length.
I only provide help via the forum - please do not contact me for private consultancy.

Go Up