fwd-edu-breakout=github:climate-action-kits/pxt-fwd-edu/fwd-breakout
sonar=github:climate-action-kits/pxt-fwd-edu
datalogger=datalogger
fwdSensors.touch.fwdOnTouch(jacdac.ButtonEvent.Down, function () {
fwdMotors.middleServo.fwdSetEnabled(false)
})
fwdSensors.dial1.fwdOnDialTurned(fwdSensors.DialDirection.CCW, function (difference) {
fwdMotors.middleServo.fwdSetSpeed(50)
})
fwdSensors.dial1.fwdOnDialTurned(fwdSensors.DialDirection.CW, function (difference) {
fwdMotors.middleServo.fwdSetSpeed(-50)
})
input.onButtonPressed(Button.A, function () {
basic.showNumber(batteryLevel)
})
batteryLevel = 0
basic.forever(function () {
if (fwdMotors.middleServo.fwdIsEnabled() == true) {
basic.showLeds(`
. . # . .
. # . # .
# # # # #
. # . # .
. # # # .
`)
batteryLevel += 1
if (batteryLevel > 100) {
batteryLevel = 100
}
basic.pause(2000)
} else {
basic.showLeds(`
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
`)
}
})
Let's build our wind turbine! We will do this in four parts:
- Build your wind turbine
- Add code to bring it to life
- Modify the code to learn how it works
- Complete a challenge to improve the wind turbine
It’s time to bring your wind turbine 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.
Next, 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 wind turbine!
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.
Take a moment to review the starter code. Look for the following:
- How the turbine spins: Find the
||fwdSensors:on dial turned||
and||fwdSensors:on touch down||
events that control the motor. - How the house lights up: Find the conditional statement (if/then) that turns on the house lights if the turbine is spinning (aka generating electricity!)
fwdSensors.touch.fwdOnTouch(jacdac.ButtonEvent.Down, function () {
fwdMotors.middleServo.fwdSetEnabled(false)
})
fwdSensors.dial1.fwdOnDialTurned(fwdSensors.DialDirection.CCW, function (difference) {
fwdMotors.middleServo.fwdSetSpeed(50)
})
fwdSensors.dial1.fwdOnDialTurned(fwdSensors.DialDirection.CW, function (difference) {
fwdMotors.middleServo.fwdSetSpeed(-50)
})
basic.forever(function () {
// @highlight
if (fwdMotors.middleServo.fwdIsEnabled() == true) {
basic.showLeds(`
. . # . .
. # . # .
# # # # #
. # . # .
. # # # .
`)
} else {
basic.showLeds(`
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
`)
}
})
Our wind turbine also has a battery to store energy for later use.
At the start of the program, the ||variables:batteryLevel||
variable is set to '0' or 0%. As the wind turbine spins, it slowly charges the battery and the number in the variable increases by 1% every 2 seconds. When the battery is full, it stops at 100%.
input.onButtonPressed(Button.A, function () {
// @highlight
basic.showNumber(batteryLevel)
})
// @highlight
batteryLevel = 0
basic.forever(function () {
if (fwdMotors.middleServo.fwdIsEnabled() == true) {
// @highlight
batteryLevel += 1
// @highlight
if (batteryLevel > 100) {
batteryLevel = 100
}
// @highlight
basic.pause(2000)
}
})
At the moment, there is no obvious sign to tell us when the battery has reached full charged. Let's add a ||music:play tone||
block to the code to make a note play when the battery reaches 100%.
Where should we put this block in our existing code?
~hint Tell me more!
- Add the block inside the conditional statement that prevents the
||variables:batteryLevel||
from going over 100%. - As soon as the
||variables:batteryLevel||
is set to 100%, the note will play. hint~
if (batteryLevel > 100) {
batteryLevel = 100
// @highlight
music.play(music.tonePlayable(262, music.beat(BeatFraction.Whole)), music.PlaybackMode.UntilDone)
}
Right now, the battery charges by 1% every 2 seconds. How could we make charging happen more quickly?
~hint Tell me more!
- You could increase the value in the
||variables:change batteryLevel by||
block or decrease the value in the||pause||
block. - Try one of these options now! hint~
if (fwdMotors.middleServo.fwdIsEnabled() == true) {
basic.showLeds(`
. . # . .
. # . # .
# # # # #
. # . # .
. # # # .
`)
batteryLevel += 1
if (batteryLevel > 100) {
batteryLevel = 100
music.play(music.tonePlayable(262, music.beat(BeatFraction.Whole)), music.PlaybackMode.UntilDone)
}
// @highlight
basic.pause(500)
} else {
basic.showLeds(`
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
`)
}
Right now, the house only lights up when the wind turbine is spinning. As we've learned, we can also power the house with our battery, as long as there is charge!
How might we write another conditional statement to tell the micro:bit to do this?
~hint Tell me more!
- Remember: Real wind turbines store energy in batteries to keep lights and machines running even when there’s no wind. hint~
We can expand our existing conditional statement by pressing the '+' icon at the bottom. This will add an else if statement which will only run when the wind turbine is off!
if (fwdMotors.middleServo.fwdIsEnabled() == true) {
basic.showLeds(`
. . # . .
. # . # .
# # # # #
. # . # .
. # # # .
`)
batteryLevel += 1
if (batteryLevel > 100) {
batteryLevel = 100
music.play(music.tonePlayable(262, music.beat(BeatFraction.Whole)), music.PlaybackMode.UntilDone)
}
basic.pause(500)
} else if (false) {
} else {
basic.showLeds(`
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
`)
}
This new conditional statement should check if the battery level is greater than 0.
Drag a ||variables:batteryLevel||
block and a ||logic:>||
block into the workspace. How should you combine these with the new conditional statement?
~hint Tell me more!
- Combine these blocks to create the following expression inside the conditional statement: batteryLevel > 0 hint~
if (fwdMotors.middleServo.fwdIsEnabled() == true) {
basic.showLeds(`
. . # . .
. # . # .
# # # # #
. # . # .
. # # # .
`)
batteryLevel += 1
if (batteryLevel > 100) {
batteryLevel = 100
music.play(music.tonePlayable(262, music.beat(BeatFraction.Whole)), music.PlaybackMode.UntilDone)
}
basic.pause(500)
} else if (batteryLevel > 0) {
} else {
basic.showLeds(`
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
`)
}
Right click the ||basic:show LEDs||
block and duplicate it. Add the new block inside the new conditional.
if (fwdMotors.middleServo.fwdIsEnabled() == true) {
basic.showLeds(`
. . # . .
. # . # .
# # # # #
. # . # .
. # # # .
`)
batteryLevel += 1
if (batteryLevel > 100) {
batteryLevel = 100
music.play(music.tonePlayable(262, music.beat(BeatFraction.Whole)), music.PlaybackMode.UntilDone)
}
basic.pause(500)
} else if (batteryLevel > 0) {
// @highlight
basic.showLeds(`
. . # . .
. # . # .
# # # # #
. # . # .
. # # # .
`)
} else {
basic.showLeds(`
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
`)
}
To make things realistic, the battery should drain as it's being used to power the house.
We've used the ||variables:change batteryLevel by||
block to increase the battery charge over time. How might we use this block to decrease the charge over time?
~hint Tell me more!
- Add a
||variables:change batteryLevel by||
block into the new conditional statement. Change the value to '-1' to decrease the charge. - Add a
||basic:pause||
block to control how quickly the battery drains. hint~
if (fwdMotors.middleServo.fwdIsEnabled() == true) {
basic.showLeds(`
. . # . .
. # . # .
# # # # #
. # . # .
. # # # .
`)
batteryLevel += 1
if (batteryLevel > 100) {
batteryLevel = 100
music.play(music.tonePlayable(262, music.beat(BeatFraction.Whole)), music.PlaybackMode.UntilDone)
}
basic.pause(2000)
} else if (batteryLevel > 0) {
basic.showLeds(`
. . # . .
. # . # .
# # # # #
. # . # .
. # # # .
`)
// @highlight
batteryLevel += -1
// @highlight
basic.pause(2000)
} else {
basic.showLeds(`
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
`)
}
Let's test out this new conditional statement.
Download your new code. Then, turn the dial to spin the turbine and charge the battery. Then stop spinning the turbine. What happens when the battery has stored energy?
~hint Tell me more!
- As long as the battery has a charge, it will be able to continue lighting up the house even when the wind turbine stops spinning.
- Once the battery hits 0%, the lights turn off.
- Remember, you can press the A button at any time to see the battery level! hint~
Let’s make your wind turbine even better. Can you:
- add a sound alert when the house is at risk of having no power at all?
- adjust how quickly the battery drains?
To add an alert when the battery reaches 0%, add a ||music:play tone||
block in the final else condition.
Change the note so it is different than the note that is played when the battery is full!
if (fwdMotors.middleServo.fwdIsEnabled() == true) {
basic.showLeds(`
. . # . .
. # . # .
# # # # #
. # . # .
. # # # .
`)
batteryLevel += 1
if (batteryLevel > 100) {
batteryLevel = 100
music.play(music.tonePlayable(262, music.beat(BeatFraction.Whole)), music.PlaybackMode.UntilDone)
}
basic.pause(2000)
} else if (batteryLevel > 0) {
basic.showLeds(`
. . # . .
. # . # .
# # # # #
. # . # .
. # # # .
`)
batteryLevel += -1
basic.pause(2000)
} else {
basic.showLeds(`
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
`)
// @highlight
music.play(music.tonePlayable(131, music.beat(BeatFraction.Whole)), music.PlaybackMode.UntilDone)
}
Finally, to adjust the rate the battery drains, change the numbers inside the ||variables:change batteryLevel by||
and ||basic:pause||
blocks.
Try adjusting these numbers so the battery charges twice as fast as it drains.
if (fwdMotors.middleServo.fwdIsEnabled() == true) {
basic.showLeds(`
. . # . .
. # . # .
# # # # #
. # . # .
. # # # .
`)
batteryLevel += 1
if (batteryLevel > 100) {
batteryLevel = 100
music.play(music.tonePlayable(262, music.beat(BeatFraction.Whole)), music.PlaybackMode.UntilDone)
}
basic.pause(500)
} else if (batteryLevel > 0) {
basic.showLeds(`
. . # . .
. # . # .
# # # # #
. # . # .
. # # # .
`)
// @highlight
batteryLevel += -1
// @highlight
basic.pause(1000)
} else {
basic.showLeds(`
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
`)
music.play(music.tonePlayable(131, music.beat(BeatFraction.Whole)), music.PlaybackMode.UntilDone)
}
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 additional thing you could do to improve your farm?
In the next step, you can click the |Done|
button to finish the tutorial.