fwd-edu-breakout=github:climate-action-kits/pxt-fwd-edu/fwd-breakout
solar=github:climate-action-kits/pxt-fwd-edu
Welcome to Electric Vehicle Coding Tutorial
Plug your USB cable into the micro:bit.
Insert it into the Climate Action Kit board.
Click three dots besides |Download|
button and follow the steps to pair your micro:bit.
Look below the @boardname@ simulator to see the Climate Action Board and the connected sensors. Try to interact with your sensor and the simulator will react to it.
Click ||fwdMotors:Motors||
drag and drop ||fwdMotors:Setup Driving||
block inside ||basic:on start||
loop.
fwdMotors.setupDriving(
fwdMotors.leftServo,
fwdMotors.leftServo,
)
Change the ||fwdMotors:right motor to middleServo||
.
Keep the ||fwdMotors: left motor to leftServo||
.
fwdMotors.setupDriving(
fwdMotors.leftServo,
fwdMotors.middleServo,
)
Click ||logic:Logic||
drag and drop ||logic:if true then||
block inside ||basic:forever||
loop.
fwdMotors.setupDriving(
fwdMotors.leftServo,
fwdMotors.middleServo,
)
basic.forever(function () {
if (true) {
}
})
Click ||logic:Logic||
drag and drop ||logic:if true then||
block under the 1st ||logic:if true then||
block
fwdMotors.setupDriving(
fwdMotors.leftServo,
fwdMotors.middleServo,
)
basic.forever(function () {
if (true) {
}
if (true) {
}
})
Click ||logic:Logic||
drag and drop ||logic:if true then||
block under the 2nd ||logic:if true then||
block. Note: Three ||logic:if true then||
blocks are used.
fwdMotors.setupDriving(
fwdMotors.leftServo,
fwdMotors.middleServo,
)
basic.forever(function () {
if (true) {
}
if (true) {
}
if (true) {
}
})
Click ||fwdSensors:Sensors||
drag and drop ||fwdSensors: line1 state is ●||
to replace ||logic:true||
condition of 1st ||logic:if true then||
block.
fwdMotors.setupDriving(
fwdMotors.leftServo,
fwdMotors.middleServo,
)
basic.forever(function () {
if (fwdSensors.line1.fwdIsLineSensorState(fwdSensors.LineSensorState.Hit)) {
}
if (true) {
}
if (true) {
}
})
Click ||fwdSensors:Sensors||
drag and drop ||fwdSensors: line2 state is ○||
to replace ||logic:true||
condition of 2nd ||logic:if true then||
block. Note: Use drop down menu to change line number.
fwdMotors.setupDriving(
fwdMotors.leftServo,
fwdMotors.middleServo,
)
basic.forever(function () {
if (fwdSensors.line1.fwdIsLineSensorState(fwdSensors.LineSensorState.Hit)) {
}
if (fwdSensors.line2.fwdIsLineSensorState(fwdSensors.LineSensorState.Miss)) {
}
if (true) {
}
})
Click ||fwdSensors:Sensors||
drag and drop ||fwdSensors: line3 state is ●||
to replace ||logic:true||
condition of 3rd ||logic:if true then||
block. Note: Use drop down menu to change line number.
fwdMotors.setupDriving(
fwdMotors.leftServo,
fwdMotors.middleServo,
)
basic.forever(function () {
if (fwdSensors.line1.fwdIsLineSensorState(fwdSensors.LineSensorState.Hit)) {
}
if (fwdSensors.line2.fwdIsLineSensorState(fwdSensors.LineSensorState.Miss)) {
}
if (fwdSensors.line3.fwdIsLineSensorState(fwdSensors.LineSensorState.Hit)) {
}
})
Click ||fwdMotors: Motors||
drag and drop ||fwdMotors: Turn 0° in place||
block inside 1st ||logic: if||
||fwdSensors:line1 state is ●||
||logic:then||
condition.
fwdMotors.setupDriving(
fwdMotors.leftServo,
fwdMotors.middleServo,
)
basic.forever(function () {
if (fwdSensors.line1.fwdIsLineSensorState(fwdSensors.LineSensorState.Hit)) {
fwdMotors.turn(0)
}
if (fwdSensors.line2.fwdIsLineSensorState(fwdSensors.LineSensorState.Miss)) {
}
if (fwdSensors.line3.fwdIsLineSensorState(fwdSensors.LineSensorState.Hit)) {
})
Change ||fwdMotors:Turn 0°||
to ||fwdMotors:5°||
fwdMotors.setupDriving(
fwdMotors.leftServo,
fwdMotors.middleServo,
)
basic.forever(function () {
if (fwdSensors.line1.fwdIsLineSensorState(fwdSensors.LineSensorState.Hit)) {
fwdMotors.turn(5)
}
if (fwdSensors.line2.fwdIsLineSensorState(fwdSensors.LineSensorState.Miss)) {
}
if (fwdSensors.line3.fwdIsLineSensorState(fwdSensors.LineSensorState.Hit)) {
})
Click ||fwdMotors:Motors||
drag and drop ||fwdMotors:Drive forward 50%||
block inside 2nd
||logic:if||
||fwdSensors:line2 state is ○||
||logic:then||
condition.
Change the ||fwdMotors:Drive forward 50%||
to ||fwdMotors:20%||
fwdMotors.setupDriving(
fwdMotors.leftServo,
fwdMotors.middleServo,
)
basic.forever(function () {
if (fwdSensors.line1.fwdIsLineSensorState(fwdSensors.LineSensorState.Hit)) {
fwdMotors.turn(5)
}
if (fwdSensors.line2.fwdIsLineSensorState(fwdSensors.LineSensorState.Miss)) {
fwdMotors.drive(fwdMotors.DrivingDirection.Forward, 20)
}
if (fwdSensors.line3.fwdIsLineSensorState(fwdSensors.LineSensorState.Hit)) {
}
})
Click ||fwdMotors:Motors||
drag and drop ||fwdMotors: Turn 0° in place||
block inside 3rd ||logic:if||
||fwdSensors:line3 state is ●||
||logic:then||
condition.
Change the ||fwdMotors:Turn 0°||
to ||fwdMotors:-5°||
.
fwdMotors.setupDriving(
fwdMotors.leftServo,
fwdMotors.middleServo,
)
basic.forever(function () {
if (fwdSensors.line1.fwdIsLineSensorState(fwdSensors.LineSensorState.Hit)) {
fwdMotors.turn(5)
}
if (fwdSensors.line2.fwdIsLineSensorState(fwdSensors.LineSensorState.Miss)) {
fwdMotors.drive(fwdMotors.DrivingDirection.Forward, 20)
}
if (fwdSensors.line3.fwdIsLineSensorState(fwdSensors.LineSensorState.Hit)) {
fwdMotors.turn(-5)
}
})
Click ||basic:Basic||
drag and drop ||basic:pause (ms) 100||
block under ||fwdMotors:Turn 5° in place||
block.
fwdMotors.setupDriving(
fwdMotors.leftServo,
fwdMotors.middleServo,
)
basic.forever(function () {
if (fwdSensors.line1.fwdIsLineSensorState(fwdSensors.LineSensorState.Hit)) {
fwdMotors.turn(5)
basic.pause(100)
}
if (fwdSensors.line2.fwdIsLineSensorState(fwdSensors.LineSensorState.Miss)) {
fwdMotors.drive(fwdMotors.DrivingDirection.Forward, 20)
}
if (fwdSensors.line3.fwdIsLineSensorState(fwdSensors.LineSensorState.Hit)) {
fwdMotors.turn(-5)
}
})
Click ||basic:basic||
drag and drop ||basic:pause (ms) 100||
block under ||fwdMotors:Drive Forward at 20%||
block.
fwdMotors.setupDriving(
fwdMotors.leftServo,
fwdMotors.middleServo,
)
basic.forever(function () {
if (fwdSensors.line1.fwdIsLineSensorState(fwdSensors.LineSensorState.Hit)) {
fwdMotors.turn(5)
basic.pause(100)
}
if (fwdSensors.line2.fwdIsLineSensorState(fwdSensors.LineSensorState.Miss)) {
fwdMotors.drive(fwdMotors.DrivingDirection.Forward, 20)
basic.pause(100)
}
if (fwdSensors.line3.fwdIsLineSensorState(fwdSensors.LineSensorState.Hit)) {
fwdMotors.turn(-5)
}
})
Click ||basic:basic||
drag and drop ||basic:pause (ms) 100||
block under ||fwdMotors:Turn -5° in place||
block.
fwdMotors.setupDriving(
fwdMotors.leftServo,
fwdMotors.middleServo,
)
basic.forever(function () {
if (fwdSensors.line1.fwdIsLineSensorState(fwdSensors.LineSensorState.Hit)) {
fwdMotors.turn(5)
basic.pause(100)
}
if (fwdSensors.line2.fwdIsLineSensorState(fwdSensors.LineSensorState.Miss)) {
fwdMotors.drive(fwdMotors.DrivingDirection.Forward, 20)
basic.pause(100)
}
if (fwdSensors.line3.fwdIsLineSensorState(fwdSensors.LineSensorState.Hit)) {
fwdMotors.turn(-5)
basic.pause(100)
}
})
Click ||fwdMotors:+||
on ||fwdMotors:Setup Driving||
block inside ||basic:on start||
block. Set bias to ||fwdMotors: 0||
.
Change ||basic:pause (ms) 100||
to ||basic:500||
for all
||basic:pause||
blocks.
fwdMotors.setupDriving(
fwdMotors.leftServo,
fwdMotors.middleServo,
0
)
basic.forever(function () {
if (fwdSensors.line1.fwdIsLineSensorState(fwdSensors.LineSensorState.Hit)) {
fwdMotors.turn(5)
basic.pause(500)
}
if (fwdSensors.line2.fwdIsLineSensorState(fwdSensors.LineSensorState.Miss)) {
fwdMotors.drive(fwdMotors.DrivingDirection.Forward, 20)
basic.pause(500)
}
if (fwdSensors.line3.fwdIsLineSensorState(fwdSensors.LineSensorState.Hit)) {
fwdMotors.turn(-5)
basic.pause(500)
}
})
|Download|
and test your code. The simulator shows how it should work.
Congratulations on completing your Electric Vehicle Prototype! - Go back to the lesson for more activities and extensions.