Built-in project · Arduino Uno R3

Lion dance head on an Arduino Uno

A lion dance head opens its servo eyelids, flashes its LED eyes to a piezo drum beat set by a knob, and goes wild when you press the button to feed it the greens. The whole build, an Arduino Uno and 8 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.

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

Lion dance head · Arduino Uno R3live0.000 s 0.00x
Turn the knob for the tempo, and press the button to feed the lion
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
// Lion dance head: a lion that wakes up, blinks its big eyes and dances to a
// drum beat, and goes wild when you feed it the greens.
//
//   eyes      pins 5 and 6 -> 220 ohms -> LED -> GND, one each
//   eyelids   servo PWM -> pin 9, VCC -> 5 V, GND -> GND
//   drum      pin 8 -> piezo -> GND
//   tempo     potentiometer wiper -> A0, ends to 5 V and GND
//   greens    pin 2 -> button -> GND        (INPUT_PULLUP)
//
// Lion dances bring good luck at Lunar New Year, to the beat of a drum,
// cymbals and a gong. Before a new lion dances for the first time, its eyes
// are dotted to wake it, so this one starts with its eyes shut and opens them
// slowly.
//
// The drum plays an eight-step pattern: a low boom on the strong beats and a
// higher tap on the others. The eyes flash on every boom and the eyelids
// blink now and then. The knob sets the tempo.
//
// The button is the greens. In the custom called cai qing, plucking the
// greens, the lion reaches for lettuce hung up high, often with a red
// envelope tied to it. Press it and the drum rolls, the eyes flash fast, and
// then the lion settles back into its dance.
//
// Things to try. Turn the knob. Press the button. Write your own rhythm into
// PATTERN: 2 is a boom, 1 a tap, 0 a rest.

#include <Servo.h>

const int EYE_L = 5, EYE_R = 6;
const int LID_PIN = 9;
const int DRUM_PIN = 8;
const int GREENS_PIN = 2;
const int OPEN = 20, SHUT = 100;

const uint8_t PATTERN[8] = {2, 0, 1, 1, 2, 0, 1, 0};
Servo lids;

int step = 0;
unsigned long nextStep = 0;
unsigned long nextBlink = 3000;

void eyes(int level) {
  analogWrite(EYE_L, level);
  analogWrite(EYE_R, level);
}

void wake() {
  // Eyes shut, then opening slowly as they light.
  for (int a = SHUT; a >= OPEN; a -= 2) {
    lids.write(a);
    eyes(map(a, SHUT, OPEN, 0, 120));
    delay(30);
  }
  Serial.println("The lion is awake");
}

void greens() {
  Serial.println("Cai qing: the lion plucks the greens!");
  for (int i = 0; i < 24; i++) {
    tone(DRUM_PIN, i % 2 ? 150 : 110, 40);
    eyes(i % 2 ? 255 : 30);
    lids.write(i % 4 < 2 ? OPEN : OPEN + 30);
    delay(60);
  }
  eyes(120);
  lids.write(OPEN);
  Serial.println("Back to the dance");
}

void setup() {
  Serial.begin(9600);
  pinMode(EYE_L, OUTPUT);
  pinMode(EYE_R, OUTPUT);
  pinMode(GREENS_PIN, INPUT_PULLUP);
  lids.attach(LID_PIN);
  lids.write(SHUT);
  wake();
}

void loop() {
  unsigned long now = millis();
  if (digitalRead(GREENS_PIN) == LOW) {
    greens();
    while (digitalRead(GREENS_PIN) == LOW) delay(10);
    return;
  }

  if (now >= nextStep) {
    // 80 to 200 beats a minute, two steps to a beat.
    int bpm = map(analogRead(A0), 0, 1023, 80, 200);
    nextStep = now + 30000UL / bpm;
    uint8_t hit = PATTERN[step];
    if (hit == 2) {
      tone(DRUM_PIN, 110, 70);
      eyes(255);
    } else if (hit == 1) {
      tone(DRUM_PIN, 220, 30);
      eyes(160);
    } else {
      eyes(90);
    }
    if (step == 0) {
      Serial.print("Drum at ");
      Serial.print(bpm);
      Serial.println(" beats a minute");
    }
    step = (step + 1) % 8;
  }

  // A blink every few seconds: shut and open again.
  if (now >= nextBlink) {
    lids.write(SHUT);
    delay(150);
    lids.write(OPEN);
    nextBlink = now + random(2500, 6000);
  }
}

Parts list

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

How it is wired

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

  • Arduino Uno R3 pin 9; Hobby servo pin PWM
  • Arduino Uno R3 pin 8; Piezo speaker pin 1
  • Arduino Uno R3 pin 6; Resistor, 220 Ω (2) pin 1
  • Arduino Uno R3 pin 5; Resistor, 220 Ω (1) pin 1
  • Arduino Uno R3 pin 2; Pushbutton pin 1a
  • Arduino Uno R3 pin 5V; Potentiometer pin 3; Hobby servo pin VCC
  • Ground: Arduino Uno R3 pin GND; Potentiometer pin 1; Pushbutton pin 2a; LED, red (1) pin C; LED, red (2) pin C; Piezo speaker pin 2; Hobby servo pin GND
  • Arduino Uno R3 pin A0; Potentiometer pin 2
  • Resistor, 220 Ω (1) pin 2; LED, red (1) pin A
  • Resistor, 220 Ω (2) pin 2; LED, red (2) 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.