Funduino Street

FunduinoLast updated on

Linienverfolger, Ultraschall und NeoPixel mit MakeCode

Der Fahrroboter Calle wird mit dem Calliope mini V3 gesteuert. Programmiert wird er grafisch in MakeCode – du steckst die Blöcke im Browser zusammen und überträgst das Programm per USB auf den Calliope.

Die Anleitung führt dich durch alle Funktionen des Roboters:

  • Motoren ansteuern und vorwärts, rückwärts und im Kreis fahren
  • Scheinwerfer und NeoPixel-LEDs schalten
  • Einer Linie folgen mit den beiden Liniensensoren
  • Hindernissen ausweichen mit dem Ultraschallsensor

Vorkenntnisse brauchst du keine. Jeder Abschnitt beginnt mit einem kurzen Erklärstück und endet mit einem fertigen Beispielprogramm zum Nachbauen.

Calle driving robot with Calliope mini V3 attached
Program with Calliope mini V3

Assembly: the drive wheels

Assembling Calle is quite easy: you only need to install the two drive wheels and the front caster. First, break off the side edges of the circuit board by hand - they are only needed for stability during manufacturing.

Attach the motors to the underside using the white brackets. Make sure the protruding part of the bracket and the motor shaft point outward.

Feed the cables from both motors through the opening in the circuit board to the top, then plug the JST connectors into the designated sockets. It does not matter which way the black and red wires are oriented, since the connectors only fit one way.

Motors mounted underneath with the white brackets
Install drive motors
JST connectors of the motors in the sockets on the top
Plug in the motor connector
1 / 2

Assembly: Idler wheel and components

The front caster acts like a support wheel and follows all the movements caused by the two drive motors.

Attach two hexagonal spacers between the caster and the circuit board using two short nylon screws. These raise the caster to a height that matches the drive wheels. Then attach the caster underneath the circuit board by inserting two more screws through the designated holes from the top and fastening them into the spacers.

Finally, plug the remaining components—the line-tracking sensors and the ultrasonic sensor—into the top of the circuit board.

Create a MakeCode project

Calle is programmed using the MakeCode IDE. If you’ve never worked with a Calliope before, you should familiarize yourself with it first.

In MakeCode, a new program is called a “project.” Click “New Project,” give it a name - in this example, “Calle” - and click “Create.” Since Calle is designed for the Calliope mini V3, select this microcontroller.

On the left, you’ll see the selected controller, with the programming blocks next to it. The large area in the middle already contains the “on start” and “forever” blocks - these are there by default.

MakeCode interface with the on start and forever blocks
The programming interface

Practical exercise: switch on the headlights

Task: Calle's two headlights should be switched on and off using buttons A and B. They are two LEDs, both connected to pin C12 on the Calliope.

Without programming, contact C12 has a status of “0” – no voltage is applied, and the LEDs do not light up. With the “write digital value of pin C12 to 1” block, you change the status to “1,” and the Calliope outputs 3.3 V at this contact.

Go to Advanced → Pins, select “write digital value of pin P0 to 0” in the Digital section, and set the pin to C12. Duplicate this block: set it to 1 for button A and to 0 for button B.

Then click “Download,” connect the cable to the Calliope and computer, click “Next,” and then “Pair.” Select the Calliope in the pop-up – you’re now connected.

If you can't connect, the problem is usually the browser: Safari, Firefox, and Internet Explorer don't support WebUSB. Chrome, Edge, and Opera do.

Program: Set C12 to 1 when button A is pressed, and to 0 when button B is pressed.
Headlights via buttons A and B
Popup with the connected Calliope
The Calliope is connected.
1 / 2

Line follower: read the sensor values

The line sensors output a voltage—or not—depending on whether they detect a light surface or a dark line.

To use the reading later, you need a place to store it: a variable. Go to Variables → Create a variable and create one named “LinienVerfolgerLinks”. Drag the “set LinienVerfolgerLinks to 0” block into the “forever” block.

Then click Advanced → Digital, drag “digital values from pin P0” into the “0” slot, and change the pin to C15.

To see what the sensor reports, add the “show text” block from Basics and replace the default text with the variable. After uploading, a “0” or “1” appears on the LED matrix.

At one point, the article says that signal “1” corresponds to a light-colored surface, and a chapter later, that the sensor outputs “1” when it sees a line. Both can’t be true at the same time—check what it means with your own setup.

Program block sets digital values from pin to C15
Query the sensor at contact C15
Program: Display sensor value as text on the LED matrix
Display the sensor value on the matrix
1 / 2

