EN
Cloud
R1BBIT · COMMUNITY

Projects

A question from the R1BBIT community

Projects·R1BBITadmin·· edited

Five Finger Fillet

What you need to build it:
ESP32-S3
TMC2209 stepper motor driver
SG90 servo
NEMA17 stepper motor
8V power supply
Jumper wires
3D-printed case
Screenshot 2026-11-24 at 14.52.27

Assemble everything according to the wiring diagram.
You’ll also need to:
Flash the board (Quick Start link)
Upload the script

R1 BASIC
CONFIG
// ---- knife mechanics ----
// servo that lifts / drops the knife (PWM1 channel 1)
PWM1_CH1 19
// stepper driver: ENABLE pin, STEP pulse (OUT2), direction (OUT3 = DIR)
OUT1 20 ENABLE
OUT2 2
OUT3 3 DIR
// keeps the board powered after the power button is released
POWER_HOLD 17
// ---- e-paper display ----
SPI2_CLK   12
SPI2_MOSI  13
DISPLAY_BUS    SPI
DISPLAY_SPI    2
DISPLAY_CS     11
DISPLAY_DC     10
DISPLAY_RST    9
DISPLAY_BUSY   8
DISPLAY_PWR    6
DISPLAY_PWR_ACTIVE_LOW ON
DISPLAY_DRIVER EPD_154_BWRY
DISPLAY_WIDTH  200
DISPLAY_HEIGHT 200
DISPLAY_DITHER ON
// I2C: SHTC3 sensor (0x70), RTC (0x51), ES8311 codec (0x18)
I2C0_SDA 47
I2C0_SCL 48
// audio: ES8311 codec + NS4150B speaker amplifier
CODEC ES8311
CODEC_ADDR 0x18
CODEC_I2C 0
CODEC_I2S 0
I2S_MCLK 14
I2S_BCLK 15
I2S_WS   38
I2S_DOUT 45
I2S_DIN  16
CODEC_PA_PIN 46
CODEC_PA_ACTIVE_LOW 0
AUDIO_OUT CODEC
// codec power switch, LOW = on
// (was OUT1 42, but OUT1 is already used as ENABLE on GPIO20)
OUT4 42
// BOOT button, pressed = 0
IN1 0
IN1_PULL UP
// battery voltage through a 1/2 divider
ADC_IN1 4
ADC_IN1_ATTEN 11
TIMEZONE -5
// touch pad under the player's hand: hand on = 1, hand off = 0
// (threshold: lower = more sensitive)
IN2 1
IN2_TOUCH 32000
END_CONFIG
// ======================================================================
//  KNIFE GAME
//  The player keeps a hand on the touch pad (IN2 = 1), the knife strikes
//  between the fingers at 5 points along the rail.
//
//   Level 1: AUDIO1, the knife runs 1->5->1 once, 300 ms strike pause
//   Level 2: the same run twice, 100 ms pause, then AUDIO2
//   Level 3: 50 strikes at random points, 80 ms pause, back home,
//            AUDIO3 (win)
//  Hand taken off at any moment of the game -> AUDIO4
//  ("Knew you were a yellow-bellied coward, partner!"),
//  the knife brakes smoothly and returns to point 1.
//
//  Position is tracked in steps (CUR, 0 = point 1), so the knife can
//  always find its way home, even after a move was cut short.
// ======================================================================
ENABLE OFF
// codec power on (LOW = on)
OUT4 OFF
// S12..S45 - number of steps between neighbouring points
// PAUS - strike pause, ms   F - target point   L - last point
// I - step counter in MOVE  P - current speed-up of the step (ramp)
// A - max speed-up          N - steps to make   D - 1 = braking phase
// Z - delay loop counter    Q, T - point / position helpers
VAR POINT=1,S12=68, S23=83, S34=70, S45=96, PAUS,F,L,I,Y,P,A=30,N,D,Z,Q,T
// B - base step delay   CUR - knife position in steps from point 1
// DR - direction (+1 forward, -1 back)   AB - 1 = hand removed (abort)
// T0 - last time the hand was seen   TW - timer for waits   W - wait, ms
// K - loop / strike counter   HOLD - 1 = hand held long enough to start
VAR B, CUR, DR, AB, T0, TW, W, K, HOLD
// ---- speed per level ----
// B  - step delay at the start of a move (bigger = slower)
// A  - how much the delay shrinks while accelerating
//      (B - A = shortest delay = top speed)
// acceleration and braking each take A/5 steps
VAR B1=100, A1=30
VAR B2=80,  A2=30
// level 3 sped up ~2x: was B3=70, A3=35 (top 35), now top 15
VAR B3=45,  A3=30
// how many ms the touch must be released to count as "hand removed"
// (filters out short touch dropouts)
VAR GONE=150
// knife parked, default direction, level 1 speed
PWM1 CH1 50,8,0
DIR OFF
B=B1
A=A1
// =========================== main loop ================================
WHILE (1)
    // ---- wait until the hand lies on the pad for 300 ms in a row ----
    // TW is reset every time the pad reads 0, so HOLD only becomes 1
    // after 300 ms of uninterrupted touch
    PRINT "WAIT HAND"
    HOLD=0
    TW=MILLIS
    WHILE HOLD=0
        IF IN2=0 THEN TW=MILLIS
        IF MILLIS-TW>=300 THEN HOLD=1
        PAUSE 20
    WEND
    GOSUB GAME
    // AB tells how the game ended: 1 = hand removed, 0 = all levels done
    IF AB=1 THEN
        GOSUB LOSE
    ELSE
        GOSUB WIN
    ENDIF
    // wait until the hand is removed, so a new game does not start
    // by itself while the hand is still on the pad
    WHILE IN2=1
        PAUSE 50
    WEND
    PAUSE 500
