Go Down

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

crosson

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. :)

Nick Gammon

Your sketch here:

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 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.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

crosson

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

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.

crosson


crosson

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

Nick Gammon

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!

More info:
http://www.gammon.com.au/electronics

crosson

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

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy