In MakeCode, the pin blocks are under Advanced, then Pins. Go as far as you like: every story
works on its own.
Start the MakeCode tutorial
all 4 stories, step by step
Story 1: Make it blinkon, off, wait
The same in Python
def on_forever():
pins.digital_write_pin(DigitalPin.P1, 1)
basic.pause(500)
pins.digital_write_pin(DigitalPin.P1, 0)
basic.pause(500)
basic.forever(on_forever)
In MakeCode, switch from Blocks to Python at the top of the editor to type it yourself.
const int redLedPin = 2;
void setup() {
pinMode(redLedPin, OUTPUT);
}
void loop() {
digitalWrite(redLedPin, HIGH); // on
delay(500);
digitalWrite(redLedPin, LOW); // off
delay(500);
}
A program sets the pin, then waits. Without the pauses the LED would switch thousands of times a second,
and you would only see a dim glow.
Your turn: make it blink faster. How short can the pause be before you stop seeing the
blinking?
Story 2: Flash like a police cartwo LEDs take turns
The same in Python
def on_forever():
pins.digital_write_pin(DigitalPin.P1, 1)
pins.digital_write_pin(DigitalPin.P13, 0)
basic.pause(300)
pins.digital_write_pin(DigitalPin.P1, 0)
pins.digital_write_pin(DigitalPin.P13, 1)
basic.pause(300)
basic.forever(on_forever)
In MakeCode, switch from Blocks to Python at the top of the editor to type it yourself.
const int redLedPin = 2;
const int blueLedPin = 5;
void setup() {
pinMode(redLedPin, OUTPUT);
pinMode(blueLedPin, OUTPUT);
}
void loop() {
digitalWrite(redLedPin, HIGH);
digitalWrite(blueLedPin, LOW);
delay(300);
digitalWrite(redLedPin, LOW);
digitalWrite(blueLedPin, HIGH);
delay(300);
}
Each step sets both LEDs: one on, the other off. Then a short wait, and the other way round.
Your turn: a double flash, like real police cars: red on, off, on, off, then blue the same.
Story 3: Run a traffic lighta fixed order
The same in Python
def lights(red: number, yellow: number, green: number):
pins.digital_write_pin(DigitalPin.P1, red)
pins.digital_write_pin(DigitalPin.P2, yellow)
pins.digital_write_pin(DigitalPin.P8, green)
def on_forever():
lights(1, 0, 0)
basic.pause(3000)
lights(1, 1, 0)
basic.pause(1000)
lights(0, 0, 1)
basic.pause(3000)
lights(0, 1, 0)
basic.pause(1000)
basic.forever(on_forever)
In MakeCode, switch from Blocks to Python at the top of the editor to type it yourself.
const int redLedPin = 2;
const int yellowLedPin = 3;
const int greenLedPin = 4;
// Switch the three lights at once: true is on, false is off.
void setLights(bool red, bool yellow, bool green) {
digitalWrite(redLedPin, red);
digitalWrite(yellowLedPin, yellow);
digitalWrite(greenLedPin, green);
}
void setup() {
pinMode(redLedPin, OUTPUT);
pinMode(yellowLedPin, OUTPUT);
pinMode(greenLedPin, OUTPUT);
}
void loop() {
setLights(true, false, false); // red: stop
delay(3000);
setLights(true, true, false); // red and yellow: get ready
delay(1000);
setLights(false, false, true); // green: go
delay(3000);
setLights(false, true, false); // yellow: stop if you can
delay(1000);
}
The order in Sweden: red, red and yellow, green, yellow, and back to red. A function switches all three
lights in one line, so the main loop reads like the order itself.
Your turn: time a real traffic light near you and copy its times.
Story 4: Press to crossinput
The same in Python
def lights(red: number, yellow: number, green: number):
pins.digital_write_pin(DigitalPin.P1, red)
pins.digital_write_pin(DigitalPin.P2, yellow)
pins.digital_write_pin(DigitalPin.P8, green)
pins.set_pull(DigitalPin.P14, PinPullMode.PULL_UP)
def on_forever():
lights(0, 0, 1)
while pins.digital_read_pin(DigitalPin.P14) == 1:
basic.pause(20)
basic.pause(1000)
lights(0, 1, 0)
basic.pause(1000)
lights(1, 0, 0)
basic.pause(4000)
lights(1, 1, 0)
basic.pause(1000)
basic.forever(on_forever)
In MakeCode, switch from Blocks to Python at the top of the editor to type it yourself.
const int redLedPin = 2;
const int yellowLedPin = 3;
const int greenLedPin = 4;
const int buttonPin = 7;
void setLights(bool red, bool yellow, bool green) {
digitalWrite(redLedPin, red);
digitalWrite(yellowLedPin, yellow);
digitalWrite(greenLedPin, green);
}
void setup() {
pinMode(redLedPin, OUTPUT);
pinMode(yellowLedPin, OUTPUT);
pinMode(greenLedPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP); // HIGH when not pressed, LOW when pressed: no extra resistor needed
}
void loop() {
setLights(false, false, true); // green for the cars
while (digitalRead(buttonPin) == HIGH) {
delay(20); // wait for the button
}
delay(1000); // the cars get a moment
setLights(false, true, false); // yellow
delay(1000);
setLights(true, false, false); // red: pedestrians cross
delay(4000);
setLights(true, true, false); // red and yellow
delay(1000);
}
The cars keep green until someone presses the button. Pull up makes the pin read 1 while the button is not
pressed; pressing it connects the pin to GND, and it reads 0. Keep the story 3 function.
Your turn: make the pedestrians wait at least 10 seconds between two red phases.
Really stuck? Try first: you learn the most by building it yourself. If it still does not work, your
teacher can give you a code for a ready-made program of your story.
Type the code in the lock at the top right of the page.