WEND
// ======================================================================
//  One full game. Every step below checks AB: once the hand is removed,
//  all remaining moves, strikes and waits are skipped and GAME returns.
// ======================================================================

GAME:
    AB=0
    T0=MILLIS
    CUR=0
    L=1
    // knife raised, ready to move
    PWM1 CH1 50,10.7,0
    // intro phrase "Oh, let's rattle your spurs a little."
    PLAY_AUDIO("@AUDIO1",128)
    W=4000
    GOSUB WAITMS
    // ---- level 1: one slow run ----
    PRINT "LEVEL 1"
    B=B1
    A=A1
    PAUS=300
    GOSUB SWEEP
    // ---- level 2: two faster runs ----
    PRINT "LEVEL 2"
    B=B2
    A=A2
    PAUS=100
    FOR Y=1 TO 2 STEP 1
        GOSUB SWEEP
    NEXT
    IF AB=0 THEN
        PLAY_AUDIO("@AUDIO2",128)
        W=6000
        GOSUB WAITMS
    ENDIF
    // ---- level 3: 50 strikes at random points ----
    // the same point never comes twice in a row: if the random point
    // equals the last one (L), a new one is drawn, so the knife always
    // moves to a different place before the next strike
    PRINT "LEVEL 3"
    B=B3
    A=A3
    PAUS=80
    K=0
    WHILE K<50 AND AB=0
        // random point 1..5 (the IF guards against RND returning exactly 5),
        // redrawn until it differs from the last point
        F=L
        WHILE F=L
            F=FLOOR(RND(5))+1
            IF F>5 THEN F=5
        WEND
        GOSUB GOTOPOINT
        GOSUB TOUCH
        K=K+1
    WEND
    // back home to point 1
    IF AB=0 THEN
        F=1
        GOSUB GOTOPOINT
    ENDIF
RETURN

// ---------- WIN ----------

WIN:
    PRINT "WIN"
    PLAY_AUDIO("@AUDIO3",128)
    PAUSE 8000
    PWM1 CH1 50,8,0
RETURN

// ---------- HAND REMOVED ----------
// the phrase starts at once; meanwhile the knife drives home at the
// calm level 1 speed. Then wait until 5 s have passed since the phrase
// started, so the whole phrase is heard.

LOSE:
    PRINT "COWARD"
    PWM1 CH1 50,10.7,0
    PLAY_AUDIO("@AUDIO4",128)
    TW=MILLIS
    B=B1
    A=A1
    // CUR steps back = exactly point 1, wherever the knife stopped
    IF CUR>0 THEN
        N=CUR
        GOSUB BACK
    ENDIF
    L=1
    WHILE MILLIS-TW<5000
        PAUSE 20
    WEND
    PWM1 CH1 50,8,0
RETURN

// ---------- RUN 1->2->3->4->5->4->3->2->1 WITH A STRIKE AT EACH POINT ----------
// first loop: forward to points 2..5, second loop: back to points 4..1

