fwd-edu-breakout=github:climate-action-kits/pxt-fwd-edu/fwd-breakout
sonar=github:climate-action-kits/pxt-fwd-edu
Welcome to Monitoring Glaciers
In this coding tutorial the device continuously measures distances to its left and right, and decides which direction to go based on which path is less obstructed.
Turn on the Climate Action Kit board.
Click three dots besides |Download|
button, and click on Connect Device. Next, follow the steps to pair your micro:bit.
Next, click the |Download|
button to download the blank project to start-up the simulators.
This is how the simulators should look after a successful download. You can see the Servo Motors along side the Pump.
Look below the @boardname@ simulator to see the Climate Action Board and the connected devices. Try to turn the motors on and off using the simulator and observe the changes.
Click ||fwdMotors:Motors||
drag and drop ||fwdMotors:set leftServo to 0'||
block
inside ||Basic:on start||
block. Change ||fwdMotors:leftServo||
to ||fwdMotors:middleServo||
.
fwdMotors.middleServo.fwdSetAngle(0)
basic.forever(function () {
})
Click ||fwdMotors:Motors||
drag and drop ||fwdMotors:set leftServo to 0'||
block
inside ||Basic:forever||
block. Change ||fwdMotors:leftServo||
to ||fwdMotors:middleServo||
.
Also, change ||fwdMotors:0'||
to ||fwdMotors:90'||
fwdMotors.middleServo.fwdSetAngle(0)
basic.forever(function () {
fwdMotors.middleServo.fwdSetAngle(90)
})
Click ||Variables:Variables||
and then click |Make a Variable| to create
these ||Variables:Variables||
||Variables:RightDistance||
||Variables:LeftDistance||
Click ||Basic:Basic||
drag and drop ||Basic:pause (ms) 100||
block
inside ||Basic:forever||
block under ||fwdMotors:set middleServo to 90'||
block.
Change ||Basic:100||
to ||Basic:2000||
.
fwdMotors.middleServo.fwdSetAngle(0)
basic.forever(function () {
fwdMotors.middleServo.fwdSetAngle(90)
basic.pause(2000)
})
Click ||Variables:Variables||
drag and drop ||Variables:set RightDistance to 0||
block
inside ||Basic:forever||
block under ||Basic:pause (ms) 2000||
block.
fwdMotors.middleServo.fwdSetAngle(0)
basic.forever(function () {
fwdMotors.middleServo.fwdSetAngle(90)
basic.pause(2000)
RightDistance = 0
})
Click ||fwdSensors:Sensors||
drag and drop ||fwdSensors:sonar1 distance (m)||
block
to replace ||Variables:0||
of ||Variables:set RightDistance to 0||
block.
fwdMotors.middleServo.fwdSetAngle(0)
basic.forever(function () {
fwdMotors.middleServo.fwdSetAngle(90)
basic.pause(2000)
RightDistance = fwdSensors.sonar1.fwdDistance()
})
Click ||Basic:Basic||
drag and drop ||Basic:pause (ms) 100||
block
under ||Variables:set RightDistance to||
||fwdSensors:sonar1 distance (m)||
.
Change ||Basic:100||
to ||Basic:2000||
.
fwdMotors.middleServo.fwdSetAngle(0)
basic.forever(function () {
fwdMotors.middleServo.fwdSetAngle(90)
basic.pause(2000)
RightDistance = fwdSensors.sonar1.fwdDistance()
basic.pause(2000)
})
Click ||fwdMotors:Motors||
drag and drop ||fwdMotors:set leftServo to 0'||
block
under ||Basic:pause (ms) 2000||
block. Change ||fwdMotors:leftServo||
to ||fwdMotors:middleServo||
and
change ||fwdMotors:0'||
to ||fwdMotors:-90'||
.
fwdMotors.middleServo.fwdSetAngle(0)
basic.forever(function () {
fwdMotors.middleServo.fwdSetAngle(90)
basic.pause(2000)
RightDistance = fwdSensors.sonar1.fwdDistance()
basic.pause(2000)
fwdMotors.middleServo.fwdSetAngle(-90)
})
Click ||Basic:Basic||
drag and drop ||Basic:pause (ms) 100||
block under ||fwdMotors:set middleServo to -90'||
block. Change ||Basic:100||
to ||Basic:2000||
fwdMotors.middleServo.fwdSetAngle(0)
basic.forever(function () {
fwdMotors.middleServo.fwdSetAngle(90)
basic.pause(2000)
RightDistance = fwdSensors.sonar1.fwdDistance()
basic.pause(2000)
fwdMotors.middleServo.fwdSetAngle(-90)
basic.pause(2000)
})
Click ||Variables:Variables||
drag and drop ||Variables:set LeftDistance to 0||
block
under ||Basic:pause (ms) 2000||
block.
fwdMotors.middleServo.fwdSetAngle(0)
basic.forever(function () {
fwdMotors.middleServo.fwdSetAngle(90)
basic.pause(2000)
RightDistance = fwdSensors.sonar1.fwdDistance()
basic.pause(2000)
fwdMotors.middleServo.fwdSetAngle(-90)
basic.pause(2000)
LeftDistance = 0
})
Click ||fwdSensors:Sensors||
drag and drop ||fwdSensors:sonar1 distance (m)||
block
to replace ||Variables:0||
of ||Variables:set LeftDistance to 0||
block.
fwdMotors.middleServo.fwdSetAngle(0)
basic.forever(function () {
fwdMotors.middleServo.fwdSetAngle(90)
basic.pause(2000)
RightDistance = fwdSensors.sonar1.fwdDistance()
basic.pause(2000)
fwdMotors.middleServo.fwdSetAngle(-90)
basic.pause(2000)
LeftDistance = fwdSensors.sonar1.fwdDistance()
})
Click ||Basic:Basic||
drag and drop ||Basic:pause (ms) 100||
block
under ||Variables:set LeftDistance to||
||fwdSensors:sonar1 distance (m)||
block
. Change ||Basic:100||
to ||Basic:2000||
.
fwdMotors.middleServo.fwdSetAngle(0)
basic.forever(function () {
fwdMotors.middleServo.fwdSetAngle(90)
basic.pause(2000)
RightDistance = fwdSensors.sonar1.fwdDistance()
basic.pause(2000)
fwdMotors.middleServo.fwdSetAngle(-90)
basic.pause(2000)
LeftDistance = fwdSensors.sonar1.fwdDistance()
basic.pause(2000)
})
Click ||Logic:Logic||
drag and drop ||Logic:if true then else||
block under ||Basic:pause (ms) 2000||
block.
fwdMotors.middleServo.fwdSetAngle(0)
basic.forever(function () {
fwdMotors.middleServo.fwdSetAngle(90)
basic.pause(2000)
RightDistance = fwdSensors.sonar1.fwdDistance()
basic.pause(2000)
fwdMotors.middleServo.fwdSetAngle(-90)
basic.pause(2000)
LeftDistance = fwdSensors.sonar1.fwdDistance()
basic.pause(2000)
if (true) {
} else {
}
})
Click ||Logic:Logic||
drag and drop ||Logic:0 < 0||
block to replace ||Logic:true||
condition.
fwdMotors.middleServo.fwdSetAngle(0)
basic.forever(function () {
fwdMotors.middleServo.fwdSetAngle(90)
basic.pause(2000)
RightDistance = fwdSensors.sonar1.fwdDistance()
basic.pause(2000)
fwdMotors.middleServo.fwdSetAngle(-90)
basic.pause(2000)
LeftDistance = fwdSensors.sonar1.fwdDistance()
basic.pause(2000)
if (0 < 0) {
} else {
}
})
Click ||Variables:Variables||
drag and drop ||Variables:RightDistance||
block to
replace left ||Logic:0||
of ||Logic:Comparison||
block. Click ||Variables:Variables||
again
drag and drop ||Variables:LeftDistance||
block to
replace right ||Logic:0||
of ||Logic:Comparison||
block.
fwdMotors.middleServo.fwdSetAngle(0)
basic.forever(function () {
fwdMotors.middleServo.fwdSetAngle(90)
basic.pause(2000)
RightDistance = fwdSensors.sonar1.fwdDistance()
basic.pause(2000)
fwdMotors.middleServo.fwdSetAngle(-90)
basic.pause(2000)
LeftDistance = fwdSensors.sonar1.fwdDistance()
basic.pause(2000)
if (RightDistance < LeftDistance) {
} else {
}
})
Click ||Basic:Basic||
drag and drop ||Basic:show string " "||
block
inside ||Logic:if then||
condition. Type ||Basic:Go Left||
in ||Basic:show string " "||
block.
fwdMotors.middleServo.fwdSetAngle(0)
basic.forever(function () {
fwdMotors.middleServo.fwdSetAngle(90)
basic.pause(2000)
RightDistance = fwdSensors.sonar1.fwdDistance()
basic.pause(2000)
fwdMotors.middleServo.fwdSetAngle(-90)
basic.pause(2000)
LeftDistance = fwdSensors.sonar1.fwdDistance()
basic.pause(2000)
if (RightDistance < LeftDistance) {
basic.showString("Go Left")
} else {
}
})
Click ||Basic:Basic||
drag and drop ||Basic:clear screen||
block under
||Basic:show string "Go Left"||
block.
fwdMotors.middleServo.fwdSetAngle(0)
basic.forever(function () {
fwdMotors.middleServo.fwdSetAngle(90)
basic.pause(2000)
RightDistance = fwdSensors.sonar1.fwdDistance()
basic.pause(2000)
fwdMotors.middleServo.fwdSetAngle(-90)
basic.pause(2000)
LeftDistance = fwdSensors.sonar1.fwdDistance()
basic.pause(2000)
if (RightDistance < LeftDistance) {
basic.showString("Go Left")
basic.clearScreen()
} else {
}
})
Click ||Basic:Basic||
drag and drop ||Basic:show string " "||
block
inside ||Logic:if then||
condition. Type ||Basic:Go Right||
in ||Basic:show string " "||
block.
fwdMotors.middleServo.fwdSetAngle(0)
basic.forever(function () {
fwdMotors.middleServo.fwdSetAngle(90)
basic.pause(2000)
RightDistance = fwdSensors.sonar1.fwdDistance()
basic.pause(2000)
fwdMotors.middleServo.fwdSetAngle(-90)
basic.pause(2000)
LeftDistance = fwdSensors.sonar1.fwdDistance()
basic.pause(2000)
if (RightDistance < LeftDistance) {
basic.showString("Go Left")
basic.clearScreen()
} else {
basic.showString("Go Right")
}
})
Click ||Basic:Basic||
drag and drop ||Basic:clear screen||
block under
||Basic:show string "Go Right"||
block.
fwdMotors.middleServo.fwdSetAngle(0)
basic.forever(function () {
fwdMotors.middleServo.fwdSetAngle(90)
basic.pause(2000)
RightDistance = fwdSensors.sonar1.fwdDistance()
basic.pause(2000)
fwdMotors.middleServo.fwdSetAngle(-90)
basic.pause(2000)
LeftDistance = fwdSensors.sonar1.fwdDistance()
basic.pause(2000)
if (RightDistance < LeftDistance) {
basic.showString("Go Left")
basic.clearScreen()
} else {
basic.showString("Go Right")
basic.clearScreen()
}
})
Click ||Basic:Basic||
drag and drop ||Basic:pause (ms) 100||
block under ||Logic:if then else ||
block. Change ||Basic:100||
to ||Basic:1000||
.
fwdMotors.middleServo.fwdSetAngle(0)
basic.forever(function () {
fwdMotors.middleServo.fwdSetAngle(90)
basic.pause(2000)
RightDistance = fwdSensors.sonar1.fwdDistance()
basic.pause(2000)
fwdMotors.middleServo.fwdSetAngle(-90)
basic.pause(2000)
LeftDistance = fwdSensors.sonar1.fwdDistance()
basic.pause(2000)
if (RightDistance < LeftDistance) {
basic.showString("Go Left")
basic.clearScreen()
} else {
basic.showString("Go Right")
basic.clearScreen()
}
basic.pause(1000)
})
Click ||Basic:Basic||
drag and drop ||Basic:pause (ms) 100||
block under
||Logic:else||
condition. Change ||Basic:100||
to ||Basic:1000||
.
fwdMotors.middleServo.fwdSetAngle(0)
basic.forever(function () {
fwdMotors.middleServo.fwdSetAngle(90)
basic.pause(2000)
RightDistance = fwdSensors.sonar1.fwdDistance()
basic.pause(2000)
fwdMotors.middleServo.fwdSetAngle(-90)
basic.pause(2000)
LeftDistance = fwdSensors.sonar1.fwdDistance()
basic.pause(2000)
if (RightDistance < LeftDistance) {
basic.showString("Go Left")
basic.clearScreen()
} else {
basic.showString("Go Right")
basic.clearScreen()
}
basic.pause(1000)
})
|Download|
and test your code. Click the bulb icon to see how
the simulator shows the components working.
|Download|
and test your code. Click the bulb icon to see how
the simulator shows the components working.
If after |Downloading|
your project does not work please refer to the
image and make sure your components are assigned correctly.
Need help in assigning the right components to their simulators. Watch the video.
Congratulations on completing your Monitoring Glaciers Project!
After your project is complete go back to the lesson for more challenges and extensions.