Reaction timer

To learn how to use the Infrared(IR) receiver and IR transmitter.

To learn use the OLED.


You'll need

1x Micro:bit
1x Breakout Board
1x IR Receiver
1x IR transmitter
1x OLED

Step 1

Instructions

Put the micro:bit into the Breakout Board and put the OLED onto any of the 4 holes seen at the bottom of the Breakout Board.




Materials

1x Micro:bit
1x OLED
1x Breakout Board


Step 2

Instructions

Pin the wires of the IR Receiver onto Pin 0(P0). Do ensure that it has been properly kept, or you will face problems later on.




Materials

1x IR receiver
1x Breakout Board


Step 3

Instructions

Good job! You finished setting it up! Before we get into actual coding... you have to download an extension. Go to the micro:bit website and create a new project. Scroll down and press ‘Advanced’ and look for ‘Extensions’. Click on it and search ‘tinker’. Download the extension showed in the picture.




Materials


Step 4

Instructions

Now let’s start coding! Ignore the first 2 lines. Make 3 new variables named ‘avg’, ‘time1’ and ‘count’. Initialise them by setting them to 0. Go to ‘Pins’, look for the block ‘set pull pin P0 to up’ and place it below the variables. This is to tell the micro:bit that there is an IR Receiver. Lastly, initialise the OLED and show the instructions of how this device works.




Materials


Step 5

Instructions

In the same ‘on start’ block, write this code. This serves as a simple countdown before the game begins.




Materials


Step 6

Instructions

In a new ‘forever’ block, write these code down. Make a new variable named ‘ir’. This will read the readings by IR Receiver. The pauses are to ensure the symbol(in this case, exclamation mark) appears randomly. The loop(green block) is to start counting the time until signal has been received by the IR Transmitter(Remote).




Materials


Step 7

Instructions

Then, switch over to JavaScript and write these few lines under ‘basic.forever(function()’. Although you can add an ‘if’ block and write this code using blocks, the 4th line of code requires JavaScript as we want to show the time in floating-Point numbers(decimals), this we have to convert the ‘time1’ variable from integer to string(this is known as typecasting). This would not be possible using the blocks, thus JavaScript is used.




Materials


Step 8

Instructions

In JavaScript, add in these lines of code after ‘let ir=0’. Once again, these few lines of code could be done by using the blocks but the 3rd line of code requires you to change ‘avg’ from integer to string, thus you need JavaScript. You can switch over to the blocks to understand the code you have written.




Materials


Step 9

Instructions

And you are done! You can now see how fast you are. To see the average on the OLED screen, you can press A and B. Remember to press the IR transmitter directly in front of the IR receiver. Enjoy!




Materials


Code and References

Completed this tutorial?

Spread the word! Let's get this trending on social media with #letsgethacking #Reactiontimer