SWEEP:
    FOR K=2 TO 5 STEP 1
        IF AB=0 THEN
            F=K
            GOSUB GOTOPOINT
            GOSUB TOUCH
        ENDIF
    NEXT
    FOR K=1 TO 4 STEP 1
        IF AB=0 THEN
            F=5-K
            GOSUB GOTOPOINT
            GOSUB TOUCH
        ENDIF
    NEXT
RETURN

// ---------- TOUCH CHECK ----------
// T0 = last moment the hand was seen on the pad.
// AB = 1 once the hand has been off for GONE ms or longer.

CHK:
    IF IN2=1 THEN T0=MILLIS
    IF MILLIS-T0>=GONE THEN AB=1
RETURN

// ---------- WAIT W ms, CHECKING THE TOUCH ----------
// a plain PAUSE would not notice the hand leaving, so the wait is cut
// into 20 ms pieces with a touch check in between; ends early on abort

WAITMS:
    TW=MILLIS
    WHILE MILLIS-TW<W AND AB=0
        GOSUB CHK
        PAUSE 20
    WEND
RETURN

// ---------- STRIKE ----------
// knife down (8), wait, knife up (10.7), wait.
// No strike if the hand is already gone.

TOUCH:
    GOSUB CHK
    IF AB=1 THEN RETURN
    PWM1 CH1 50,8,0
    PAUSE 10
    W=PAUS
    GOSUB WAITMS
    PWM1 CH1 50,10.7,0
    W=PAUS
    GOSUB WAITMS
RETURN

// ---------- MOVE FORWARD N STEPS ----------

FWD:
    DIR OFF
    DR=1
    GOSUB MOVE
RETURN

// ---------- MOVE BACK N STEPS ----------

BACK:
    DIR ON
    DR=-1
    GOSUB MOVE
RETURN

// ---------- N STEPS WITH ACCELERATION AND BRAKING ----------
// Each step is one pulse on OUT2 followed by a delay of B - P loop
// iterations (BRAKE). P is the speed-up:
//   acceleration: P grows by 5 every step up to A (delay gets shorter)
//   braking:      once the steps left (N - I) are just enough to bring
//                 P back to 0 in steps of 5, i.e. (N - I) * 5 <= P,
//                 P shrinks by 5 every step (delay gets longer)
// So the ramp down mirrors the ramp up. On a short move the knife never
// reaches top speed and starts braking earlier.
//
// CUR is updated on every step, so the knife position is always known.
// If the hand is removed mid-move, N is cut to I + P/5: exactly enough
// steps left to brake from the current speed instead of stopping dead.

MOVE:
    P=0
    I=0
    WHILE I<N
        OUT2 ON
        GOSUB BRAKE
        OUT2 OFF
        I=I+1
        CUR=CUR+DR
        // the touch is checked every 4th step, not every step: a check
        // takes almost as long as the step delay itself and would slow
        // the motor down
        IF AB=0 AND I%4=0 THEN
            GOSUB CHK
            IF AB=1 AND I+P/5<N THEN N=I+P/5
        ENDIF
        // D = 1 -> braking phase, D = 0 -> accelerating / cruising
        D=0
        IF (N-I)*5<=P THEN D=1
        IF D=1 AND P>0 THEN P=P-5
        IF D=0 AND P<A THEN P=P+5
    WEND
RETURN

// ---------- GO TO POINT F IN ONE MOVE ----------
// convert point F to a position in steps (Q), compare with the current
// position CUR and drive the difference forward or back.
// Nothing moves if the knife is already there (Q = CUR).

GOTOPOINT:
    IF AB=1 THEN RETURN
    T=F
    GOSUB POSOF
    IF Q>CUR THEN
        N=Q-CUR
        GOSUB FWD
    ENDIF
    IF Q<CUR THEN
        N=CUR-Q
        GOSUB BACK
    ENDIF
    L=F
RETURN

// position of point T in steps from point 1 -> Q
// (sum of the distances between all points before it)

POSOF:
    Q=0
    IF T>=2 THEN Q=Q+S12
    IF T>=3 THEN Q=Q+S23
    IF T>=4 THEN Q=Q+S34
    IF T>=5 THEN Q=Q+S45
RETURN

// ---------- STEP DELAY ----------
// empty counting loop, B - P iterations: the smaller, the faster the
// motor turns. The dummy assignments only make each iteration take time.

BRAKE:
    FOR Z=1 TO B-P STEP 1
        Z=Z
        P=P
    NEXT
RETURN

Click Run Script
That’s it, the game is ready!

If this project feels too complex, let’s start with something simpler.

1
0 replies

Sign in to reply