Line follower: controlling the motors

The principle of line following is simple: If Calle veers to the left of the line, the left motor should turn faster, making the robot turn right. If it veers to the right, the right motor turns faster.

With two sensors, there are four cases: both light, both dark, left dark and right light, left light and right dark. When both sensors detect “dark,” the robot is centered on the line and can go full speed.

To program it, create two variables, “LineFollowerLeft” and “LineFollowerRight,” and assign them the digital values from contacts C14 and C15. From the Logic category, select “if true then otherwise” and expand the block twice using the plus symbol. Check the four cases using two comparisons each, joined with “and.”

Motor control is already built into MakeCode: The right motor is M0, and the left is M1.

If the program runs but the sensors don't respond, they're probably not calibrated correctly. There's a blue box on the sensors that you can adjust with a screwdriver. To do this, place Calle over the line and adjust it so that the sensor operates right at the threshold between on and off.

Nested if-then conditions for both line sensors
the conditional structure
Finished line-following program with motor control
The finished line-following program
1 / 2

Measuring distance with the ultrasonic sensor

There is a module with two cylinders on the front of the Calliope that looks like two eyes. It is an ultrasonic sensor.

It works like a bat: It emits ultrasonic waves that are reflected by objects and return like an echo. The Calliope calculates the distance based on how long the wave takes to travel.

External modules aren’t included in MakeCode by default. Click “Extensions” and enter the direct link to the library in the search bar: github.com/Funduino-GmbH/RCW0001

After clicking the “RCW-0001” card, a new category appears with a single block: “Ultrasonic sensor (in cm) trigger on C8 and echo on C9”.

To display the distance, drag “show number 0” from the Basics category into the “forever” block and replace the “0” with the ultrasonic block.

Ultrasonic sensor block with trigger C8 and echo C9
The ultrasonic block
Program: Output measured distance using show number
Display distance on the matrix
1 / 2

Output measured values via serial communication

The LED matrix display has one drawback: It takes quite a while for the number to scroll across, and the program only continues once it’s finished. If data needs to be processed quickly, “show number” and “show text” aren’t suitable.

Instead, use serial communication. Under Advanced → Serial, place the “redirect serial to USB” block inside “on start” and “serial write line” inside “forever”. After downloading, a new option appears on the left side of the webpage, where you can view the measured values.

To reuse the values, create a variable called “Distance”, put the ultrasonic block in “set Distance to 0”, and add the variable to the serial output. This lets you read the data while other program blocks run in parallel.

Program: Store distance in a variable and output it serially
Output distance serially

Program dependencies

Now you play a tone depending on the measured distance. In computer science, this kind of dependency is called an “if statement” or an “if-then function.”

From the Logic category, take “if true then,” from the Sound category, the “play note … for …” block, and also the “Greater than” comparison from Logic. Put these blocks together.

To expand it, click the plus sign in the dark green block: Calle should drive forward when the distance is greater than 30 and stop when it is less. From the Motors category, take the “Motor” block and change it from M0 to M0 & M1. Below 30, set the value to 0; otherwise, set it to 50 percent so Calle doesn’t drive too fast.

Complete program: drive when farther than 30 cm, otherwise stop
The complete program

Control NeoPixels

Calle has six special light sources: intelligent WS2812 LEDs, often called NeoPixels. They can shine in thousands of colors, and several hundred of them share a single data line.

Calle uses two pins: P2 controls the three LEDs on the right side, and P3 controls the three on the left.

Under “Extensions,” search for “NeoPixel” and install the library. Then, in the new category, click “Set strip to NeoPixels on pin P0 …” and set the pin to P2. For the left side, create a second variable - we’ll call it “strip2” - and set it to P3.

Use the “show color” block to set the color. To stop the colors from changing too quickly, add “pause (ms) 1000” from the Basics category.

You can control the LEDs individually using NeoPixel → more with the “strip set color of NeoPixel 0 to red” block. On both sides, the numbering runs from 0 at the very front to 2 at the very back.

Two NeoPixel strips, strip on P2 and strip2 on P3
Create both LED sides
Program: Set individual NeoPixels to blue, green, and red
Control the LEDs individually
Numbering of the NeoPixels: 0 at the front to 2 at the back, top view
The pixel numbering
1 / 3
Rate the instructions

Comments

Ask a question or share an experience

Contact us if something doesn’t work. Please include your board and the error message.

We review every comment before publication. Your email address remains with us.