2017-03-27 06:22:40 +02:00
|
|
|
# Code
|
2018-08-23 20:47:36 +02:00
|
|
|
|
2017-09-07 22:42:08 +02:00
|
|
|
## @description code to make the Milk Carton Robot alive
|
2017-03-27 06:22:40 +02:00
|
|
|
|
2017-09-07 22:42:08 +02:00
|
|
|
## ~avatar avatar
|
2017-03-27 06:22:40 +02:00
|
|
|
|
2017-03-27 20:19:08 +02:00
|
|
|
Add code to make the Milk Carton Robot move.
|
2017-03-27 06:22:40 +02:00
|
|
|
|
2017-09-07 22:42:08 +02:00
|
|
|
## ~
|
2017-03-27 06:22:40 +02:00
|
|
|
|
|
|
|
## Duration: ~15 minutes
|
|
|
|
|
|
|
|
## Step 1: wire up the servo
|
|
|
|
|
|
|
|
Follow the instructions in [servo 101 manual](/device/servo) to connect the servo to the @boardname@.
|
|
|
|
|
|
|
|
https://youtu.be/m-HS8OyS0pw
|
|
|
|
|
|
|
|
## Step 2: code light sensor
|
|
|
|
|
|
|
|
Code the lightsensor on the @boardname@ to control the servo.
|
|
|
|
|
|
|
|
```blocks
|
|
|
|
basic.forever(() => {
|
|
|
|
led.plotBarGraph(
|
|
|
|
input.lightLevel(),
|
|
|
|
0
|
|
|
|
)
|
|
|
|
pins.servoWritePin(AnalogPin.P0, input.lightLevel())
|
|
|
|
})
|
|
|
|
```
|
|
|
|
|
|
|
|
https://youtu.be/Ah4fEbJtklU
|
|
|
|
|
|
|
|
It works but your servo might be trying to move too much. Let's calibrate it.
|
|
|
|
|
|
|
|
## Step 3: calibrate the servo
|
|
|
|
|
2019-09-11 19:54:24 +02:00
|
|
|
Download the [servo calibration program](/projects/servo-calibrator) to determine
|
|
|
|
the opening and closing angles of the servo for your robot.
|
2017-03-27 06:22:40 +02:00
|
|
|
|
|
|
|
https://youtu.be/lZxWC82HDn0
|
|
|
|
|
|
|
|
We are going to map the light level range, ``[0, 255]`` to the
|
|
|
|
angle range, ``[closed, opened]`` using ``pins.map``.
|
|
|
|
|
|
|
|
```blocks
|
|
|
|
let angle = 0
|
|
|
|
let closed = 0
|
|
|
|
let opened = 0
|
|
|
|
basic.forever(() => {
|
|
|
|
led.plotBarGraph(
|
|
|
|
input.lightLevel(),
|
|
|
|
0
|
|
|
|
)
|
|
|
|
angle = pins.map(
|
|
|
|
input.lightLevel(),
|
|
|
|
0,
|
|
|
|
255,
|
|
|
|
opened,
|
|
|
|
closed
|
|
|
|
)
|
|
|
|
pins.servoWritePin(AnalogPin.P0, angle)
|
|
|
|
})
|
|
|
|
// TODO: use the angle found at calibration!
|
|
|
|
opened = 95
|
|
|
|
// TODO: use the angle found at calibration!
|
|
|
|
closed = 175
|
|
|
|
```
|
|
|
|
|
|
|
|
|
2017-09-07 22:42:08 +02:00
|
|
|
## ~button /projects/milk-carton-robot/connect
|
2017-03-27 06:22:40 +02:00
|
|
|
NEXT: Connect
|
2019-09-11 19:54:24 +02:00
|
|
|
## ~
|