Sunday, January 13, 2013

Proto Cylon v1.0001

Proto Cylon v1.0001

By Bobby Neal Winters
I’ve been fiddling with my Arduino board a little more.  The Internet in general and YouTube in particular is rich with examples of projects using the Arduino board.  As with any student, my struggle has been to find the right level of video.  There are those which assume that you know everything about electronics and that you have certain things just lying around.  Then there are those which are very detailed and step-by-step, but don’t do much in creating a conceptual framework.  My hope is to work in a little of that conceptual framework.  It won’t be at the right level for anyone besides me, of course, but it will fit into the general milieu of the Internet as another piece of the puzzle.
The Arduino is able to take in information from the outside world in the form of electrical information.  It can process that information in form of binary numbers.  Then it can output information in such a way as to effect the outside world.
My Proto Cylon v1.0001 is an example of that.  In it I get input from a potentiometer, scale that input to a range from 0 to 255, and then output it in binary form using LEDs.
In this I use:
  • A 100 ohm potentiometer I got from Amazon.
  • An Arduino Starter Kit I also got from Amazon.
    • This included the Arduino Uno board;
    • Breadboard;
    • Jumper wires;
    • And a nice piece of plastic to mount the Arduino and the breadboard on.
    • A 9 volt battery connector.
  • I also got an assortment of leds and resistors from Amazon.
Before I dive into the project proper, let me remark on a few things that apparently everyone talking about this sort of thing believes everyone knows.  
First let me say that breadboard is the coolest stuff since, uh, sliced bread. You can hook together your components without soldering.  Those of you who have read of my adventures with the crystal radio know of the mental block I have regarding solder.  With breadboard, you just plug stuff in.  The breadboard I used here has power bars along the side with connections that run the length of the board and connections along the middle that run the width.  It has an adhesive backing that you can peel-off and stick to stuff.
The LEDs and resistors come together on purpose.  The LEDs run at a lower level of current than the Arduino board puts out.  The resistors are used to decrease that current.  If you don’t do this, the LEDs will get hot.  I learned that by not using the resistors and touching one.  Resistors come in various denominations. There is a way to figure out what size of resistor that you need to get a particular current.  I am not going to that depth at this point.  I will say that Ohms is the unit of resistance and those cute little stripes you see on the resistor tell you what that resistance is. In order to read them, you need to know the secret language of resistors and it can be found in Wikipedia.
In this project, we do our input from a 100 ohm potentiometer. The potentiometer is basically a knob you turn.  It has three prongs on the bottom: left, right, and middle. The left and the right prongs are where the power is hooked on.  The middle is where it is read out.  It is an entity unto itself and only interacts with the LEDs that we use for output via the Arduino.  The left prong will be hooked to 5 volts, the right will go to ground, and the middle pin will be taken to the A0 pin on the Arduino board. Once hooked to the board, the potentiometer will send a signal to the pin that is interpreted as a number between 0 and 1023. The function that retrieves this value is given below:
For more detailed information about the syntax of this and the other functions used, look at the Arduino website reference.
Those of you who are attuned to the computer age might be suspicious of that particular range of values.  There are 1024 total values represented there. You might recognize 1024 as being 2 to the tenth power.  It requires 10 bits to represent this number. I discovered that wiring a board for LEDs requires a bit of patience and manual dexterity. I had enough patience to wire 8 LEDs but not enough to wire 10.  Wiring apparently has an accumulative effect on one’s nerves, and I’d run out of hard cider.
This means I needed to scale the value of the potentiometer reading to be in the appropriate range.  This is done via the function below:
 val=map(val, 0, 1023, 0, 255);
Once the value is read and mapped, we want to tear it to bits. (We all love our own puns; please indulge me.) We do this by mathematical functions in the C language.  The eight bits that we are going to use for our binary number from 0 to 255 are numbered from 0 to 7.  The first bit is labeled as 0 for 2 to the 0th power; the second as 1 for 2 to the 1st, and so on.  I write all of this so that I can say bitRead( x, i) with return the value of the ith bit in the number x.  For example for x=1001001 in binary, bitRead( x,0) returns 1 and bitRead(x,1) returns 0.  This is used only at one place in the program, but it is crucial.
I won’t impose the program on you until after the prose is done in interest of not boring you to actual tears. Let me put the lie to those words by now sharing a bit about my programming philosophy.  The program should be easy to read to see what it does.  This means not only should it be documented, but it should be written in functions so that at each level you can see what is done.  In programming the Arduino, after an initial setup, you go into a loop that is executed over and over and over. I put the things in the loop the will happen again and again: the value will be read from the potentiometer and the value will be output to the LEDs.
This is done with two functions.  The input function is fairly straightforward, and the output function isn’t too bad.  Their composition can be followed in the code at the end of this blog entry.
We now come to the wiring of the project.  A picture is indeed worth a thousand words, but a few words can help the picture.  Each LED is wired as follows: its pin on the Arduino goes to a resistor; the resistor goes to the long end of the LED; the short end of the LED goes to ground on one of the side bars of the breadboard.  By putting all of your ground to the side bar of the breadboard, you only need to run one ground wire to the Arduino.  You may see it in the picture below:
The code is below:
//Set the pin for the potentiometer
int potPin=0;

//Map the Arduino pins to bits of a number in the range from 0 to 255
int b0=3;
int b1=4;
int b2=5;
int b3=6;
int b4=7;
int b5=8;
int b6=9;
int b7=10;
//Initiate values for the analog inputs from the potentiometer
int val=0;
const int MAX_VAL=255;
void setup(){
 //Set the led pins for output

void loop(){
 //Grab the reading from the potentiometer
 //light the number

int grabValue( int pPin){
 //Get the value from the pin
 int mVal;
 //Map the reading to our allowed range of values
 mVal=map(mVal, 0, 1023, 0, MAX_VAL);  
 return mVal;

void lightBinary( int pN)
    //Loop digit by digit turing the led on or off as appropriate
    for(int j=0;j<8 j="j" span="span">

void lightBit( int bL, int val)
    //Light the led if the bit is 1
    digitalWrite(bL ,HIGH);
    //leave the function
    //turn off the led if the bit is 0
    //leave the function
 //If you've made it here something is terribly wrong

No comments: