Code Library - Binary Counter

Making your program work.
Post Reply
BobW
Posts: 11
Joined: Tue Jun 02, 2009 6:19 am

Code Library - Binary Counter

Post by BobW » Tue Jun 02, 2009 6:32 am

Hi:

I'm just getting started with my brand new BBB and Arduino.
It just came off the soldering iron yesterday.

Is there a library of popular code snippets that I may access so I don't have to re-invent the wheel?

I'm trying to cycle ten output pins through 1024 binary numbers.
Someone must've posted this code somewhere.

Thank you.
BobW

teedeeus
Posts: 7
Joined: Tue Jun 02, 2009 11:32 am

Re: Code Library - Binary Counter

Post by teedeeus » Tue Jun 02, 2009 1:28 pm

There is some code here that would have to be modified for your application: http://www.arduino.cc/cgi-bin/yabb2/YaB ... 1236705296, but that should give you a starting point.

For beginners, remember not to connect anything to digital pins 0 and 1 as they are used for uploading your sketch. Use pins 2-11 for LED's and adjust the code accordingly.

BobW
Posts: 11
Joined: Tue Jun 02, 2009 6:19 am

Re: Code Library - Binary Counter

Post by BobW » Tue Jun 02, 2009 4:38 pm

Teedeeus:

You da man!
That worked great.
I appreciate you sharing your knowledge.

BobW

paul
Site Admin
Posts: 733
Joined: Mon May 12, 2008 4:19 pm

Re: Code Library - Binary Counter

Post by paul » Wed Jun 03, 2009 8:08 am

Thanks for the link teedeeus.

Here's some code (warning - not tested) that blinks 10 LED's using the port variables. It's easier to read than the previous example, but you need to get into some intermediate coding concepts such as bit shifting and Port variables. These are in the arduino reference section but you'll have to look in "port manipulation" for explanation of how the port variables work. Maybe not first thing on your agenda but you can see that there are few things still missing from the Arduino language to make things easy for programmers. Luckily the IDE supports lots of more advanced ways (AVR code) to do things.


Paul

Code: Select all

/* code removed because of an error - see code below

teedeeus
Posts: 7
Joined: Tue Jun 02, 2009 11:32 am

Re: Code Library - Binary Counter

Post by teedeeus » Wed Jun 03, 2009 4:28 pm

I noticed a potential problem with the global variable i in this code. When you get done setting your outputs, i will contain 11. The loop adds 1 to i so the first binary number displayed will be 12. Personally I prefer to initialize my loop variables (i,j,k,etc) within the loop they are used in. I realize this may take slightly more memory, but the troubleshooting time this saves is enormous. I spent 2.5 hours troubleshooting an issue I had with an Arduino program directly driving a 7-segment LED display from an array and was using i as a global variable for all my loops.

BTW, the bit logic you used in this is brilliant. I didn't know about that - very cool.

Here's how I would change the code:

Code: Select all

/*
* BlinkLEDsWPortVariables
* Blink 10 LED's with binary encoding
*/

void setup()                     // run once, when the sketch starts
{
    for (int  i = 2; i < 12; i++){    
/* 
A variable can be defined right in the for loop.  I just put the variable type, in this case 'int' right in front of it.  Now this variable 'i' only exists within this loop, when the loop is done, the variable is gone.
*/
        pinMode(i, OUTPUT);       // set  pins 2 to 11  to output
    }
}

void loop()                     // run over and over again
{  
    for(int i=0; i<1024; i++) {    
/*
This variable i only exists within the context of this for() loop.  Again, because it is being defined within the context of the for() loop, when the loop is done, the variable won't interfere with any other 'i' variables you may have defined.

and yes, I like for() loops :)
*/

        // PORTD is the output variable for pin states of digital pins 0 to 7
        PORTD = i << 2;                 // bit shift the variable up 2 to avoid pin 0 & 1 (RX, TX) 
        PORTB = i >> 6;                 // bit shift the top four bits into the lowbyte
                                                // PORTB controls pins 8, 9 , 10, 11 etc.

        delay(100);
    }
}
Anyway - just my 2 cents. I think it's helpful to beginners to let them know about this. More information on this can be found here: http://www.arduino.cc/en/Reference/Scope.

paul
Site Admin
Posts: 733
Joined: Mon May 12, 2008 4:19 pm

Re: Code Library - Binary Counter

Post by paul » Wed Jun 03, 2009 8:39 pm

Oops - that's what I get for putting up untested code, but thanks tons for posting a working version teedeeus. Plus some good tips for beginning coders.

I thought I noticed one of those uncanny sensations that one gets when one does something that "might not be a good idea", while I was coding that variable. I tend to use for loops the way teedeeus explained too.

Paul

BobW
Posts: 11
Joined: Tue Jun 02, 2009 6:19 am

Re: Code Library - Binary Counter

Post by BobW » Thu Jun 04, 2009 8:15 am

Paul:

Thanks for the code you posted.

I like it even better than teedious's (which I liked a lot).

In particular, in yours, the digits change synchronously.
I was noting minor artifacts in the teedious program for example during the transition from 5 to 6. As the bits get set, the right upper segment of the seven segment LED connected to the pins illuminates transiently during the transition. Stepping thruough the program explains why.

Thank you both, Paul and Teedious for helping me.
I have made a lot of progress in the past few days.

BobW

Aaronhycle
Posts: 7
Joined: Mon Sep 04, 2017 3:31 pm

Code Library Binary Counter

Post by Aaronhycle » Sun Sep 10, 2017 4:56 pm

Hi, I am new at using python. I am trying to create a control which takes in video and gets the frames from it. I tried using the following code form the example:

Code:

Post Reply