Monday, January 21, 2013

ProtoCylon: Telling the Temperature


ProtoCylon: Telling the Temperature

By Bobby Neal Winters
I remember reading old documents, ages ago, about the computing model that was in three parts: Input, Process, and Output.  I thought it was kind of goofy at the time, but I was younger and smarter then.  Now that I am old and stupid I’ve gained an appreciation of how separating a project into smaller parts--even into three parts--can make doing the project a reality. (When I was younger and smarter, I never actually did anything.)
Thus is the tale of teaching my ProtoCylon to tell the temperature.  It comes in three parts and there is a part where I grow just a little bit.
The Input part I referred to in my post ProtoCylon Senses. This involved the DS18B20 digital temperature sensor.  This is a tiny thing.  It has three pins like a transistor.  Heck, for all I know, it might be a transistor.  The power goes in one side, out the other, and the center pin is connected to the Arduino board.  You can check the wiring at this tutorial.
For the Output, I used an LCD display.  This required growth for me.  I will work with the most obvious part first. This display has 16 pins.  That is a lot of pins. This is eased a bit by the fact that four of them are used for power and one is just grounded out.  This leaves eleven, though, and that is nothing to sneeze at.  This wasn’t the major growth inducer, however.  No, the big this is that I had to solder--yes, solder--those 16 pins to a set of pins that would allow me to plug it into the breadboard before I could even start to hook up the rest. (I will leave out the part where I attempted to hook it up without soldering. Not a happy time.)
After I got it soldered, I hooked it up and it didn’t work.  This is becoming a theme with me, but I am learning from this and recovering quicker each time.  The first stage is not to allow myself to be crushed by each failure.  I stop, take a deep breath, let it out, and try to figure out where I goofed.  
One useful technique is to take it apart and put it together again--slowly from scratch.  For wiring up the LCD display, I recommend the tutorial at Adafruit Learning System.
In between the Input and the Output is the Process.  The process in this case involved the use of libraries.  Libraries are specialized functions that make using hardware easier.  While there is a young turk deep inside me that would like to write my own libraries, there is an old goat who would like to actually get something done without taking it apart electron by electron.  Therefore, I happily use the libraries.
In creating this project, I actually used my creativity to merge two programs.  One of these was a “Hello, world!” program for the LCD that was an example that came with my Arduino compiler.  The other was in the book Beginning Arduino by Michael MacRoberts. I lay claim to the creativity crown because I did have to modify their code mutatis mutandis for the project to work.  The completed project follows:


This is the code:
/*
The uses temperature sensors to take the temperature
and outputs it to an LCD display.
I fused two programs by other people and modified them.

The first was:

The LCD Library originally added 18 Apr 2008
by David A. Mellis
library modified 5 Jul 2009
by Limor Fried (http://www.ladyada.net)
example added 9 Jul 2009
by Tom Igoe
modified 22 Nov 2010
by Tom Igoe

This example code is in the public domain.

http://www.arduino.cc/en/Tutorial/LiquidCrystal

The second was from Begining Arduino by Michael MacRoberts
*/

// include the library code:
#include
#include
#include

#define ONE_WIRE_BUS 3

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7,8,9,10,11,12);
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress insideThermometer, outsideThermometer;

void setup() {
 // set up the LCD's number of columns and rows:
 lcd.begin(16, 2);
 lcd.clear();
 // Get the temperature sensors running.
 sensors.begin();
 sensors.getAddress(insideThermometer,0);
 sensors.getAddress(outsideThermometer,1);

}

void loop() {
 //Get the Temperatures
 sensors.requestTemperatures();
 //Print it on the first row, i.e. row 0
 lcd.setCursor(0,0);
 printTemperature(insideThermometer);
 //Print it on the second row, i.e. row 1
 lcd.setCursor(0, 1);
 printTemperature(outsideThermometer);
 //enjoy the weather for one second
 delay(1000);
}


void printTemperature(DeviceAddress deviceAddress){
 //Get the temperature
 float tempC=sensors.getTempC(deviceAddress);
 //print it out.
 lcd.print("TempF:  ");
 lcd.print(DallasTemperature::toFahrenheit(tempC));
}

Sunday, January 20, 2013

ProtoCylon Senses

ProtoCylon Senses

By Bobby Winters

I’ve spent the last week on a voyage of discovery in which I’ve discovered myself the same as I’ve always been.
Let me explain.  
Last weekend I tried to get a UltraSonic distance sensor hooked up to my Arduino Uno.  The procedure is easy to find on the Internet.  I wired it, and it didn’t work.  I required it, and...it didn’t work.  In my reading of the reviews in the process of buying this sensor, one of the reviewers had complained the one he bought was trash. I figured I’d had a bit of bad luck, so I bought another one.  Yesterday, I wired it up again.
It worked great.
Then my curiosity got the better of me, so I plugged in the other one to the same wiring.  It worked too.  It had been miswired the first time.  
Now, I am fully capable of screwing up that badly.  I admit that freely.  In my fiddling around, I discovered that one of the jumper wires I had been using was bad.  This is a possibility that hadn’t occurred to me before.  In math your results don’t just go belly-up because your pencil has a crack in its lead.
This is a practice where the real world will check your work for you.
Another project I’d been working on from Beginning Arduino was to connect temperature sensors.  The sensors I’d purchased for this were DS18B20 sensors. You get 10 for that price.  I worked through the book, and, having learned my lesson, I wired the boards painstakingly. I hooked it up and discovered it was 285 degrees centigrade in my house. (For you non-metric types, water boils at 100 degrees centigrade.)  I discovered my error when I looked further in the book and discovered the DS18B20 are a different type of sensor and must be wired in a different way and require a different library of functions to operate.  I hooked them up, and got the temperature down to 85 degrees centigrade.  I changed a resistor in my wiring and it came down to 74 Fahrenheit.
Like I said, the real world checks your work for you.

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:
 val=analogRead(potPin);
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
 pinMode(b0,OUTPUT);
 pinMode(b1,OUTPUT);
 pinMode(b2,OUTPUT);
 pinMode(b3,OUTPUT);
 pinMode(b4,OUTPUT);
 pinMode(b5,OUTPUT);
 pinMode(b6,OUTPUT);
 pinMode(b7,OUTPUT);  
}          

void loop(){
 //Grab the reading from the potentiometer
 val=grabValue(potPin);
 //light the number
 lightBinary(val);
}

int grabValue( int pPin){
 //Get the value from the pin
 int mVal;
 mVal=analogRead(pPin);
 //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">
    {
     lightBit(j+3,bitRead(pN,j));

    }
}
void lightBit( int bL, int val)
{
 if(val==1){
    //Light the led if the bit is 1
    digitalWrite(bL ,HIGH);
    //leave the function
    return;
 }
 if(val==0){
    //turn off the led if the bit is 0
    digitalWrite(bL,LOW);
    //leave the function
    return;
 }
 //If you've made it here something is terribly wrong
 Serial.print("Error");
}

Wednesday, January 02, 2013

The Proto Cylon

The Proto Cylon

By Bobby Neal Winters
I’ve been captured by another enthusiasm.  The Arduino controller card.  If you go to http://arduino.cc/en/, then you can learn as much as I know about it in short order.  There is probably a fancy name for it, and if you read the whole page on the link I just gave you, then you can probably find it.  Regardless of what you call it, it is basically a little computer that you can use to run stuff.  
It costs about $20.
I learned of its existence while looking around for Raspberry Pi. This turns out to be a slightly different kettle of fish, but I will doubtless return to Raspberry Pi eventually.  But I digress.
The Arduino collection of cards give you the ability to interact with the mechanical world. You can control a small amount of electrical current using the C programming language.  This appealed to the same part of my brain that was attracted to the Potato Cannon and the Itty Bitty Ubuntu Box.  
(Since it controls a small amount of current and the potato cannon needs a small amount of current to produce a spark, there is even the possibility of combining the two.  Using an Arduino card to make a potato machine gun. Hmmmm.)
In any case, one of the classical projects for a card such as this is to make a robotic car.  That is my long range goal.  But why stop with just a car?  Why not think really long range and work one’s way up to a Cylon. (I am thinking a Six or an Eight. The Threes aren’t stable.)   In the meantime, we do baby steps.  My first step is learning how to blink LEDs on breadboard. The code is below:

int inputPin=5;
int ledPin1=2;
int ledPin2=6;
int ledPin3=8;

void setup() {
 pinMode(ledPin1, OUTPUT);
 pinMode(ledPin2, OUTPUT);
 pinMode(ledPin3, OUTPUT);
 pinMode(inputPin, INPUT);
 digitalWrite(inputPin,HIGH);
}

void loop() {
 blinkOff(ledPin1, 0);
 blinkOff(ledPin2, 0);
 blinkOff(ledPin3, 0);
 blinkOn(ledPin1, 500);
 blinkOn(ledPin2, 500);
 blinkOn(ledPin3, 500);
 blinkOff(ledPin3, 500);
 blinkOff(ledPin1, 500);
 blinkOff(ledPin2, 500);   
}

void blinkOn( int ledNum, int duration)
{
 int switchOpen=digitalRead(inputPin);
 digitalWrite(ledNum, ! switchOpen);
 delay(duration);  
}
void blinkOff( int ledNum, int duration)
{
 int switchOpen=digitalRead(inputPin);
 digitalWrite(ledNum, switchOpen);
 delay(duration);  
}

This is what it looks like:


Saturday, December 15, 2012

Rudolph at large



Rudolph at large

By Bobby Neal Winters
Those of you who follow this space may remember a report I relayed last year at this time regarding some difficulties Santa Claus had encountered during a promotional tour which took a rest stop along a deserted stretch of road between Jesse, Oklahoma and US Highway 377.  Eight shots were heard along that deserted stretch of road and venison jerky was being sold out of the trunk of a car at a basketball game in Stonewall.  There was also a report of a man wearing a red hat turning up in a Pentecostal Church with peppermint schnapps on his breath and his being taken off to jail.
Many of you have undoubtedly been of the opinion, since Christmas did come off last year and the presents from Santa were delivered on schedule, that I had made the whole account up.  While that might be an easy thing to believe in these days of rampant dishonesty, the truth is much more complicated.
It turns out that North Pole Headquarters is well prepared for situations such as this.  This is not the first time Santa has had to be sprung from jail, and there is a special squad of elves that have been trained for just such an occasion.  Usually, however, they just have to go to Vegas and spread enough money around.  The crew at the Pontotoc County Courthouse is made of much purer material however, and it took some community service for Santa this time.  If you saw an old man with a beard picking up trash on the side of the road, that might have been him. 
As for the reindeer, while their losses were substantial, they are of a military caste and took them as being a part of their duty.  New Dashers, Dancers, etc. succeeded into their hereditary positions as a matter of course. 
Duty is one thing, but, after a period of careful investigation, vengeance will be taken.  It will be quick, but it won’t necessarily be pretty.  You don’t mess with someone whose boss can get into your house at will.
You may also recall that, at the time the piece was written, Rudolph, the most famous reindeer of them all, had not yet been located, and that my old friend Bubba was searching for him.
If you are familiar with Bubba’s hunting skills, then you should be comfortably certain that, as long as Bubba is hunting for him on purpose, Rudolph is perfectly safe.  Any venison Bubba ever obtained—other than in exchange for cash from someone else’s trunk—has been a victim of his own front bumper.
“I hunted for him all last winter and into the spring,” Bubba said when I talked to him on the phone the other day.  “I never seen hide nor hair of him.”
“Maybe he flew home on his own,” I offered.
“I’d thought that myself,” he said, sounding serious, “but I heard some things that makes me think different.” 
Bubba always has some sort of theory or other to offer, so my better judgment told me to just let that pass, but I was unable to resist.
“What’s that?” I asked.
Continuing in his somber tone, he answered, “Some of my friends spend a lot of time in the woods hunting and fishing, and they’ve been seeing things they can’t explain.  One of them was out walking in the woods as saw some deer sign.”
“Well, that’s to be expected,” I said.  “It is deer season after all.”
“But this glowed in the dark,” he said.
“Glowed in the dark?” I asked.  “That’s pretty hard to believe.  Did you see it yourself?”
“No, but my friend used it to fertilize his tomatoes and when they were ripe, they looked just like red Christmas ornaments,” Bubba replied.  “He showed me one of them, and they were the prettiest little things.”
I didn’t believe him, but I didn’t feel like calling him a liar since he’d seen the Christmas ornaments and all.
“You said ‘things they can’t explain,’” I said.  “What else have they seen?”
“Well,” Bubba drawled, “a friend of mine was out deer hunting on Thanksgiving and thought he saw a red lightening bug one morning.  His eyes adjusted to the light saw a yearling deer where the lightening bug had been.  He blinked and when he opened his eyes the yearling was gone.  Then he heard rustling in the tree tops.”
“So,” I said, trying to sound as disinterested as possible, “what do you think is going on?”
“Well, you know how irresistible Oklahoma women are,” he said—and I wasn’t going to disagree with him, having married one myself.  (Some of my cousins have married several.)  “I think Rudolph has taken up with some of the local does.”
“You do?” I said this with the tone that may have started a fight if it hadn’t been muted by the phone.  Bubba either missed it or pretended too.
“Yep, I do, and you know what,” he continued unabated.  “I think it’s the best thing that’s happened around here in a long time.  Just think about how big a business deer hunting is.  What about hunting flying deer?  Think how festive a deer head with a glowing nose would look over the mantle in the holiday season.”
“I can’t imagine,” I said, and I tried not to.