Programming and note
Digital Read:
Just like the way
digital write
block turns a pin on (1) or off (0), the
digital read
block check at the state of a pin, which is either HIGH (1) or LOW (0). In this case, we can detect if
the switch is pressed or not by using this block.
If/else:
The
if/else
block is a logical structure. If the logical statement that is attached to it
(switchState=1) is true, then it will execute the code blocks inside of the if. If that statement is
false, it will execute the
else
blocks. In this case, if the statement is true(the value is digital read
from P0 port equal to 1), then turn on the LED on pin 1; else, turn off the LED on P1.
Download the program to micro:bit to see what happens.
Do you like music? Have you ever dreamed of becoming a singer? The power of music is really
great, it can make people happy physically and mentally. In this experiment, we will make the
micro:bit to be a “singer”, let’s see how to do it!
Experiment 3 – Micro:bit singer
Instruction
When you press the switch, the LED lights up, and when you press the switch
a second time, the LED turns off.
Result
Do you have this experience? whenever you go up the stairs at night, the lights will
light up in front of you. Can we make such a lamp?
11