fwd-edu-breakout=github:climate-action-kits/pxt-fwd-edu/fwd-breakout
sonar=github:climate-action-kits/pxt-fwd-edu
datalogger=datalogger
basic.forever(function () {
while (fwdSensors.solar1.fwdIsLightLevelPastThreshold(95, fwdSensors.ThresholdDirection.Over)) {
fwdMotors.leftServo.fwdSetEnabled(true)
basic.pause(2000)
fwdMotors.leftServo.fwdSetEnabled(false)
basic.pause(5000)
}
})
Let's build a compost tumbler powered by the sun! We will do this in four parts:
- Build your tumbler
- Add code to bring it to life
- Modify your tumbler to learn how it works
- Complete a challenge to improve it
We need to connect our project to the computer to make it come to life with code!
The code will be the instructions that tell our micro:bit what to do.
IMPORTANT! Make sure your Climate Action Kit Breakout Board is turned on and your micro:bit is plugged into your computer.
Click the three dots beside the |Download|
button, then click on Connect Device. Follow the steps to pair your micro:bit.
Next, click the |Download|
button to download the code to your project.
We are now ready to modify our compost tumbler!
Tutorial Tips
- Follow the steps at the top of the screen.
- When you are ready for more details, click 'Tell me more!'
- If you need help with the code, click the lightbulb!
- After each change,
|Download|
the new code to your micro:bit.
Let's test out our compost tumbler! Shine a light on the solar sensor for 10-30 seconds. This light represents sunlight.
Write down what you see happening with as much detail as possible. Do you notice any patterns?
~hint Tell me more! While the light is on the sensor, the motor:
- Turns on
- Spins for 2 seconds
- Turns off
- Stays still for 5 seconds
This cycle repeats again and again. hint~
What happens when you take the light away from the solar sensor?
~hint Tell me more!
- The tumbler stops spinning. hint~
Which code block do you think makes the tumbler spin when light is present?
~hint Tell me more!
- The
||loops:while||
loop controls the motor. - When the solar sensor detects bright sunlight (more than 95%), then the motor is turned on. hint~
// @highlight
while (fwdSensors.solar1.fwdIsLightLevelPastThreshold(95, fwdSensors.ThresholdDirection.Over)) {
// @highlight
fwdMotors.leftServo.fwdSetEnabled(true)
basic.pause(2000)
fwdMotors.leftServo.fwdSetEnabled(false)
basic.pause(5000)
}
What do you think will happen if you change the threshold (the sunlight level) for the solar sensor?
Change the number in the ||fwdSensors:solar1 light level over||
block to a lower value. What do you notice?
~hint Tell me more!
- The threshold controls when the tumbler starts spinning.
- If you lower the threshold, the tumbler will spin even when there is a lower level of sunlight. hint~
// @highlight
while (fwdSensors.solar1.fwdIsLightLevelPastThreshold(50, fwdSensors.ThresholdDirection.Over)) {
}
Change the threshold back to 95%.
// @highlight
while (fwdSensors.solar1.fwdIsLightLevelPastThreshold(95, fwdSensors.ThresholdDirection.Over)) {
}
Which specific blocks of code do you think make the tumbler spin and stop?
~hint Tell me more!
- The
||fwdMotors:set leftServo ON||
block turns the motor on and makes the tumbler spin. - The
||fwdMotors:set leftServo OFF||
block turns the motor off and stops the tumbler. hint~
while (fwdSensors.solar1.fwdIsLightLevelPastThreshold(95, fwdSensors.ThresholdDirection.Over)) {
// @highlight
fwdMotors.leftServo.fwdSetEnabled(true)
basic.pause(2000)
// @highlight
fwdMotors.leftServo.fwdSetEnabled(false)
basic.pause(5000)
}
Why do you think there are two ||basic:pause||
blocks?
Try removing them and see what happens!
~hint Tell me more!
- The pauses control how long the motor spins and how long it waits before the next spin.
- Without the pauses, the motor would turn on and off so quickly that we wouldn't even see it happen! hint~
basic.forever(function () {
while (fwdSensors.solar1.fwdIsLightLevelPastThreshold(95, fwdSensors.ThresholdDirection.Over)) {
fwdMotors.leftServo.fwdSetEnabled(true)
fwdMotors.leftServo.fwdSetEnabled(false)
}
})
Add the ||basic:pause||
blocks back into the code.
while (fwdSensors.solar1.fwdIsLightLevelPastThreshold(95, fwdSensors.ThresholdDirection.Over)) {
fwdMotors.leftServo.fwdSetEnabled(true)
// @highlight
basic.pause(2000)
fwdMotors.leftServo.fwdSetEnabled(false)
// @highlight
basic.pause(5000)
}
Think back to what we learned in the lesson. The tumbler needs to spin enough to mix the compost but not too often, or it wastes energy. How long should the tumbler spin? How long should it pause?
Do some rapid research and adjust the numbers in the ||basic:pause||
blocks based on what you learn.
Let’s add some visual output to show what part of the composting cycle we are on.
Drag a ||basic:show LEDs||
block and a ||basic:clear screen||
block into the workspace. Where should we add these in our existing code to show an animation while the tumbler is spinning?
~hint Tell me more!
- Add the
||basic:show LEDs||
block right after the motor turns on to show that the tumbler is spinning. - Add the
||basic:clear screen||
block after the motor turns off to show that the tumbler has stopped. hint~
while (fwdSensors.solar1.fwdIsLightLevelPastThreshold(95, fwdSensors.ThresholdDirection.Over)) {
fwdMotors.leftServo.fwdSetEnabled(true)
// @highlight
basic.showLeds(`
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
`)
basic.pause(2000)
fwdMotors.leftServo.fwdSetEnabled(false)
// @highlight
basic.clearScreen()
basic.pause(5000)
}
What kind of symbol should we use for the animation?
You could make a spinning wheel, a happy face, or something else. Get creative and draw your own!
while (fwdSensors.solar1.fwdIsLightLevelPastThreshold(95, fwdSensors.ThresholdDirection.Over)) {
fwdMotors.leftServo.fwdSetEnabled(true)
// @highlight
basic.showLeds(`
. # # # .
# . . . #
# . . . #
# . . . #
. # # # .
`)
basic.pause(2000)
fwdMotors.leftServo.fwdSetEnabled(false)
basic.clearScreen()
basic.pause(5000)
}
Instead of clearing the animation, what symbol could represent the second stage of composting (when the tumbler is resting)?
Try for yourself and then check the lightbulb for our solution!
while (fwdSensors.solar1.fwdIsLightLevelPastThreshold(95, fwdSensors.ThresholdDirection.Over)) {
fwdMotors.leftServo.fwdSetEnabled(true)
basic.showLeds(`
. # # # .
# . . . #
# . . . #
# . . . #
. # # # .
`)
basic.pause(2000)
fwdMotors.leftServo.fwdSetEnabled(false)
// @highlight
basic.showLeds(`
. . . . .
. . . . .
# # # # #
. . . . .
. . . . .
`)
basic.pause(5000)
}
You've completed the activity!
Think about something in this project that was tricky.
How did you figure it out? How did that make you feel?
What is one thing you could do to improve your compost tumbler?
In the next step, you can click the |Done|
button to finish the tutorial.