Dear students:
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:
Let’s make a game—one that uses hardware. The simplest piece of interactive hardware that exists is the push button switch. Surely we can make a game with just one button? When the button is pressed, let’s just have the boss disappear.
The first step is to assemble our controller’s circuit. Your instructor will provide you with a collection of hardware. For input, we’ll use a standard push button. Inspect the one that your instructor has provided. You’ll see it has four pins. We will use just two that are on the same side. Electrons will flow in one and out the other when the button is pressed.
Follow these steps to build your circuit:
All told, your controller should look like this:
An Arduino is a miniature computer. It runs a single program, its firmware, which we write on a standard computer and then upload.
Our program must contain definitions for two functions: setup
and loop
. In setup
, we initialize state and any global variables. In loop
, we do the ongoing work of reading pins and communicating back to the computer.
Follow these steps to get your firmware up and running:
setup
in the following way: void setup() {
pinMode(7, INPUT_PULLUP);
Serial.begin(9600);
}
INPUT
. To communicate back to the computer running the game, we open a serial connection that sends messages at a rate of 9600 bits per second.loop
in the following way: void loop() {
int value = digitalRead(7);
Serial.println(value);
delay(100);
}
value
will be either 0 or 1. We print its current state to the serial port and then delay 100 milliseconds. Note that the function is called loop
, but it doesn’t actually contain a loop. The Arduino tooling automatically inserts a main
function that effectively looks like this:
void main() {
setup();
while (true) {
loop();
}
}
But you don’t write this main
.
Now we’re ready to install and test the firmware.
You should see a number once every 100 milliseconds, which is a lot of output. Let’s fix that with this open-ended challenge:
Serial.println
when the reading changes, that is, when the button changes from pressed to unpressed and vice versa.Once your program is printing 0 and 1 only when the button changes, let’s do one last step:
Serial.println
prints its argument as ASCII text. It’s easier to read binary values in Unity, not ASCII. We want to send a single byte value. Switch Serial.println
to Serial.write
.Now we are ready to hook up this controller to a game in Unity. Complete the following steps to build your game:
Your game is running. There’s the boss—and there’s no way to get rid of it. What a hard game! Let’s get our boss-removing button controller integrated with these steps:
BossController
.BossController
to look like this: using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO.Ports;
public class BossController : MonoBehaviour {
private SerialPort serial;
void Start() {
serial = new SerialPort("PORTNAME", 9600);
serial.Open();
}
void Update() {
if (serial.BytesToRead > 0) {
int state = serial.ReadByte();
Debug.Log(state);
}
}
}
PORTNAME
with the same text that you see in the Arduino IDE for the port.Let’s finish off the boss and the game with an open-ended exercise:
Here’s your TODO list for next time:
See you next time!
Comments