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 () {
stationOn = true
})
function rotateSensor () {
fwdMotors.rightServo.fwdSetAngleAndWait(angle)
angle += 10
if (angle >= 210) {
angle = 0
}
}
input.onButtonPressed(Button.B, function () {
stationOn = false
})
let angle = 0
let stationOn = false
stationOn = false
angle = 0
fwdSensors.ledRing.fwdSetAllPixelsColour(0x000000)
basic.forever(function () {
if (stationOn == true) {
if (fwdSensors.sonar1.fwdDistance() > 0.1) {
fwdSensors.ledRing.fwdSetAllPixelsColour(0x000000)
rotateSensor()
} else {
fwdSensors.ledRing.fwdSetAllPixelsColour(0xff0000)
}
basic.pause(100)
} else {
fwdMotors.rightServo.fwdSetEnabled(false)
}
})
Let's build a Hope Spot Monitoring Station! This station will deter ships from entering a Hope Spot by issuing a warning.
We are going to do this in three parts:
- Build your station
- Add code to bring it to life
- Use and study your station to learn how it works
Now that your station is built, let’s connect it to your computer and upload code so it can detect ships and issue alerts.
IMPORTANT! Make sure your Climate Action Kit Breakout Board is turned on and your micro:bit is connected to your computer.
Click the three dots next to the |Download|
button, then select Connect Device. Follow the instructions to pair your micro:bit.
Click the |Download|
button to transfer the code to your project.
We are now ready to use our Hope Spot Monitoring Station!
Tutorial Tips
- Follow the steps at the top of the screen.
- For more details, click 'Tell me more!'
- If you need help with the code, click the lightbulb!
This monitoring station will help protect our Hope Spot. It does this by detecting nearby ships and sending out a warning when they are too close to the conservation area.
Take a look at the physical station.
What robotic components do you notice? How do you think they’ll work together to make the station function as expected?
~hint Tell me more! The station includes:
- One positional servo motor that rotates between 0° and 270°.
- One sonar sensor that calculates the distance of an object by sending out sound waves and measuring the time for the “echo” to return.
- One LED ring that can display a variety of colours.
- These parts are all connected to the breakout board through cables.
- Finally, the breakout board is connected to the micro:bit which holds all the code that will tell our robotic components what to do and when to do it! hint~
Examine the code in the workspace.
What do you predict will happen when you run the program? Which parts of the code influenced your prediction?
Turn the station on by pressing the 'A' button on the micro:bit.
Does the station behave as you predicted? If not, what was different? Try to explain these differences.
The servo motor is rotating the sonar sensor so it can scan the area for ships. Observe this movement closely. Write down the angles scanned by the sensor. Tip: It may be helpful to also look at the simulators for this!
Based on your analysis, can you identify which part of the program controls this movement?
~hint Tell me more!
- In the code, the
||Functions:rotateSensor||
function moves the sonar sensor in 10° increments, up to 210°, then resets to 0° to start a new scan. hint~
function rotateSensor () {
fwdMotors.rightServo.fwdSetAngleAndWait(angle)
angle += 10
if (angle >= 210) {
angle = 0
}
}
Functions are helpful because they can make programs easier to read. In our code, where do we call the ||Functions:rotateSensor||
function?
~hint Tell me more!
- We call the
||Functions:rotateSensor||
function whenever the sonar distance is greater than 0.1 m using a conditional statement. In other words, the sensor continues to rotate as long as there are no nearby ships. hint~
// @hide
function rotateSensor () {
fwdMotors.rightServo.fwdSetAngleAndWait(angle)
angle += 10
if (angle >= 210) {
angle = 0
}
}
if (fwdSensors.sonar1.fwdDistance() > 0.1) {
fwdSensors.ledRing.fwdSetAllPixelsColour(0x000000)
// @highlight
rotateSensor()
} else {
fwdSensors.ledRing.fwdSetAllPixelsColour(0xff0000)
}
Functions also make code reusable.
Can you think of other situations or projects where this type of rotate function might be useful?
~hint Tell me more!
- Rotating functions are helpful for scanning across ranges, like tracking sunlight with a solar sensor. hint~
Simulate an approaching ship by placing an object near the sonar sensor. Observe how the station responds as the object moves closer.
What part of the program is responsible for this response?
~hint Tell me more!
- The conditional statement in the code controls when the LED colour changes. It turns red when an object is detected within 0.1 m. hint~
// @hide
function rotateSensor () {
fwdMotors.rightServo.fwdSetAngleAndWait(angle)
angle += 10
if (angle >= 210) {
angle = 0
}
}
if (fwdSensors.sonar1.fwdDistance() > 0.1) {
fwdSensors.ledRing.fwdSetAllPixelsColour(0x000000)
rotateSensor()
} else {
// @highlight
fwdSensors.ledRing.fwdSetAllPixelsColour(0xff0000)
}
What additional features could make this station more effective at protecting its Hope Spot? How might you use the Climate Action Kit to incorporate these features?
~hint Tell me more!
- Data Logging: Recording each detected ship’s distance and timestamp could help track ship activity trends.
- Radio Alerts: Radio signals can notify remote teams when a ship is detected.
Check out the Modify tutorial to see how we can edit our program to include some of these functionalities. hint~
Imagine this station is built from on a reclaimed oil rig.
Why might reclaimed rigs be valuable for marine conservation? Do some rapid research to justify your response.
You've completed the activity!
List 2 new things you learned today.
Think critically about this station’s impact on marine conservation. How could systems like this be implemented at scale to protect multiple Hope Spots? Record any ideas or further questions you have.
In the next step, you can click the |Done|
button to finish the tutorial.