Pages: [1]   Go Down
 Author Topic: My first sketch. Hello world and Hello Arduino forums  (Read 303 times) 0 Members and 1 Guest are viewing this topic.
Offline
Newbie
Karma: 0
Posts: 27
 « on: January 20, 2013, 03:22:36 pm » Bigger Smaller Reset

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

Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13897
Lua rocks!
 « Reply #1 on: January 20, 2013, 03:31:33 pm » Bigger Smaller Reset

Code:
// 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);
}
}

for(int led = 0; led < number; 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...
}
}
}

// the loop routine runs over and over again forever:
void loop() {
for(int x = 0; x < 32; x++){

if (x == 31){
analogWrite(8, 254);
}

delay(delayms);

if (x == 31){
analogWrite(8, 0);
}

delay(delayms);
}
}

Your array "PINS" seems to have 5 elements, but you are accessing elements 0 to 31, if I am not mistaken.
 Logged

Offline
Newbie
Karma: 0
Posts: 27
 « Reply #2 on: January 20, 2013, 03:40:25 pm » Bigger Smaller Reset

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

Offline
Newbie
Karma: 0
Posts: 27
 « Reply #3 on: January 20, 2013, 03:44:13 pm » Bigger Smaller Reset

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

Offline
Newbie
Karma: 0
Posts: 27
 « Reply #4 on: January 20, 2013, 03:49:30 pm » Bigger Smaller Reset

Ahh sorry for the noise. I found a good answer here.
 Logged

Offline
Newbie
Karma: 0
Posts: 27
 « Reply #5 on: January 20, 2013, 04:05:54 pm » Bigger Smaller Reset

Fixed!

Code:
// 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);
}

for(int led = 0; led < PINS_SIZE; 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...
}
}
}

// the loop routine runs over and over again forever:
void loop() {
for(int x = 0; x < 32; x++){

if (x == 31){
analogWrite(8, 254);
}

delay(delayms);

if (x == 31){
analogWrite(8, 0);
}

delay(delayms);
}
}
 Logged

Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13897
Lua rocks!
 « Reply #6 on: January 20, 2013, 04:44:10 pm » Bigger Smaller Reset

A generic "size of array" define is this:

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

Then you have:

Code:
int const PINS_SIZE = NUMITEMS (PINS);
 Logged

Offline
Newbie
Karma: 0
Posts: 27
 « Reply #7 on: January 21, 2013, 12:07:13 pm » Bigger Smaller Reset

Yes. Putting it in a method makes a lot more sense. Thanks Nick!
 Logged

 Pages: [1]   Go Up