Pages: [1]   Go Down
Author Topic: My first sketch. Hello world and Hello Arduino forums  (Read 702 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

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

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Your sketch here:

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);
  }
}

//Blinks LEDS in binary
void 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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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 Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is there a sizeof, length, or size method I can use against an array? For example...

Quote
void blink_binary_number(int number, boolean vol=HIGH){
  for(int led = 0; led < PINS.length + 1; 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...
    }
  }
}

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

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

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

Offline Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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);
}

//Blinks LEDS in binary
void 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);
 }
}
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset 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 Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: