fwd-edu-breakout=github:climate-action-kits/pxt-fwd-edu/fwd-breakout
sonar=github:climate-action-kits/pxt-fwd-edu
datalogger=datalogger
input.onButtonPressed(Button.A, function () {
for (let index = 0; index < 4; index++) {
fwdMotors.drive(fwdMotors.DrivingDirection.Forward, 50)
basic.pause(1000)
fwdMotors.stop()
fwdMotors.middleServo.fwdSetAngle(0)
basic.pause(250)
fwdMotors.middleServo.fwdSetAngle(45)
basic.pause(250)
}
fwdMotors.stop()
})
fwdMotors.middleServo.fwdSetAngle(45)
fwdMotors.setupDriving(
fwdMotors.leftServo,
fwdMotors.rightServo,
10
)
Let's build an automated tree seeder. We are going to do this in four parts:
- Build the automated tree seeder
- Add code to make it move
- Use the tree seeder to learn how it works
- Become a company owner and complete a math challenge
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.
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 use our tree seeder!
Tips
- Follow the instructions at the top of the screen.
- When you are ready for more information, click 'Tell me more!'
- If you need help with the code, click the lightbulb!
Think back to the lesson or watch this video.
What should an automated tree seeder be able to do?
~hint Tell me more! The automated tree seeder should be able to:
- Continuously move
- Plant seeds evenly across a field
Different products may have more features, but these two are the minimum requirement. hint~
Take a look at the physical project you just built.
What robotic components do you notice? How do you think they’ll work together to make the tree seeder function as expected?
~hint Tell me more! The automated tree seeder has:
- Two continuous servo motors that rotate a full 360 degrees. They will spin the wheels to allow the seeder to drive forward.
- One positional servo motor that moves at a specified angle. Each time it moves, it will plant a seed.
- These parts are all connected to the breakout board through connector cables.
- Finally, the breakout board is connected to the micro:bit which holds all the code that will tell our motors what to do and when to do it! hint~
Let’s test it out. Unplug your project from the computer. Then, place it on the ground in an open area.
Press 'A'. What do you notice? Do this a few times, if necessary, then document your findings.
What did you notice? Were there any patterns?
~hint Tell me more! Hopefully you noticed that there was indeed a pattern. The tree seeder completed the following steps 4 times in a row:
- Drive forward
- Stop
- Move arm towards ground
- Stop
- Move arm away from ground hint~
Take a look at the code in the workspace.
Given what you just learned and observed, can you guess which block(s) are making the tree seeder move or drive?
~hint Tell me more!
- We set up the driving function using the
||fwdMotors:setup driving||
block. - We make the tree seeder move forward at a certain speed using the
||fwdMotors:drive forward||
block. hint~
fwdMotors.middleServo.fwdSetAngle(45)
// @highlight
fwdMotors.setupDriving(
fwdMotors.leftServo,
fwdMotors.rightServo,
10
)
input.onButtonPressed(Button.A, function () {
for (let index = 0; index < 4; index++) {
// @highlight
fwdMotors.drive(fwdMotors.DrivingDirection.Forward, 50)
basic.pause(1000)
fwdMotors.stop()
fwdMotors.middleServo.fwdSetAngle(0)
basic.pause(250)
fwdMotors.middleServo.fwdSetAngle(45)
basic.pause(250)
}
fwdMotors.stop()
})
Can you guess which block(s) are responsible for dropping or planting the seed?
~hint Tell me more!
- The
||fwdMotors:set middleServo||
blocks are responsible for swinging the arm/pencil back and forth. This action simulates planting a seed. hint~
// @highlight
fwdMotors.middleServo.fwdSetAngle(45)
fwdMotors.setupDriving(
fwdMotors.leftServo,
fwdMotors.rightServo,
10
)
input.onButtonPressed(Button.A, function () {
for (let index = 0; index < 4; index++) {
fwdMotors.drive(fwdMotors.DrivingDirection.Forward, 50)
basic.pause(1000)
fwdMotors.stop()
// @highlight
fwdMotors.middleServo.fwdSetAngle(0)
basic.pause(250)
// @highlight
fwdMotors.middleServo.fwdSetAngle(45)
basic.pause(250)
}
fwdMotors.stop()
})
Imagine you own the company that builds these automated tree seeders. You have a customer that wants to plant 1000 trees on their field.
Given what you’ve just learned:
- How long will it take the seeder to plant this many trees?
- How far will it have to drive to meet this goal?
- How do you know?
Attempt a solution before moving to the next step.
Since we have access to the code, we know exactly what the vehicle needs to do to plant each tree! This will allow us to estimate the time it takes to plant a tree simply by looking at each block.
The micro:bit executes each block very quickly, so the contribution of each to run time is almost negligible. There is one exception, though, and that is the ||basic:pause||
blocks!
In this program, we have 3 ||basic:pause||
blocks which add up to 1500 milliseconds (or 1.5 seconds!) per loop. We also know 1 seed is planted each loop. Ultimately, this means it takes 1.5 seconds to plant each seed:
for (let index = 0; index < 4; index++) {
fwdMotors.drive(fwdMotors.DrivingDirection.Forward, 50)
// @highlight
basic.pause(1000)
fwdMotors.stop()
fwdMotors.middleServo.fwdSetAngle(0)
// @highlight
basic.pause(250)
fwdMotors.middleServo.fwdSetAngle(45)
// @highlight
basic.pause(250)
}
fwdMotors.stop()
Knowing this, we can calculate that 1000 seeds will take 1500 seconds (or 25 minutes!) to plant!
Unfortunately, we have no way to estimate the distance traveled from the code alone.
When the code doesn’t help us, we can run our program and take physical measurements with a time and a ruler. Then, we can extrapolate the data!
Try it out:
- Set up your tree seeder and mark its starting point.
- Simultaneously press A and start a timer.
- Stop the timer as the seeder plants its first seed.
- Measure the distance between these two points.
This is the time and distance it took to plant 1 seed. Multiply each value by 1000 to extrapolate to the time and distance required to plant 1000.
Did you use another approach to solve this problem?
Hint: You could also use a MakeCode extension like the Data Logger to track live data from your tree seeder!
How did the answers compare between each solution?
Why might there be slight differences?
How can we make our estimates more accurate?
You've completed the activity!
In the next step, you can click the |Done|
button to finish the tutorial.