teaching machines

CS 491: Lecture 3 – Peggle

February 12, 2018 by . Filed under gamedev, lectures, spring 2018.

Dear students:

Last time we started working on a game of Peggle. The real thing is full of glitz. We’ll keep working on ours today, but it won’t quite match the professionals:

I’ve expanded our controller a bit so it has both a push button switch to fire and a potentiometer to aim the cannon.

This leads us to a little segment I like to call Program This:

Person A, pretend you’re an Arduino. You’ve got two inputs connected to you. One is a digital input—a button for which you will send out 0 or 1. The other is analog—a potentiometer for which you will send out 0-255. You only send an input’s data when it changes.

Persons B and C, imagine you are the receivers of the data.

Person A, start generating messages. Decide at random which input fires an event. Persons B and C, what needs to be true for you to understand what’s going on?

An Arduino program that reads both the button and the potentiometer might look like this:

#include "Arduino.h"
#define THRESHOLD 5

// I use this to toggle between debugging with the
// serial monitor (plaintext) and communicating with
// Unity (binary).
/* #define write println */

int old_potentiometer;
int old_button = LOW;

void setup() {
  pinMode(7, INPUT);


void emitPotentiometer(int potentiometer) {
  int byte = map(potentiometer, 0, 1023, 0, 255);
  old_potentiometer = potentiometer;

void loop() {
  int potentiometer = analogRead(A5);
  if (abs(old_potentiometer - potentiometer) > THRESHOLD) {

  int button = digitalRead(7);
  if (button != old_button) {
    if (button == LOW) {
    old_button = button;

We solve the communication problem by establishing a protocol. Each packet is two bytes. The first indicates the device: 0 for the potentiometer, 1 for the button. The second indicates the state of the device: a value in 0-255 for the potentiometer, and a 1 for the button, as we only care about release events.

Here’s our checklist to get this game up and running:

Here’s your TODO list:

See you next time!


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