Built-in project · ESP32-C3-DevKitM-1

ESP32-C3 Simon

Four LEDs flash a growing sequence, four buttons play it back. The whole build, an ESP32-C3 and 12 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.

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

ESP32-C3 Simon · ESP32-C3-DevKitM-1live0.000 s 0.00x
Hold the button
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 · ESP32-C3-DevKitM-1
// Simon: a memory game on four buttons and four LEDs.
//
// Wiring, board to breadboard:
//   LEDs (each through a 220 ohm resistor to GND): GPIO 2, 3, 4, 5
//   Buttons (each to GND, internal pull-up):       GPIO 18, 19, 20, 21
//
// The game plays a sequence of one to sixteen lamps, one flash at a time, then
// waits for the same sequence back on the matching buttons. Get it right and
// the sequence grows by one; get it wrong and the game reports how far you got
// and starts over. There is no timeout: the buttons are read in a plain loop,
// so a player can take as long as they like on each step.
//
// `random()` is deliberately left unseeded, so the sequence is the same every
// time the board starts: the yellow lamp on GPIO 3 first, every run. Seeding
// it is the lesson's own closing exercise, and seeding it from micros() here
// would make the first lamp a function of how many instructions setup() took,
// which is not something a sketch should depend on.

const int LEDS[] = {2, 3, 4, 5};
const int BUTTONS[] = {18, 19, 20, 21};
const int COUNT = 4;
const int MAX_LEN = 16;

uint8_t sequence[MAX_LEN];
int length = 1;

static void flash(int i, int ms) {
  digitalWrite(LEDS[i], HIGH);
  delay(ms);
  digitalWrite(LEDS[i], LOW);
  delay(ms / 2);
}

// A short flourish so a fresh round looks different from a step of the game.
static void allFlash(int times, int ms) {
  for (int t = 0; t < times; t++) {
    for (int i = 0; i < COUNT; i++) digitalWrite(LEDS[i], HIGH);
    delay(ms);
    for (int i = 0; i < COUNT; i++) digitalWrite(LEDS[i], LOW);
    delay(ms);
  }
}

static int pressedButton() {
  for (int i = 0; i < COUNT; i++) {
    if (digitalRead(BUTTONS[i]) == LOW) return i;
  }
  return -1;
}

static void waitForRelease() {
  while (pressedButton() >= 0) delay(5);
  delay(20);  // a little settle time, cheaper than real debounce logic
}

static void newGame() {
  length = 1;
  for (int i = 0; i < MAX_LEN; i++) sequence[i] = (uint8_t)random(COUNT);
  Serial.println("new game");
  delay(400);
}

void setup() {
  for (int i = 0; i < COUNT; i++) {
    pinMode(LEDS[i], OUTPUT);
    digitalWrite(LEDS[i], LOW);
  }
  for (int i = 0; i < COUNT; i++) pinMode(BUTTONS[i], INPUT_PULLUP);
  Serial.begin(115200);
  Serial.println("Mokxi ESP32-C3: Simon, four lamps and four buttons");
  newGame();
}

void loop() {
  // Play the sequence so far.
  for (int i = 0; i < length; i++) flash(sequence[i], 350);
  delay(200);

  // Read it back.
  for (int i = 0; i < length; i++) {
    int got = -1;
    while (got < 0) got = pressedButton();
    digitalWrite(LEDS[got], HIGH);

    if (got != sequence[i]) {
      digitalWrite(LEDS[got], LOW);
      Serial.print("wrong at step ");
      Serial.print(i + 1);
      Serial.print(" of ");
      Serial.println(length);
      allFlash(3, 120);
      waitForRelease();
      newGame();
      return;
    }

    waitForRelease();
    digitalWrite(LEDS[got], LOW);
  }

  Serial.print("round ");
  Serial.print(length);
  Serial.println(" correct");
  length++;
  if (length > MAX_LEN) {
    Serial.println("full sequence, starting over");
    allFlash(5, 100);
    newGame();
    return;
  }
  delay(500);
}

Parts list

14 parts, plus the jumper wires. Every one is in the editor's parts bin.

How it is wired

13 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.

  • ESP32-C3-DevKitM-1 pin 2; Resistor, 220 Ω (1) pin 1
  • ESP32-C3-DevKitM-1 pin 3; Resistor, 220 Ω (2) pin 1
  • Ground: ESP32-C3-DevKitM-1 pin GND; LED, red pin C; LED, yellow pin C; LED, green pin C; LED, blue pin C; Pushbutton (1) pin 2a; Pushbutton (1) pin 2b; Pushbutton (2) pin 2a; Pushbutton (2) pin 2b; Pushbutton (3) pin 2a; Pushbutton (3) pin 2b; Pushbutton (4) pin 2a; Pushbutton (4) pin 2b
  • ESP32-C3-DevKitM-1 pin 4; Resistor, 220 Ω (3) pin 1
  • ESP32-C3-DevKitM-1 pin 5; Resistor, 220 Ω (4) pin 1
  • ESP32-C3-DevKitM-1 pin 18; Pushbutton (1) pin 1a; Pushbutton (1) pin 1b
  • ESP32-C3-DevKitM-1 pin 19; Pushbutton (2) pin 1a; Pushbutton (2) pin 1b
  • ESP32-C3-DevKitM-1 pin 20; Pushbutton (3) pin 1a; Pushbutton (3) pin 1b
  • ESP32-C3-DevKitM-1 pin 21; Pushbutton (4) pin 1a; Pushbutton (4) pin 1b
  • Resistor, 220 Ω (1) pin 2; LED, red pin A
  • Resistor, 220 Ω (2) pin 2; LED, yellow pin A
  • Resistor, 220 Ω (3) pin 2; LED, green pin A
  • Resistor, 220 Ω (4) pin 2; LED, blue pin A

Change it and keep it

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