GAMERIFTRobotics & Coding Shop kits
HomeKitsFor schoolsAboutGAMERIFT Store ↗
Free sample mission · no code needed

The Mood Machine

Give three LEDs a personality using a 120-year-old piece of mathematics, the same idea that, scaled up a few trillion times, powers modern AI. This one's on us: the complete GAMERIFT mission format, free.

BRIEF Build a robot with moods

Machines follow rules. But what if the rule is a roll of the dice? Today you build a robot with three moods, calm, curious, alarmed, that drifts between them unpredictably, yet with unmistakable personality. One robot might be chilled and hard to startle; another might be a nervous wreck. The difference is nine numbers you choose. Those nine numbers are called a Markov chain, and by the end of this mission you'll understand a real piece of the mathematics behind weather forecasts, Google's original search ranking, and the AI chatbots everyone's talking about.

Time: 50 minutes · Difficulty: 2/5 · You need to know: how to wire an LED and upload a sketch (covered in Beginner Kit Lessons 01–02, but the wiring table below is enough to follow along).

KIT What you need

Concept · The Markov chain, memory of exactly one moment

A Markov chain is a system that hops between states (our moods), where the chance of each next state depends only on the current one, not on the whole history. A calm robot doesn't care that it was alarmed an hour ago; only now decides next. Mathematician Andrey Markov worked this out around 1906, using the letters in a Russian poem. Today the same idea forecasts weather, models queues at the bank, and ranked the early web.

Concept · The transition table, personality as numbers

Nine numbers, arranged three-by-three: each row says "when I'm in this mood, here's my percentage chance of each mood next." Row CALM might read 70–25–5: mostly stays calm, sometimes gets curious, rarely panics. Change the numbers, change the character. Personality is just probabilities, that's the whole secret.

Concept · The AI connection, this is the great-grandfather of ChatGPT

A modern language model answers one question, billions of times per sentence: "given where I am now, what's the probability of each thing that could come next?" That is a Markov chain's question, asked with vastly more states and a much longer memory. When your robot picks its next mood from a probability table, it is doing, in miniature, honestly, what the world's biggest AI systems do. You're not playing with a toy version of the idea. You're playing with the idea.

BUILD Wire it

USB unplugged while you wire. Three LEDs, each with its resistor bodyguard, sharing one ground rail:

FromToMeaning
R3 GNDBlue (−) railShared ground for all three
Pin 11Resistor → LED long legCALM (green if you have it)
Pin 12Resistor → LED long legCURIOUS (yellow)
Pin 13Resistor → LED long legALARMED (red)
Each LED short legBlue (−) railThree loops complete

CODE Program it

// GAMERIFT free mission, The Mood Machine
// A three-state Markov chain with an attitude.

const int calmLED = 11, curiousLED = 12, alarmedLED = 13;

// The personality: each row is a mood, each number a % chance.
// Row order: CALM, CURIOUS, ALARMED. Rows must add up to 100.
int personality[3][3] = {
  { 70, 25,  5 },   // from CALM:    mostly stays calm
  { 40, 40, 20 },   // from CURIOUS: could go either way
  { 10, 10, 80 }    // from ALARMED: hard to calm down
};

int mood = 0;   // 0 = calm, 1 = curious, 2 = alarmed, start calm

void setup() {
  pinMode(calmLED, OUTPUT);
  pinMode(curiousLED, OUTPUT);
  pinMode(alarmedLED, OUTPUT);
  randomSeed(analogRead(A0));   // real randomness: electrical noise from an empty pin
}

void loop() {
  // 1. show the current mood (only the matching LED burns)
  digitalWrite(calmLED,    mood == 0 ? HIGH : LOW);
  digitalWrite(curiousLED, mood == 1 ? HIGH : LOW);
  digitalWrite(alarmedLED, mood == 2 ? HIGH : LOW);
  delay(1000);   // hold each mood for a second

  // 2. roll the dice (0–99) and let the current row decide what's next
  int roll = random(100);
  if      (roll < personality[mood][0])                          mood = 0;
  else if (roll < personality[mood][0] + personality[mood][1])   mood = 1;
  else                                                           mood = 2;
}

(The ? : lines are shorthand for if/else: "if the mood matches, HIGH, otherwise LOW.") Upload it and just… watch for a minute. Long calm stretches. A flicker of curiosity. And occasionally, a panic that takes ages to settle, because row three makes alarm sticky. Nobody scripted that behaviour. It emerged from nine numbers and a dice roll. That feeling of watching something decide is why this maths conquered the world.

BREAK IT Now break it on purpose

  1. Make every number 33 (near enough, rows of 34, 33, 33). Upload. The personality vanishes: pure coin-flip chaos, no character at all. Personality lives in the imbalance.
  2. Make ALARMED inescapable: set its row to 0, 0, 100. Sooner or later the robot panics, and never, ever recovers. Congratulations: you've built what mathematicians genuinely call an absorbing state. (Also known, in this house, as a robot sulk.)
  3. Make a mood unreachable: set the middle column to 0 in every row. Curious never lights again, a state no path leads to is as good as deleted. Real network engineers hunt exactly this kind of dead node.

REMIX Make it yours

Enjoyed this? It's Mission 13 of 12.

This is the full GAMERIFT mission format, brief, concepts, build, code, break-it, remix, exactly what Beginner Kit owners get twelve more of, from their very first blinking LED to a desk guardian that senses when someone comes near, ending in a certificate of completion you can verify online. The kit ships with every component this lesson used, and the course assumes nothing.