fwd-edu-breakout=github:climate-action-kits/pxt-fwd-edu/fwd-breakout
ledRing=github:climate-action-kits/pxt-fwd-edu
soilMoisture=github:climate-action-kits/pxt-fwd-edu
Welcome to Advanced Agriculture Coding Tutorial
In this coding tutorial we will build a moisture level indicator using the LED Ring's pixels.
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.
Look below the @boardname@ simulator to see the Climate Action Board and the connected sensors. Try holding the Moisture Sensor or dipping it in a glass of water. See how the virtual simulators react.
Click ||logic: Logic||
drag and drop ||logic:If then Else||
block inside ||Basic:forever||
block.
basic.forever(function () {
if (true) {
}
else {
}
)}
Click ||fwdSensors:Sensors||
drag and drop ||fwdSensors:is soilMoisture1 moisture level over 5%||
to replace ||logic:true||
condition of ||logic:if then else||
block.
basic.forever(function () {
if (fwdSensors.soilMoisture1.fwdIsMoistureLevelPastThreshold(5, fwdSensors.ThresholdDirection.Over)) {
}
else {
}
)}
Click ||basic:basic||
drag and drop ||basic:show icon||
block inside ||logic:if then||
condition.
Select ||basic: :)||
icon.
basic.forever(function () {
if (fwdSensors.soilMoisture1.fwdIsMoistureLevelPastThreshold(5, fwdSensors.ThresholdDirection.Over)) {
basic.showIcon(IconNames.Happy)}
else {
}
)}
Click ||basic:basic||
drag and drop ||basic:show icon||
block inside ||logic:else||
condition.
Select ||basic: :(||
icon.
basic.forever(function () {
if (fwdSensors.soilMoisture1.fwdIsMoistureLevelPastThreshold(5, fwdSensors.ThresholdDirection.Over)) {
basic.showIcon(IconNames.Happy)}
else {
basic.showIcon(IconNames.Sad)}
)}
Click ||fwdMotors:Motors||
drag and drop ||fwdMotors:run pump for 500||
under
||basic: :(||
icon.
basic.forever(function () {
if (fwdSensors.soilMoisture1.fwdIsMoistureLevelPastThreshold(50, fwdSensors.ThresholdDirection.Over)) {
basic.showIcon(IconNames.Happy)
} else {
basic.showIcon(IconNames.Sad)
fwdMotors.pump.fwdTimedRun(100)
}
})
Click ||basic:basic||
drag and drop ||basic:pause (ms) 100||
block under ||fwdMotors:run pump for 500||
block.
Change the ||basic:100||
to ||basic:500||
basic.forever(function () {
if (fwdSensors.soilMoisture1.fwdIsMoistureLevelPastThreshold(50, fwdSensors.ThresholdDirection.Over)) {
basic.showIcon(IconNames.Happy)
} else {
basic.showIcon(IconNames.Sad)
fwdMotors.pump.fwdTimedRun(500)
basic.pause(500)
basic.clearScreen()
}
})
Click ||basic:basic||
drag and drop ||basic:clear screen||
block under ||basic:pause (ms) 500||
block.
basic.forever(function () {
if (fwdSensors.soilMoisture1.fwdIsMoistureLevelPastThreshold(50, fwdSensors.ThresholdDirection.Over)) {
basic.showIcon(IconNames.Happy)
} else {
basic.showIcon(IconNames.Sad)
fwdMotors.pump.fwdTimedRun(500)
basic.pause(500)
basic.clearScreen()
}
})
Click ||Logic:Logic||
drag and drop ||Logic:If true then||
block under
||Logic: If then else||
block. Repeat this step ||Logic:4||
times. Note: You should have 4 ||Logic:If then||
blocks.
basic.forever(function () {
if (fwdSensors.soilMoisture1.fwdIsMoistureLevelPastThreshold(50, fwdSensors.ThresholdDirection.Over)) {
basic.showIcon(IconNames.Happy)
} else {
basic.showIcon(IconNames.Sad)
fwdMotors.pump.fwdTimedRun(500)
basic.pause(500)
basic.clearScreen()
}
if(true){
}
if(true){
}
if (true){
}
if (true){
}
})
Click ||fwdSensors:Sensors||
drag and drop ||fwdSensors:soilMoisture1 level is over||
block
to replace ||Logic:true||
condition of ||Logic:4 if true then||
blocks.
basic.forever(function () {
if (fwdSensors.soilMoisture1.fwdIsMoistureLevelPastThreshold(50, fwdSensors.ThresholdDirection.Over)) {
basic.showIcon(IconNames.Happy)
} else {
basic.showIcon(IconNames.Sad)
fwdMotors.pump.fwdTimedRun(500)
basic.pause(500)
basic.clearScreen()
}
if(fwdSensors.soilMoisture1.fwdIsMoistureLevelPastThreshold(5, fwdSensors.ThresholdDirection.Over)){
}
if(fwdSensors.soilMoisture1.fwdIsMoistureLevelPastThreshold(5, fwdSensors.ThresholdDirection.Over)){
}
if(fwdSensors.soilMoisture1.fwdIsMoistureLevelPastThreshold(5, fwdSensors.ThresholdDirection.Over)){
}
if(fwdSensors.soilMoisture1.fwdIsMoistureLevelPastThreshold(5, fwdSensors.ThresholdDirection.Over)){
}
})
Click ||fwdSensors:Sensors||
drag and drop ||fwdSensors:set all ledRing LEDs to||
block
under ||fwdSensors:soilMoisture1 level is over||
block.
basic.forever(function () {
if (fwdSensors.soilMoisture1.fwdIsMoistureLevelPastThreshold(50, fwdSensors.ThresholdDirection.Over)) {
basic.showIcon(IconNames.Happy)
} else {
basic.showIcon(IconNames.Sad)
fwdMotors.pump.fwdTimedRun(500)
basic.pause(500)
basic.clearScreen()
}
if(fwdSensors.soilMoisture1.fwdIsMoistureLevelPastThreshold(5, fwdSensors.ThresholdDirection.Over)){
fwdSensors.ledRing.fwdSetAllPixelsColour(0xff0000)
}
if(fwdSensors.soilMoisture1.fwdIsMoistureLevelPastThreshold(5, fwdSensors.ThresholdDirection.Over)){
}
if(fwdSensors.soilMoisture1.fwdIsMoistureLevelPastThreshold(5, fwdSensors.ThresholdDirection.Over)){
}
if(fwdSensors.soilMoisture1.fwdIsMoistureLevelPastThreshold(5, fwdSensors.ThresholdDirection.Over)){
}
})
Click ||fwdSensors:Sensors||
drag and drop ||fwdSensors:set ledRing 0 to||
block
under remaining ||fwdSensors:3 soilMoisture1 level is over||
blocks.
basic.forever(function () {
if (fwdSensors.soilMoisture1.fwdIsMoistureLevelPastThreshold(50, fwdSensors.ThresholdDirection.Over)) {
basic.showIcon(IconNames.Happy)
} else {
basic.showIcon(IconNames.Sad)
fwdMotors.pump.fwdTimedRun(500)
basic.pause(500)
basic.clearScreen()
}
if(fwdSensors.soilMoisture1.fwdIsMoistureLevelPastThreshold(5, fwdSensors.ThresholdDirection.Over)){
fwdSensors.ledRing.fwdSetAllPixelsColour(0xff0000)
}
if(fwdSensors.soilMoisture1.fwdIsMoistureLevelPastThreshold(5, fwdSensors.ThresholdDirection.Over)){
fwdSensors.ledRing.fwdSetPixelColour(0, 0xff0000)
}
if(fwdSensors.soilMoisture1.fwdIsMoistureLevelPastThreshold(5, fwdSensors.ThresholdDirection.Over)){
fwdSensors.ledRing.fwdSetPixelColour(0, 0xff0000)
}
if(fwdSensors.soilMoisture1.fwdIsMoistureLevelPastThreshold(5, fwdSensors.ThresholdDirection.Over)){
fwdSensors.ledRing.fwdSetPixelColour(0, 0xff0000)
}
})
Right click ||fwdSensors:set ledRing 0||
block and duplicate it. Each
||Logic:If||
||fwdSensors:soilMoisture1 is over||
||Logic:then||
block
should have ||fwdSensors:2 set ledRing 0||
blocks inside the ||Logic:If condition||
block.
basic.forever(function () {
if (fwdSensors.soilMoisture1.fwdIsMoistureLevelPastThreshold(50, fwdSensors.ThresholdDirection.Over)) {
basic.showIcon(IconNames.Happy)
} else {
basic.showIcon(IconNames.Sad)
fwdMotors.pump.fwdTimedRun(500)
basic.pause(500)
basic.clearScreen()
}
if(fwdSensors.soilMoisture1.fwdIsMoistureLevelPastThreshold(5, fwdSensors.ThresholdDirection.Over)){
fwdSensors.ledRing.fwdSetAllPixelsColour(0xff0000)
}
if(fwdSensors.soilMoisture1.fwdIsMoistureLevelPastThreshold(5, fwdSensors.ThresholdDirection.Over)){
fwdSensors.ledRing.fwdSetPixelColour(0, 0xff0000)
fwdSensors.ledRing.fwdSetPixelColour(0, 0xff0000)
}
if(fwdSensors.soilMoisture1.fwdIsMoistureLevelPastThreshold(5, fwdSensors.ThresholdDirection.Over)){
fwdSensors.ledRing.fwdSetPixelColour(0, 0xff0000)
fwdSensors.ledRing.fwdSetPixelColour(0, 0xff0000)
}
if(fwdSensors.soilMoisture1.fwdIsMoistureLevelPastThreshold(5, fwdSensors.ThresholdDirection.Over)){
fwdSensors.ledRing.fwdSetPixelColour(0, 0xff0000)
fwdSensors.ledRing.fwdSetPixelColour(0, 0xff0000)
}
})
Change the threshold values of the ||fwdSensors:soilMoisture1 level over||
block
||fwdSensors:soilMositure1 level over 80||
||fwdSensors:soilMositure1 level over 60||
||fwdSensors:soilMositure1 level over 40||
||fwdSensors:soilMositure1 level over 20||
basic.forever(function () {
if (fwdSensors.soilMoisture1.fwdIsMoistureLevelPastThreshold(50, fwdSensors.ThresholdDirection.Over)) {
basic.showIcon(IconNames.Happy)
} else {
basic.showIcon(IconNames.Sad)
fwdMotors.pump.fwdTimedRun(500)
basic.pause(500)
basic.clearScreen()
}
if (fwdSensors.soilMoisture1.fwdIsMoistureLevelPastThreshold(80, fwdSensors.ThresholdDirection.Over)) {
fwdSensors.ledRing.fwdSetAllPixelsColour(0xff0000)
}
if (fwdSensors.soilMoisture1.fwdIsMoistureLevelPastThreshold(60, fwdSensors.ThresholdDirection.Over)) {
fwdSensors.ledRing.fwdSetPixelColour(0, 0xff0000)
fwdSensors.ledRing.fwdSetPixelColour(0, 0xff0000)
}
if (fwdSensors.soilMoisture1.fwdIsMoistureLevelPastThreshold(40, fwdSensors.ThresholdDirection.Over)) {
fwdSensors.ledRing.fwdSetPixelColour(0, 0xff0000)
fwdSensors.ledRing.fwdSetPixelColour(0, 0xff0000)
}
if (fwdSensors.soilMoisture1.fwdIsMoistureLevelPastThreshold(20, fwdSensors.ThresholdDirection.Over)) {
fwdSensors.ledRing.fwdSetPixelColour(0, 0xff0000)
fwdSensors.ledRing.fwdSetPixelColour(0, 0xff0000)
}
})
Change the ||fwdSensors:LED Ring pixel values||
in pairs.
- Pair 1
||fwdSensors:set ledRing 0 to 5||
||fwdSensors:set ledRing 0 to 4||
- Pair 2
||fwdSensors:set ledRing 0 to 3||
||fwdSensors:set ledRing 0 to 2||
- Pair 3
||fwdSensors:set ledRing 0 to 1||
||fwdSensors:set ledRing 0 to 0||
basic.forever(function () {
if (fwdSensors.soilMoisture1.fwdIsMoistureLevelPastThreshold(50, fwdSensors.ThresholdDirection.Over)) {
basic.showIcon(IconNames.Happy)
} else {
basic.showIcon(IconNames.Sad)
fwdMotors.pump.fwdTimedRun(500)
basic.pause(500)
basic.clearScreen()
}
if (fwdSensors.soilMoisture1.fwdIsMoistureLevelPastThreshold(80, fwdSensors.ThresholdDirection.Over)) {
fwdSensors.ledRing.fwdSetAllPixelsColour(0xff0000)
}
if (fwdSensors.soilMoisture1.fwdIsMoistureLevelPastThreshold(60, fwdSensors.ThresholdDirection.Over)) {
fwdSensors.ledRing.fwdSetPixelColour(5, 0xff0000)
fwdSensors.ledRing.fwdSetPixelColour(4, 0xff0000)
}
if (fwdSensors.soilMoisture1.fwdIsMoistureLevelPastThreshold(40, fwdSensors.ThresholdDirection.Over)) {
fwdSensors.ledRing.fwdSetPixelColour(3, 0xff0000)
fwdSensors.ledRing.fwdSetPixelColour(2, 0xff0000)
}
if (fwdSensors.soilMoisture1.fwdIsMoistureLevelPastThreshold(20, fwdSensors.ThresholdDirection.Over)) {
fwdSensors.ledRing.fwdSetPixelColour(1, 0xff0000)
fwdSensors.ledRing.fwdSetPixelColour(0, 0xff0000)
}
})
|Download|
and test your code. Click the bulb icon to see how
the simulator shows the components working.
Congratulations on completing your Advanced Agriculture Project!
After your project is complete, go back to the lesson for more challenges and extensions.