First steps with Hodor and Arduino

First steps with Hodor and Arduino

If we think of projects as our children, then I am a parent who is completely unprepared. This is very true of my baby Hodor, who is going to be the brawn of my World of Warcraft playing robot, Rube Goldbot. I don’t know anything about the underlying technology I’m going to use to make him, the fore of which is the Arduino.

Conceptually, of course, I get that the Arduino is a microcontroller – it is capable of doing some computation and then sending signals out to other electronic parts who have names and purposes I don’t know anything about either, but at least this knowledge is a start.

For my purposes, I need to be able to get the Arduino to control “stuff” based on computational logic, so I figure I should start with getting it to control an LED. My first effort basically duplicated what I saw in a tutorial:

// Eww, a global variable. But useful: 13 is the pin (or something) for an on-board LED?
int LED = 13; 

void setup()
{
	// Tells LED that it's gonna be an output (can an LED be an input? How would that even work? Wait, no, it just tells the PIN it's going to be an output. Okay that makes sense...)
	pinMode(LED,OUTPUT);
}

// The main logic .. loop. It's a convention
void loop()
{
	digitalWrite(LED,HIGH); // Tell LED to get power
	delay(500); // Wait 500 milliseconds
	digitalWrite(LED,LOW); // Tell LED to not get power
	delay(500); // Wait 500 milliseconds
}

I’m using Fritzing for my programming and circuit diagramming needs. So I put the above code into the editor, plug in my Arduino via USB, tell Fritzing to publish it and… It tells me I need to install the Arduino app in order to actually deploy my code. So I do that and configure it and then…

IT’S ALIVE! Hodor is blinking! And like the real Hodor all he can say is one thing: blink blink blink. This still isn’t controlling anything off of the Arduino board, but it’s at least doing SOMETHING. Since I’m already in code mode, I might as well improve Hodor’s vocabulary a bit. I’m going to give him two different kind of blinks – fast and slow or dot and dash.

To do this, I add the following code:

void dot(int led)
{
	digitalWrite(led,HIGH);
	delay(250);
	digitalWrite(led,LOW);
	delay(250);
}

void dash(int led)
{
	digitalWrite(led,HIGH);
	delay(500);
	digitalWrite(led,LOW);
	delay(500);
}

These two methods (er, functions? This is C, I guess.) take an int that I’m calling ‘led’ (to not confuse it with the global variable I’m using) and then tell it to turn on and off for either 250 or 500 milliseconds. I guess I could have had them just used the global LED variable for now, but I’m planning on controlling around 100 solenoids eventually, so I might as well think in those terms to start with.

I update my loop() to be:

void loop()
{
	dot(LED);
	dash(LED);
}

And voila! Hodor can now do a quick or slow blink! Dots and dashes are the foundation of Morse code, so I’m going to have a little fun with this. I’m going to create another handful of functions:

void d(int led)
{
	dash(led);
	dot(led);
	dot(led);
}

void h(int led)
{
	dot(led);
	dot(led);
	dot(led);
	dot(led);
}

void o(int led)
{
	dash(led);
	dash(led);
	dash(led);
}

void r(int led)
{
	dot(led);
	dash(led);
	dot(led);
}

Then I’ll update my loop():

void loop()
{
	h(LED);
	o(LED);
	d(LED);
	o(LED);
	r(LED);
	delay(1000);
}

Save this and upload it and now Hodor can Hodor to his heart’s content. If this were C# (what I usually code in), I’d probably have made something like a Dictionary<char, func> and had each letter of the alphabet stored in it along with the corresponding func to do dots and dashes following morse, and then done something like make another method that would take any arbitrary string rip through it character by character to generate the proper blinks. Since it’s C or C++ and I have literally zero idea how to do any of that, for right now I’m going to settle for Hodor being dumb and his code also being pretty dumb as well.

The next step is getting Hodor to say something with an LED that isn’t part of the Arduino. I have zero clue how to do this kind of thing at all, but there are a ton of video tutorials out there for Baby’s First Blinking Light. The tutorials tell me I need to:

  1. Run power from the port on the Arduino I want to send my output to over to the longer pin on the LED
  2. Connect a resistor (for reasons I don’t fully understand) to the other pin on the LED
  3. Connect the other pin on the resistor to ground (and to be honest, I still don’t know exactly what “grounding” is, but that’s for some later reading…)
  4. Connect the ground to the ground on the Arduino

Conceptually this makes sense – I need to run power to the LED and then have it complete the circuit. The details – why do I need a resistor (my guess is because I need to limit the amount of juice being fed to the LED – but how do I *KNOW* this?) and how to tell what “size” resistor I have (presumably the colored bands on them are a code to that, but a sucky one since my eyes are terrible),  and what is grounding are murky, but I’ll figure that out in time.

For now, I have plugged all this stuff together. I tossed a green LED into a spot on the breadboard in the Arduino kit – long pin to the right, short to the left. I put a resistor in with one pin in the square port under the short LED pin, and then the other resistor pin to the left of that. Then I put a wire from port 13 on the Arduino to the spot below the long pin on the LED, put another wire from below the left side resistor pin and ran that to ground. Insert meme cat in a lab with “I have no idea what I’m doing” here – this can’t POSSIBLY WORK…

… plug the Arduino USB cable in… everything is going to explode… I’m going to die horribly…

IT WORKED. What the hell? Both the onboard LED and the one on my breadboard are blinking out Hodor in unison.

This is sorcery, and a girl could get used to that.

Baby steps with Hodor: ACCOMPLISHED!

 

Leave a Reply

Your email address will not be published. Required fields are marked *