teaching machines

CS 491: Lecture 1 – Push Button

January 29, 2018 by . Filed under gamedev, lectures, spring 2018.

Welcome to CS 491! The Registrar doesn’t really give this elective class a name, but between you and me, I’m calling it Game Development and Physical Computing. The Game Development part means we’re going to be making games. The Physical Computing part means we’re going to be assembling hardware. That hardware will sense the world in order to control our games.

First, let’s address some expectations for this course:

Each week will look like this:

Today’s Monday, so let’s have a normal lecture. In fact, let’s make a game—one that uses hardware. The simplest piece of interactive hardware that exists is the pushbutton switch. It only lets current flow when the button is actively depressed. We will use it to make a one-button game.

The first step is to assemble a circuit. I don’t expect you to know anything about electronics. I know very little myself. We’re here to learn. We’ll use a standard push button, which has four pins. We only need two of them. Electrons will flow in one of them, and out the other if the button is depressed.

We wire it up this way initially:

An Arduino has a builtin main function that runs this algorithm:

void main() {
  setup();
  while (true) {
    loop();
  }
}

We must supply definitions for setup and loop. In setup, we tell the pins whether we will be reading from them or writing to them. We also initialize the serial port, which will be our communication channel to the computer. In loop, which runs repeatedly, we query pin 7 and send its data over the serial port. This Arduino sketch accomplishes these tasks:

#include "Arduino.h"

void setup() {
  pinMode(7, INPUT);
  Serial.begin(9600);
}

void loop() {
  int value = digitalRead(7);
  Serial.println(value);
  delay(100);
}

I use a tool called PlatformIO to compile and upload code to an Arduino and observe the serial port. These are the commands I use most often:

platformio init -b uno             # to initialize a project
platformio run -t upload           # to compile and upload
platformio device monitor -b 9600  # to listen to the serial port

When I run this, I find that the Arduino continues to emit 1s or HIGHs long after I’ve released the button. There’s no longer a closed circuit between the button’s pins, but pin 7 has entered this indeterminate floating state. The Arduino documentation for digitalRead anticipates this. To force pin 7 to go LOW, we need to feed ground into it. Electrical voltage is often described as electrical pressure. That pressure disappears when the button is released, and we pull in ground instead. But we discourage ground by adding some resistance between it and pin 7. We use a 10K ohm resistor. The complete circuit looks like this:

We really only care about the transitions between pressed and non-pressed. Let’s add some history to our Arduino sketch so we detect those change events:

#include "Arduino.h"

int old_value = LOW;

void setup() {
  pinMode(7, INPUT);
  Serial.begin(9600);
}

void loop() {
  int value = digitalRead(7);
  if (old_value != value) {
    Serial.println(value);
    old_value = value;
  }
  delay(100);
}

In fact, for our game, we only really care about down events, so let’s be even more selective. We watch for those transitions that go from LOW to HIGH:

#include "Arduino.h"

int old_value = LOW;

void setup() {
  pinMode(7, INPUT);
  Serial.begin(9600);
}

void loop() {
  int value = digitalRead(7);
  if (value == HIGH && old_value == LOW) {
    Serial.println(value);
  }
  old_value = value;
  delay(100);
}

Now we are ready to hook up this controller to a game in Unity. Our game will be simple since we have only a single button. But that doesn’t mean it can’t completely lack story. Let’s place the player on a planet spinning around its axis. There’s a satellite orbiting the planet that has gone haywire and gets dangerously close to the planet, nearly hitting the player. The player can’t move, but he can reverse the direction of the planet’s spin by pressing the button.

We’ll do as much as we can of the following steps:

I think that’s enough for our first day.

Here’s your TODO list for next time:

Sincerely,

P.S. It’s time for a haiku!

Simple fishing game
One button to cast, reel, net
I’ll call it Clickbait