Built-in project · Arduino Uno R3

Dinosaur game player: light sensor on A0, servo on 9, on an Arduino Uno

A photoresistor on A0 watches the screen of the browser’s dinosaur game; when a dark cactus passes over it, the servo on pin 9 taps the space bar to jump. The whole build, an Arduino Uno and 3 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Beginner Runs in your browser. Free, and no account needed.

The parts retail for about $33. On Mokxi they're free.

Dinosaur game player: light sensor on A0, servo on 9 · Arduino Uno R3live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// A robot that plays the dinosaur game.
//
//   photoresistor: one leg -> A0, the other -> GND; 10k from A0 -> 5V
//   servo signal -> pin 9   red -> 5V   brown -> GND
//
// On the bench the light sensor is taped to the screen just ahead of the
// dinosaur, and the servo's arm rests over the space bar. The game's
// background is bright; a cactus is dark. With the 10k pulling A0 up and the
// photoresistor pulling it down, a bright screen gives the sensor a low
// resistance and A0 a low reading, and a dark cactus a high one. When the
// reading rises past THRESHOLD, the servo swings down to press the key.
//
// Drag the light level on the photoresistor down to play the cactus.

#include <Servo.h>

const int SERVO_PIN = 9;
const int SENSOR_PIN = A0;
const int THRESHOLD = 310;  // tune this to your screen's brightness
const int REST = 0;
const int PRESS = 30;

Servo arm;

void setup() {
  arm.attach(SERVO_PIN);
  arm.write(REST);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: dinosaur game player, light sensor on A0, servo on 9");
}

void loop() {
  int light = analogRead(SENSOR_PIN);
  Serial.println(light);
  if (light > THRESHOLD) {
    Serial.println("cactus! jump");
    arm.write(PRESS);
    delay(200);
    arm.write(REST);
    delay(300);
  }
  delay(20);
}

Parts list

5 parts, plus the jumper wires. They retail for about $33; on Mokxi they're free.

How it is wired

4 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.

  • Ground: Arduino Uno R3 pin GND; Light sensor pin 1; Hobby servo pin GND
  • Arduino Uno R3 pin 9; Hobby servo pin PWM
  • Arduino Uno R3 pin 5V; Resistor, 10k Ω pin 2; Hobby servo pin VCC
  • Arduino Uno R3 pin A0; Light sensor pin 2; Resistor, 10k Ω pin 1

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.