2017-03-20 23:17:24 +01:00
# Code
2017-09-07 22:42:08 +02:00
## @description code to make the Milky Monster alive
2017-03-20 23:17:24 +01:00
2017-09-07 22:42:08 +02:00
## ~avatar avatar
2017-03-20 23:17:24 +01:00
Add code to make the Milky Monster move.
2017-09-07 22:42:08 +02:00
## ~
2017-03-20 23:17:24 +01:00
## Duration: ~30 minutes
2017-11-20 22:41:35 +01:00
## Step 1: Calibrate servo
2017-03-20 23:17:24 +01:00
In order for the Milky Monster to move, the @boardname @ needs to command the servo to go between ``0`` and ``180`` degrees at a certain pace. In the code below:
2017-11-20 22:41:35 +01:00
- Press button ``A`` to switch the servo to 180 degrees (to close the mouth of Milky Monster).
- Press button ``B`` to switch the servo to 0 degrees (to open the mouth of Milky Monster).
2017-03-20 23:17:24 +01:00
```blocks
input.onButtonPressed(Button.A, () => {
pins.servoWritePin(AnalogPin.P0, 180)
basic.showNumber(180)
})
input.onButtonPressed(Button.B, () => {
pins.servoWritePin(AnalogPin.P0, 0)
basic.showNumber(0)
})
basic.showString("calibrate")
```
2017-11-20 22:41:35 +01:00
## Step 2: Attach rotor
2017-03-20 23:17:24 +01:00
2017-11-20 22:41:35 +01:00
The servo should be positioned at 180 degrees **before** attaching the rotor to it. This will make sure the mouth of the Milky Monster is closed once the servo reaches 180 degrees.
2017-03-20 23:17:24 +01:00
2017-09-07 22:42:08 +02:00
## ~ hint
2017-03-20 23:17:24 +01:00
2017-11-20 22:41:35 +01:00
You can use a philips screw driver to attach the rotor to the servo.
2017-03-20 23:17:24 +01:00
2017-09-07 22:42:08 +02:00
## ~
2017-03-20 23:17:24 +01:00
https://youtu.be/YZfkMWTeH4o
2017-11-20 22:41:35 +01:00
## Step 3: Check calibration
2017-03-20 23:17:24 +01:00
2017-11-20 22:41:35 +01:00
When the user presses ``A`` the servo rotor should be in 'up' position.
2017-03-20 23:17:24 +01:00
https://youtu.be/bAqXEawUsSM
2017-11-20 22:41:35 +01:00
## Step 4: Connect cord to rotor
2017-03-20 23:17:24 +01:00
Connect the cord to the servo rotor while mouth of Milky Monster is **closed** .
https://youtu.be/AWsnwk_iA_A
2017-11-20 22:41:35 +01:00
## Step 5: Connect cables to @boardname@
2017-03-20 23:17:24 +01:00
Connect cables from @boardname @ to the servo and place the battery.
https://youtu.be/fAR58GJUZdM
2017-11-20 22:41:35 +01:00
## Step 6: Code light sensor
2017-03-20 23:17:24 +01:00
2017-11-20 22:41:35 +01:00
Code the light sensor on the @boardname @ to control the servo.
2017-03-20 23:17:24 +01:00
```blocks
basic.forever(() => {
pins.servoWritePin(AnalogPin.P0, input.lightLevel())
led.plotBarGraph(
input.lightLevel(),
0
)
})
```
## Step 6: Ready!
Your Milky Monster is ready!
https://youtu.be/egl3fNAYylk
2017-09-07 22:42:08 +02:00
## ~button /projects/milky-monster/connect
2017-03-20 23:17:24 +01:00
NEXT: Connect
2017-09-07 22:42:08 +02:00
## ~