Projects / Higher or lower number game

Higher or lower number game

Beginner1.2 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. NANO12:45

    Install the TM1637Display library

    From the Library Manager.

  2. NANO

    Wire the encoder

    CLK to D2, DT to D3, SW to D4, plus 5V and GND.

  3. NANO

    Wire the display and buzzer

    Display on D5 and D6, buzzer on D8.

  4. NANO

    Upload the sketch

    The display shows 50.

  5. NANO

    Turn the knob

    The guess changes, faster if you turn fast.

  6. NANO

    Press the knob

    UP or dn, with a beep.

  7. NANO

    Find the number

    The display shows how many tries you used.

In short

How do you build a higher or lower number game?

The Nano thinks of a number from 1 to 100 and you dial in guesses with a rotary knob on a four-digit display, with arrows or beeps telling you to go higher or lower. A good small game that shows how a smart player finds the number in seven guesses by halving the range. You need Arduino Nano, Rotary encoder module (KY-040), TM1637 4-digit display, Passive buzzer, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm). It takes 7 steps and about 70 minutes.

Level
Beginner
Time
About 70 min
Parts
6

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
Encoder CLKNano D2Turn pulses
Encoder DTNano D3Direction
Encoder SWNano D4Press to submit a guess
Encoder + and GNDNano 5V and GNDPower for the module
TM1637 CLK and DIONano D5 and D6Number display
TM1637 VCC and GNDNano 5V and GNDPower
Passive buzzer + and -Nano D8 and GNDHigh and low beeps

Code

The Nano picks a secret number from 1 to 100. Turn the knob to set your guess on the display, turning quickly to move in steps of five, and press the knob to submit it. A high beep and UP on the display mean the secret is higher than your guess, and a low beep and dn mean it is lower. When you guess right the screen shows how many tries it took, and pressing the knob starts a new game. The best score is kept while the Nano is powered. The best strategy is to halve the range each time: guess 50, then 25 or 75, and so on, which always finds the number within seven guesses, because 2 to the power 7 is 128. If the knob turns the wrong way, swap the sign on the direction line. Cheap encoders can bounce, so if the number skips, hold the knob firmly or add a 100 nanofarad capacitor from each of CLK and DT to GND. Install "TM1637Display" by Avishay Orpaz from the Library Manager. Try letting the Nano guess your number instead, using the same halving idea.

Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

// Library: TM1637Display (by Avishay Orpaz).
#include <TM1637Display.h>

const uint8_t ENC_CLK = 2;
const uint8_t ENC_DT = 3;
const uint8_t ENC_SW = 4;
const uint8_t DISP_CLK = 5;
const uint8_t DISP_DIO = 6;
const uint8_t BUZZ = 8;

TM1637Display display(DISP_CLK, DISP_DIO);

// Letters made from segments: "UP" and "dn"
const uint8_t SEG_UP[] = {0x3E, 0x73, 0x00, 0x00};
const uint8_t SEG_DN[] = {0x5E, 0x54, 0x00, 0x00};

uint8_t secret = 1;
int guess = 50;
uint8_t tries = 0;
uint8_t best = 0;
bool won = false;
bool prevClk = true, prevSw = true;
unsigned long lastTurn = 0;

void newGame() {
  secret = random(1, 101);
  guess = 50;
  tries = 0;
  won = false;
  display.showNumberDec(guess, false);
  Serial.println(F("I am thinking of a number from 1 to 100."));
}

void setup() {
  pinMode(ENC_CLK, INPUT_PULLUP);
  pinMode(ENC_DT, INPUT_PULLUP);
  pinMode(ENC_SW, INPUT_PULLUP);
  pinMode(BUZZ, OUTPUT);
  Serial.begin(9600);
  display.setBrightness(3);
  randomSeed(analogRead(A0) ^ micros());
  newGame();
}

void loop() {
  unsigned long now = millis();

  bool clk = digitalRead(ENC_CLK);
  if (!clk && prevClk && !won) {                         // one step of the knob
    int dir = digitalRead(ENC_DT) ? 1 : -1;              // swap if it turns the wrong way
    int step = (now - lastTurn < 40) ? 5 : 1;            // turn fast to move fast
    lastTurn = now;
    guess = constrain(guess + dir * step, 1, 100);
    display.showNumberDec(guess, false);
  }
  prevClk = clk;

  bool sw = digitalRead(ENC_SW);
  if (!sw && prevSw) {
    delay(20);
    if (won) {
      newGame();
    } else {
      tries++;
      if (guess == secret) {
        won = true;
        if (best == 0 || tries < best) best = tries;
        tone(BUZZ, 1500, 100); delay(150);
        tone(BUZZ, 2000, 200);
        Serial.print(F("Right in "));
        Serial.print(tries);
        Serial.print(F(" tries. Best: "));
        Serial.println(best);
        display.showNumberDec(tries, false);             // shows how many tries it took
      } else if (guess < secret) {
        tone(BUZZ, 1200, 120);
        display.setSegments(SEG_UP);                     // the number is higher
        delay(700);
        display.showNumberDec(guess, false);
      } else {
        tone(BUZZ, 400, 120);
        display.setSegments(SEG_DN);                     // the number is lower
        delay(700);
        display.showNumberDec(guess, false);
      }
    }
  }
  prevSw = sw;
}

What the serial monitor should show

Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual Arduino Nano with its sensors at their default values. Your numbers will follow your real readings, but the format should match.

I am thinking of a number from 1 to 100.

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Turn the knobGuess rises and falls
Fast turnSteps of five
Guess too lowHigh beep and UP
Guess too highLow beep and dn
Right guessRising beeps and the number of tries

If it does not work

Knob turns the wrong way

Swap the sign on the direction line.

Number skips

Add 100 nanofarad capacitors from CLK and DT to GND.

Display blank

Check CLK to D5 and DIO to D6.

Press does nothing

Check SW goes to D4.

Library not found

Install TM1637Display.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the higher or lower number game?

Arduino Nano, Rotary encoder module (KY-040), TM1637 4-digit display, Passive buzzer, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).

How long does the higher or lower number game take to build?

About 70 minutes for a beginner maker, in 7 steps.

How do I know the higher or lower number game works?

Turn the knob: Guess rises and falls. Fast turn: Steps of five. Guess too low: High beep and UP.

Knob turns the wrong way: what should I check?

Swap the sign on the direction line.

Number skips: what should I check?

Add 100 nanofarad capacitors from CLK and DT to GND.

Display blank: what should I check?

Check CLK to D5 and DIO to D6.

Press does nothing: what should I check?

Check SW goes to D4.