Let's Build a Japanese Crane Game With LEGO SPIKE Prime and C!

by AfrelEdTech in Circuits > Robots

51 Views, 1 Favorites, 0 Comments

Let's Build a Japanese Crane Game With LEGO SPIKE Prime and C!

サムネイル.png

Crane games are super fun and popular in Japan—you’ll find them in arcades everywhere!

This time, we decided to recreate a mini version of the Japanese crane game using LEGO SPIKE Prime and C programming.

Can you grab the candy perfectly? Let’s try!

Supplies

  1. 1 PC with Windows 10 or 11 operating system.
  2. SPIKE Prime

Robot Setup

Here’s what we used to build our crane game robot:

  1. Crane movement: 3 motors control forward, backward, left/right, and the gripper
  2. Control buttons: Force sensor (to move the crane)
  3. Coin system (optional): Color sensor to detect inserted “coins”
  4. Prize detection (optional): Distance sensor to check if you grabbed the candy!

Features

ClawMachine

Crane Arm

The crane moves in all directions—left, right, forward, and backward—and it grabs things with a claw.

We used three motors to control all the parts. The gripper opens and closes to catch the candy.


Control Button

Want to move the crane? Just press the force sensor!

  1. On the first press-and-hold, the crane moves sideways (left or right).
  2. On the second press-and-hold, the crane moves backward.
  3. After both movements, the gripper automatically lowers and tries to grab the candy.

The crane only moves while you’re holding the button down—just like a real crane game in an arcade!


pup_motor_t *motorB; // Variable for using motor B
pup_motor_t *motorD; // Variable for using motor C
pup_motor_t *motorE; // Variable for using motor E
pup_device_t *ForceSensor; // Variable for using force sensor

void Main(intptr_t exinf)
{
// // Wait 3 seconds
// dly_tsk(3000000);

motorB = pup_motor_init(PBIO_PORT_ID_B, PUP_DIRECTION_CLOCKWISE); // Motor for the gripper
motorD = pup_motor_init(PBIO_PORT_ID_D, PUP_DIRECTION_CLOCKWISE); // Motor for forward/backward movement
motorE = pup_motor_init(PBIO_PORT_ID_E, PUP_DIRECTION_CLOCKWISE); // Motor for left/right movement
ForceSensor = pup_force_sensor_get_device(PBIO_PORT_ID_C);

// Move the arm to the right while the force sensor is pressed
while(!pup_force_sensor_touched(ForceSensor));
while(pup_force_sensor_touched(ForceSensor)){
pup_motor_set_speed(motorE, 100);
}
pup_motor_stop(motorE);

// Move the arm backward while the force sensor is pressed
while(!pup_force_sensor_touched(ForceSensor));
while(pup_force_sensor_touched(ForceSensor)){
pup_motor_set_speed(motorD, 100);
}
pup_motor_stop(motorD);
dly_tsk(1000000);

// Grab the prize
pup_motor_set_speed(motorB, 200);
while (pup_motor_get_count(motorB) < 100);
pup_motor_stop(motorB);
dly_tsk(1000000);

// Return the arm to its original position
pup_motor_set_speed(motorD, -100);
while (pup_motor_get_count(motorD) > 10);
pup_motor_stop(motorD);

pup_motor_set_speed(motorE, -100);
while (pup_motor_get_count(motorE) > 10);
pup_motor_stop(motorE);

dly_tsk(1000000);

// Release the prize
pup_motor_set_speed(motorB, -300);
while (pup_motor_get_count(motorB) > 10);
pup_motor_stop(motorB);
// End the program
exit(0);
}

Add More Fun!

Want to make your crane game even more awesome? Try these upgrades:

Coin Detection (Color Sensor)

Use a color sensor to detect a coin-shaped block.

Only play if a “coin” is inserted! You can even program different coin colors to give different playtimes.

Candy Detection (Distance Sensor)

Add a distance sensor to detect if the candy falls into the prize chute.

If it does, you win! The robot can even play music to celebrate your success!

Let’s Play!

We had a blast building this Japanese-style crane game using LEGO SPIKE Prime and C.

It’s a fun project for kids, students, or anyone who loves arcade games and robotics.

Ready to try it yourself? What kind of prizes would you put inside?

【Free Trial】Start C Language Programming With Afrel's Educational Materials

Are you intrigued by the idea of programming your SPIKE™ Prime with C? Did you know that Afrel is a key information provider for SPIKE™ Prime?


To meet the demand of those who want to "learn more about SPIKE™ Prime " and "try programming in C," Afrel has developed and sells educational materials for learning C language programming with SPIKE™ Prime.


Stepping up from Python to C is an excellent opportunity to further enhance your programming skills. Learning C will give you a deeper understanding of how computers work and enable more advanced control.


Afrel's C language materials are designed for beginners, ensuring a smooth learning experience. Why not take this chance to discover the joy of controlling SPIKE™ Prime with C and expand your programming horizons?


If you're ready to tackle C language programming, click the link below for more details.


Learn more about SPIKE™ Prime C Language Programming Materials here