Skip to content

Roulette Wheel

Roulette isn't a great idea in real life. If you bet on red, you're almost flipping a coin — but not quite. The casino adds a green space that makes it a little harder to win. At best, you only have a 47% chance of winning any single spin, and that's not fair!

But a roulette wheel can make a fun light show.

A real roulette wheel with the ball resting in a pocket.


Overview

You'll program the ring of ten pixels on the Circuit Playground Express to act like a spinning roulette "ball" that slows down and stops on its own. Along the way you'll use a variable to track position, an infinite loop to animate it, and a timing trick to make the motion feel real.

Before you start

Connect your Circuit Playground Express and open code.py on the CIRCUITPY drive. If you don't see the drive, ask for help before continuing.

You've got it when…

  • A single white light travels around the ring on its own.
  • The light slows down as it goes.
  • It comes to rest on one pixel and stays there.

Collaboration & AI

Work: On your own. Ask a neighbor for a hand if you're stuck, but the code you submit should be yours.

AI — AIAS Level 1, No AI: This lab is about learning the loop by writing it yourself, so set AI aside for the code this time. What the levels mean.


Make It Spin

First we get a single light moving around the ring. Do the Setup steps before the infinite loop.

  1. Start by importing the libraries we'll need.

    code.py
    from adafruit_circuitplayground import cp
    import time
    
  2. Define some colors so we can use them later.

    RED = (255, 0, 0)
    WHITE = (255, 255, 255)
    BLACK = (0, 0, 0)
    
  3. Turn down the brightness.

    cp.pixels.brightness = 0.2
    
  4. Set the initial colors for the wheel. We'll start by turning off all the pixels by setting their colors to black.

    cp.pixels[0] = BLACK
    cp.pixels[1] = BLACK
    cp.pixels[2] = BLACK
    cp.pixels[3] = BLACK
    cp.pixels[4] = BLACK
    cp.pixels[5] = BLACK
    cp.pixels[6] = BLACK
    cp.pixels[7] = BLACK
    cp.pixels[8] = BLACK
    cp.pixels[9] = BLACK
    
  5. Create a variable to keep track of where the roulette "ball" is.

    i = 0
    

The rest of this section goes inside the infinite loop.

while True:
    # All code that loops goes here!

Warning

Remember to indent all code inside the loop. Code below the infinite loop will never run!

  1. Set the pixel at location i to the color black.

    Need help? Open for a hint.

    Look at how we set each pixel's color in step 4 above. Instead of using an actual number, we'll use the variable i, which is the number of the current pixel in the loop.

  2. Add one to i.

  3. The last pixel on the wheel is number 9. Check if i is now greater than 9. If it is, set it to zero to "wrap around" the roulette wheel.

  4. Set the pixel at location i to the color white.

  5. Use the time.sleep() function to delay for one-tenth of a second.

        time.sleep(0.1)
    

Try your program. You should see a white light move around the Circuit Playground Express.


Make It Stop

Now that we have a spinning "ball" on our roulette wheel, we need to make it slow to a stop. The first step is Setup — it goes before the infinite loop.

  1. Create a variable to keep track of how long we will wait before moving the "ball" to the next pixel.

    delay = 0.1
    
  2. Edit this line of code inside the infinite loop to use the delay variable instead of a constant number.

        time.sleep(delay)
    
  3. Increase the delay each loop so we wait longer and longer before moving the "ball" to the next pixel. This will cause it to gradually slow down. We'll also print the delay to the Serial terminal so we can see it increasing.

        delay = delay + 0.1
        print(delay)
    

    Try your program. The "ball" should still spin and will now slow down as it goes around.

  4. Finally, if the ball is going slow enough, we want to stop it entirely. Here's a neat trick to do this with very little code:

    • At the beginning of the infinite loop, check if delay is greater than some wait time.
    • If it is, use the continue keyword to go on to the next loop.
    • The rest of the code in the loop will just be ignored!
    while True:
        if delay > 1.5:
            continue
    

Try your program. The "ball" should settle at a final location now.

Checkpoint

Your wheel spins, slows, and stops on its own. That's the whole machine — everything from here is polish.


Tuning

If everything is working well, we can tune our program to make it "feel" more realistic. For example, we can edit this line to make the delay increase by 10% each loop, rather than by a constant amount:

delay = delay * 1.1

And we can start with a faster initial speed:

delay = 0.01

And we can reduce the speed where the "ball" settles to a stop:

    if delay > 1:

Try changing these values until you get a result that you like.


Challenges

Please work on these challenges on your own.

  1. Background colors: see if you can change the background colors of every other pixel like a real wheel (red-black-red-black, etcetera).

    1. In the setup, make all even numbered pixels red, and make all odd numbered pixels green.
    2. In the infinite loop, instead of setting the current pixel to black, check if i is an even number like this:

          if i % 2 == 0:
      
    3. If i is even, make it red. Else, make it green.

  2. Make it random: make the ball stop on a random pixel. Here's one way to do it:

    1. Import the random library.
    2. Use random.randint(0, 9) to select a random pixel.
    3. Hint: what if we start on a random pixel instead of worrying about how to stop on one?
  3. Add a button: spin the wheel again when a button is pressed.

    1. Remember that we can check if a button is pressed like this:

          if cp.button_a or cp.button_b:
      
    2. If a button is pressed, reset the delay to what it is in the setup of the program.

    3. Notice that you might have to press and hold the button for a moment to make this work. Why?

Turn It In

  • Submit your code.py file. You'll find it on the CIRCUITPY drive that appears when your board is connected and turned on.

The CIRCUITPY drive in a file browser with code.py selected.


How It's Graded

This lab is worth up to 4 points. One score covers everything you turn in.

Score What it looks like
4 — Excellent Your code.py makes the white ball travel the ring, slow down, and settle on one pixel — with the wrap-around check and the continue trick both doing their jobs — the delay is tuned so the stop feels like a real wheel, and at least one challenge works.
3 — Above Average The ball spins, slows, and stops, with a rough edge — it halts with a jolt because the tuning values were never touched, or a challenge was attempted but doesn't quite work.
2 — Average Half the machine — the ball spins and slows but never comes to rest, or it only circles at a constant speed.
1 — Below Average The light never makes it around the ring, or code.py doesn't run.
0 — Failing No code.py submitted.