Pages: 1 [2]   Go Down
Author Topic: Cannot blink pins 7 through 5 (NEWBIE)  (Read 1570 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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:
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.
Logged

Queens, New York
Offline Offline
Faraday Member
**
Karma: 106
Posts: 3748
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What does a_size return?
Logged

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

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That makes perfect sense. It has been a very long time since I've coded in c++ but some of this is coming back. Do you think the way I have it now is acceptable?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Alright well I have a working clock. In my excitement to get it all working I may have mangled my code a bit. I had to add a crude setup process so you could set the right time. Still it is wonderful that I got it up and running.

Now I can refine it and improve it. Since this is the first real thing I've made with Arduino it has spiked a load of questions regarding electronics. Some of resistors I used were guess work. I'll probably post some followup questions in the electronics forum.

Below is what I have so far in my code.
Code:
//#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 hours = 1;
int minute = 0;
int setup_time = 10000;

void setup() {               
  for(int led = 4; led < 14; led++){
    pinMode(led, OUTPUT);
  }
  //Serial.begin(9600);
  pinMode(3, INPUT);
}

//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() {
  while (setup_time > 0) {
    if (digitalRead(3) == HIGH) {
//      Serial.print("Minute: ");
//      Serial.println(minute);
//      Serial.print("Hour: ");
//      Serial.println(hours);
//      Serial.println("------");       
      blink_binary_number(hours, HOURS, 4);
      blink_binary_number(minute, MINUTES, 6);

      //Incriment time   
      minute++;
      if (minute == 60) {
        minute = 0;
        hours++; 
      }
     
      if (hours > 12) {
        hours = 1;
      }
      delay(150);
      setup_time = 10000;
    } else {
     delay(10);
     setup_time = setup_time - 10;
    }
  }
 
 
//  Serial.print("Minute: ");
//  Serial.println(minute);
//  Serial.print("Hour: ");
//  Serial.println(hours);
//  Serial.println("------");   
 
  blink_binary_number(hours, HOURS, 4);
  blink_binary_number(minute, MINUTES, 6);

 //Incriment time   
  minute++;
  if (minute == 60) {
    minute = 0;
    hours++; 
  }
 
  if (hours > 12) {
    hours = 1;
  }
  delay(60000);
}



Uploading a video now.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Video of the clock.
Logged

Pages: 1 [2]   Go Up
Jump to: