Saturday, March 26, 2011

ARDUINO!

Holy shit, omg, so forth and so on

I managed to lay my grubby hands on an Arduino Uno, and man is it a blast to play with. I'm totally brand new to Arduino, so gimme a break if I seem a little more remedial than normal. You really only need a couple things to start playing with one of these buggers; a computer that can run the ide (integrated development environment), a usb cable, an Arduino and some electronic components to interact with. The cost is absolutely minimal compared to back in the day when the home hobbyist would have to spend hundreds of dollars to play with a micro-controller. Thanks to the open sourced nature of Arduino you can be set up for under $30.

Setting up was a breeze. I run Ubuntu 10.10 as my OS and I wasn't entirely surprised to find the Arduino ide included in the universe repository. However, it didn't work. It seems that 10.10 broke some of the packages that the included version of Ardiuino ide (18) needed, so I had to manually download and install the latest version from the Arduino website. Shit went off without a hitch. As long as you are near your computer you don't need external power supply for your board as it gets that from the usb. Since the one I have is the Uno, that's the only one I can really talk about. There are many different variants available to suit both small and large needs alike. So far I have only blinked leds, but I'm very easily amused, so it's all good. My Uno has 13 digital pins, which can be configured as either input or output. So far I've only used them as outputs to blink leds, but that's all it takes to get your mind churning with all sorts of ridiculous shit you want to build. I'm probably a bit early on posting this as I really have nothing to add that hasn't already been done by school kids in Malaysia, but I thought I'd share nonetheless. I'm going to take you through my very first project, the hello world of Arduino, to show you the evolution of a retard's understanding. I'll play the retard.

Durr Hurr, it begins

So here's a pic of my first project in all it's glory.

I should note that the Arduino does not come mounted in anything. What you see there is a little extra. As you can see, I got the tray that holds the Arduino from sparkfun. That is also where I got the Arduino, but the breadboard came from RadioShack as did the leds.

I didn't take any pics of when I had only one led blinking on there. I felt that taking pics of that would have been pretty retarded, even for me, plus I forgot. Once that lonely led began to blink I immediately dug out a few more leds to play with more outputs at once, and that is when I started taking pics. Just for posterity's sake and the irresistible urge to be a dork, Imma post the code from my first project. Really it's just a copy pasta (fuck you 4chan) of the hello world code from the Ardiuno playground. Well, before I post it maybe I should back up just for a second for those even less familiar than me with how this all works. Basically you write a small amount of code, called a sketch, within the ide. Once you're done writing your sketch you click on the upload button which loads your code onto the micro-controller on the board. In the case of the Uno the micro-controller is the now infamous Atmega 328 in case you were wondering. I've been programming machinery for over 17 years, so I have a slight advantage over a total greenhorn, but only slight. Like any machine that has to do what it's told by a human, it's very fickle. The syntax isn't that hard to grasp in most cases and could even be called simple, but that doesn't guaranteed you wont have problems. Until you get used to writing a particular style or language of code you always have that learning curve. At first you scan right over that semi-colon or curly brace and then scratch your head when shit when won't compile, but over time your mind is trained to code with these subtleties in mind. I would definitely recommend tinkering with and breaking the code you copy pasta from the net. This will make you fluent in Arduino chatter, or at least I'm hoping.

Okay, for my first project I used only one led connected to digital pin 3. I could have picked any pin I wanted, but I stuck with the tutorial to the letter for my very first time ever playing with one of these. Below is a schematic I drew up for you, yer welcome.


Here's the code for the sketch.
/*
1: Hello World!

This is a simple sketch to make an LED blink.
It will introduce you to the very basics of how the Digital Pins work on an Arduino.
The Digital Pins can be used as inputs or outputs. In this case, we'll be outputting an "ON" or
an "OFF" command to an LED.
*/

//we can call our global variables first for convenience (you'll see why as we progress)

int ledPin = 3; //set the pin number for our LED on the Arduino board

int delayTime = 1000; //how fast do we want it to blink in milliseconds?

//The code in setup() runs once when we upload to the board
//We can put anything we only need to do once in here

void setup(){
Serial.begin(9600); //Open the Serial Port at a specified speed

pinMode(ledPin, OUTPUT); //Tell the Arduino that the LED pin is an output

}

//The code in this section runs continuously from top to bottom i.e loops

void loop(){
digitalWrite(ledPin, HIGH); //switch the led at ledPin HIGH (on!)
delay(delayTime);
digitalWrite(ledPin, LOW);
delay(delayTime); //wait again
}

It works! Here's me as soon as I saw the led blinking.


As soon as I saw that thing blinking I grabbed a few more leds so I could try to code and build a light bar like what Kitt used to have. You remember Knight Rider, don't you? Here's the schematic. I totally suck at gEda, so the schematics suck severely.


Here's the sketch;
/*
1: Hello World!
This is a simple sketch to make an LED blink.
It will introduce you to the very basics of how the Digital Pins work on an Arduino.
The Digital Pins can be used as inputs or outputs. In this case, we'll be outputting an "ON" or
an "OFF" command to an LED.
*/

//we can call our global variables first for convenience (you'll see why as we progress)

int ledPin1 = 3; //set the pin number for our LED on the Arduino board
int ledPin2 = 4;
int ledPin3 = 5;
int ledPin4 = 6;

int delayTime = 100; //how fast do we want it to blink in milliseconds?

//The code in setup() runs once when we upload to the board
//We can put anything we only need to do once in here

void setup(){
Serial.begin(9600); //Open the Serial Port at a specified speed

pinMode(ledPin1, OUTPUT); //Tell the Arduino that the LED pin is an output
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
}

//The code in this section runs continuously from top to bottom i.e loops

void loop(){
digitalWrite(ledPin1, HIGH); //switch the led at ledPin HIGH (on!)
delay(delayTime);
digitalWrite(ledPin2, HIGH);
delay(delayTime);
digitalWrite(ledPin1, LOW);
delay(delayTime);
digitalWrite(ledPin3, HIGH);
delay(delayTime);
digitalWrite(ledPin2, LOW);
delay(delayTime);
digitalWrite(ledPin4, HIGH);
delay(delayTime);
digitalWrite(ledPin3, LOW);
delay(delayTime);
digitalWrite(ledPin3, HIGH);
delay(delayTime);
digitalWrite(ledPin4, LOW);
delay(delayTime);
digitalWrite(ledPin2, HIGH);
delay(delayTime);
digitalWrite(ledPin3, LOW);
delay(delayTime);
digitalWrite(ledPin1, HIGH);
delay(delayTime);
digitalWrite(ledPin2, LOW);
}

And here's the video.



Doing this project taught me some very valuable lessons. The first thing it taught me is something I haven't had to deal with in many years, the need for clean concise code. And it taught me that I'll never work for the jet propulsion lab. See, as mighty as this little thang is, it has a very finite amount of memory. Explicitly telling each led to come on then turn off in a complete loop I use a lot of memory. It's a much more effecient use of the hardware to create a conditional loop which uses a variable rather than explicitly state each pin. This is always the goal of a good coder and is what makes one person better at it than the other. Back in the day, I used to run into this problem a lot when programming really complex geometries on machines with very little memory. I learned quick that it was much easier to loop either a definite shape or a routine that figured it's next point than it was to write a couple thousand lines of code. Most cnc machinists I know of never even think of this type of thing since they use third party software to create their toolpath. This is the main reason why cnc work has lost it's luster for me. The challenge and whatnot are completely fuckin gone. But, I digress...

Pwn'd

I haven't had a change to really play with looping it the right way yet, so I can't post about it. I did however, stumble across a wicked and awesome project on the playground website that totally tripped me out. It seems that these things can do a little trick known as Pulse Width Modulation, or pwm. In a nutshell, the digital outputs are pulsed to simulate analog output. Just light flipping a light switch on and off really fast makes it half dark and half light, thereby making it darker, so goes pwm. The sketch I saw faded a set of 8 lights in a sine wave pattern. Even though the example used 8 leds and I only had 4, that didn't stop me from loading up the sketch and seeing if it would run, and it did. In case you don't know, analog signals create waves, like sound from a speaker. Digital signals only produce a square wave, an on and off, a 1 or a 0. I will definitely be playing with this type of thing with my rodon coil, so more to come on pwm. Anyway, I'm tired. Peace.

No comments:

Post a Comment