Hodor evolves.

Hodor evolves.

My baby boy Hodor is really growing up. Yesterday I figured out how to have him control multiple LEDs individually, which is important because Rube Goldbot is going to need something that can control solenoids individually in order to hit various keys on my keyboard. This is good – but not good enough. Right now, Hodor is only capable of saying things that are hard coded in an endless loop. Hodor just Hodors all Hodo— er, day, if left to his own devices.

What I need is for Hodor to listen to commands in sequences that aren’t part of his programming and respond to those commands appropriately. If Grace (once she actually exists) tells Hodor to press a key, I need Hodor to be able to listen and do what she says.

Right now, the only way I can think of to get data to Hodor is through USB – that’s the only “input” I’ve got. Googling “Arduino listen to USB” (and then refining my Google searches multiple times) I found the Serial library and functionality for Arduino. Serial is going to let Hodor listen. Hodor can also talk back through Serial, which is great, so I can have Hodor let the world know that he’s ready to do whatever we tell him.

I’m modifying my global variables – I’ve got (for now) 4 LEDs set up on pins 9 through 12 on my board. I’ll have them each correspond to a given letter, a through d:

int a = 12;
int b = 11;
int c = 10;
int d = 9;

Next I need to modify my setup() functionality. I want to get Serial configured and I want Hodor to gleefully announce that he is ready:

void setup()
{
  // Tell all pins corresponding to my LEDs that they are OUTPUTS
  pinMode(a,OUTPUT);
  pinMode(b,OUTPUT);
  pinMode(c,OUTPUT);
  pinMode(d,OUTPUT);

  //Tell Serial it's speed 
  Serial.begin(9600);

  //Call Hodor function and tell it to blink excitedly 3 times
  hodor(3);
}

The hodor function looks like this:

void hodor(int blinks)
{
  Serial.println("Hodor!");
  blinkAll(blinks);
  Serial.println();
}

The blinkAll function just tells all the lights to turn on and off “blinks” number of times and really isn’t worth showing.

So here’s where the magic happens:

void loop()
{
  String str;

  if(Serial.available() > 0){
    str = Serial.readStringUntil('\n');
    str.toLowerCase();
    if(str=="hodor")
    {
      hodor(10);
    }
    else
    {
      blinkString(str);
    }
  }
}

What happens here is that Hodor listens to the Serial input and, if there’s anything there, listens to serial until there’s a terminating value at the end of whatever input there was. After that it converts the input to lower case (easier to do string comparisons with that. If the string is “hodor” Hodor becomes very excited and runs the hodor() function 10 times. If it’s anything else it goes to our blinkString() function.

void blinkString(String str)
{
  for(int x=0; x<str.length(); x++)
  {
    switch(str[x])
    {
      case 'a':
        dot(a);
        break;
      case 'b':
        dot(b);
        break;
      case 'c':
        dot(c);
        break;
      case 'd':
        dot(d);
        break;
      default:
        break;
    }
    Serial.println(str[x]);
  }
}

This takes the string we got from Serial and iterates through each character in it. Then we do a switch and if the character is any of the ones we have a light for, it tells the corresponding light to do a dot() call (as described previously).

I’ve got this working – it works exactly as intended, and I’m starting to feel reasonably confident with the very, very simple things now. There’s still a ton more for me to learn about the electronics, but right now Hodor is in a good state.

By good state, what I mean is this: we have, in essence, all we need from Hodor. Hodor can control external components, telling them to turn on and off – this can be LEDs, this can be solenoids, it doesn’t matter. Hodor can also LISTEN to incoming commands that come in through the USB port – and these commands can come from a keyboard, from another program speaking on the USB port, or whatever else. Hodor can also give us a status update when he comes online or at any other time he wants to let us know something important.

Since I don’t have any of my solenoids yet, I can’t really do the full-on hardware version. I also need to figure out how to scale this sucker up from 4 LEDs to 100+ whatevers I’ll be controlling. I’ve heard there are things called shift registers that will allow me to extend the number of outputs greatly, but I have no idea how to do that. So, for now, I’m going to table any of the big hardware changes to Hodor and focus on integrating him with the larger Rube Goldbot system.

With what I have working right now I could do the simple version of Hodor that doesn’t rely on solenoids but instead outputs through another USB port to, say, the computer playing World of Warcraft. I think I need something called a USB shield in order to add another USB port, but in any case, this is now pretty straightforward, I think.

Hodor has made his momma so proud!

My next steps will probably be to come up with a really simple version of Grace and get her talking to Hodor. Grace is going to have a much bigger brain than Hodor – she’ll be running on a Raspberry Pi, with much more memory and capability. She’ll also be programmed using either Lisp or Haskell, languages I know literally nothing about other than their names and that they’re “functional” languages, whatever that means.

Leave a Reply

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