Fork me on GitHub

Project Notes

#833 Solving cassidoo’s moveNums on Arduino

Using an Arduino to interactively solve cassidoo’s interview question of the week (2026-02-09): using a rotary encoder to enter an array on an LED 8x8 matrix, dial-in a target digit on a 7-segment display, and then let it animate the recalculation of the matrix accordingly.

Build

Here’s a quick demo..

clip

Notes

The interview question of the week (2026-02-09) asks us rearrange an array:

Given an integer array and a number n, move all of the ns to the end of the array while maintaining the relative order of the non-ns. Bonus: do this without making a copy of the array!

Example:

moveNums([0,2,0,3,10], 0)
[2,3,10,0,0]

I originally solved this using Factor: see LCK#412 moveNums, but all the while I was visualizing the solution. So why not try to implement it with LEDs and an Arduino?

The basic idea:

  • an 8x8 LED matrix will display our array:
    • 8 members (columns) - fixed size
    • each with a value of 1..8 (rows)
    • I am not allowing “0”, as a blank column on the LED matrix can be misleading
  • a rotary encoder with press-switch is used to enter the array:
    • dial up the desired value
    • click to move to next position
  • once the array has been entered it then moves to a 7-segment display to select the value of “n”:
    • again the rotary encoder is used to dial in the value. it is constrained to select 1..8
    • a final click completes the entry
  • then we just do the magic:
    • re-arrange the columns to move all columns that match the “n” to the right
  • another click will restart the process

The moveNums Algorithm

The algorithm I’ve used here to implement moveNums is an in-place strategy that operates on the array of values int8_t columns[MAX_COLS].

The basic procedure:

  • iterate all columns from position 0
  • if the value doesn’t match n, move to the next column
  • if the value does match n, find the to next column that doesn’t match n: swap values then move to the next column

Here is the core method as implemented in C++ for the Arduino.

For display animation purpose, it updates the column display with a short delay after each swap:

void MatrixDisplay::moveNums(int8_t n) {
  for (uint8_t c = 0; c < MAX_COLS; c++) {
    if (columns[c] != n) continue;

    for (uint8_t swap = c; swap < MAX_COLS; swap++) {
      if (columns[swap] != n) {
        columns[c] = columns[swap];
        columns[swap] = n;
        display_column(c);
        delay(ANIMATION_DELAY);
        display_column(swap);
        delay(ANIMATION_DELAY);
        break;
      }
    }
  }
}

Circuit Design

Key components I am using:

  • an Arduino Uno
  • Rotary Encoder Module with push-button action
  • an 8x8 LED Matrix with SPI interface
  • a 7-segment LED mounted on an adapter board with a SPI-like 74HC595 shift register interface LEAP#178 7-Segment Shift Drive Module

When all the LEDs are on, the current draw is too much to be powered from the Arduino Uno, so a separate 5V power supply is used to power the peripherals.

Designed with Fritzing: see move-nums.fzz.

bb

schematic

Wired together on a breadboard:

bb_build

The Program

This is a quick overview of the program structure:

Non-standard libraries used:

Demonstration

Entered a series of values on the matrix:

test1a

After selecting “3” on the 7-segment display and recalculating the matrix - the column with 3 LEDs has been shifted to the right:

test1b

Here’s a quick demo video..

clip

Credits and References

About LEAP#833 cassidooLED 7-SegmentLED 8x8Rotary Encoder74HC595Arduino

This page is a web-friendly rendering of my project notes shared in the LEAP GitHub repository.

Project Source on GitHub Return to the LEAP Catalog
About LEAP

LEAP is my personal collection of electronics projects - usually involving an Arduino or other microprocessor in one way or another. Some are full-blown projects, while many are trivial breadboard experiments, intended to learn and explore something interesting.

Projects are often inspired by things found wild on the net, or ideas from the many great electronics podcasts and YouTube channels. Feel free to borrow liberally, and if you spot any issues do let me know or send a pull-request.

NOTE: For a while I included various scale modelling projects here too, but I've now split them off into a new repository: check out LittleModelArt if you are looking for these projects.

Project Gallery view the projects as an image gallery Notebook reference materials and other notes Follow the Blog follow projects and notes as they are published in your favourite feed reader