Go Down

### Topic: My first sketch. Hello world and Hello Arduino forums (Read 2215 times)previous topic - next topic

#### crosson

##### Jan 20, 2013, 09:22 pm
This is my first go with the Arduino and my first post here on the forum! I have quite a bit to learn about electronics. The nice thing is that the Arduino feels like legos for electronics. I find that it is very easy and inviting to learn as well as fun and rewarding.

I ordered a starter kit and was able to assemble a small binary counter given the items I had. I'd like to turn this into a binary clock, perhaps with an alarm.

Presently it counts to 31 as I only have 5 LEDS and buzzes when it hits 31. You can control the speed with the potentiometer.

Sketch avail on my github page.
Sketch

Video

Criticism are welcome! I am here to learn.

#### nickgammon

#1
##### Jan 20, 2013, 09:31 pm

Code: [Select]
`// Array of LED PINS. Makes it easy for our binary counter to decide which LEDS to blink.//int PINS[] = {13, 12, 11, 10, 9};int delayms = 0;// the setup routine runs once when you press reset:void setup() {                  // initialize digital pins as outputs.  for(int led = 8; led < 14; led++){    pinMode(led, OUTPUT);  }}//Blinks LEDS in binaryvoid blink_binary_number(int number, boolean vol=HIGH){  for(int led = 0; led < number; led++){    if (bitRead(number, led) == 1){      digitalWrite(PINS[led], vol); //Here we use our PINS array to decide which LED to blink. Since I wanted 13 to be bit 0, 12 bit 1, etc...    }  }}// the loop routine runs over and over again forever:void loop() { for(int x = 0; x < 32; x++){   delayms = analogRead(0) * 10;      if (x == 31){     analogWrite(8, 254);   }      blink_binary_number(x);   delay(delayms);         if (x == 31){     analogWrite(8, 0);    }      blink_binary_number(x, LOW);   delay(delayms); }}`

Your array "PINS" seems to have 5 elements, but you are accessing elements 0 to 31, if I am not mistaken.
Please post technical questions on the forum, not by personal message. Thanks!

#### crosson

#2
##### Jan 20, 2013, 09:40 pm
Ahh yes. I see what you mean. It didn't immediately pop out to me but inside of my blink_binary_number I am iterating over elements that I never initiated.

#### crosson

#3
##### Jan 20, 2013, 09:44 pm
Is there a sizeof, length, or size method I can use against an array? For example...

Quote

for(int led = 0; led < PINS.length + 1; led++){
digitalWrite(PINS[led], vol); //Here we use our PINS array to decide which LED to blink. Since I wanted 13 to be bit 0, 12 bit 1, etc...
}
}
}

This would allow me to change the size of my pins array without re-writing this method.

#### crosson

#4
##### Jan 20, 2013, 09:49 pm
Ahh sorry for the noise. I found a good answer here.

#### crosson

#5
##### Jan 20, 2013, 10:05 pm
Fixed!

Code: [Select]
`// Array of LED PINS. Makes it easy for our binary counter to decide which LEDS to blink.//int const PINS[] = {13, 12, 11, 10, 9};int const PINS_SIZE = (sizeof(PINS)/sizeof(int));int delayms = 0;// the setup routine runs once when you press reset:void setup() {                  // initialize digital pins as outputs.  for(int led = 8; led < 14; led++){    pinMode(led, OUTPUT);  }  //Serial.begin(9600);}//Blinks LEDS in binaryvoid blink_binary_number(int number, boolean vol=HIGH){  for(int led = 0; led < PINS_SIZE; led++){    if (bitRead(number, led) == 1){      digitalWrite(PINS[led], vol); //Here we use our PINS array to decide which LED to blink. Since I wanted 13 to be bit 0, 12 bit 1, etc...    }  }}// the loop routine runs over and over again forever:void loop() { for(int x = 0; x < 32; x++){   delayms = analogRead(0) * 10;      if (x == 31){     analogWrite(8, 254);   }      blink_binary_number(x);   delay(delayms);         if (x == 31){     analogWrite(8, 0);    }      blink_binary_number(x, LOW);   delay(delayms); }}`

#### nickgammon

#6
##### Jan 20, 2013, 10:44 pm
A generic "size of array" define is this:

Code: [Select]
`// number of items in an array#define NUMITEMS(arg) ((unsigned int) (sizeof (arg) / sizeof (arg [0])))`

Then you have:

Code: [Select]
`int const PINS_SIZE = NUMITEMS (PINS);`
Please post technical questions on the forum, not by personal message. Thanks!