MakeCode Input & Output
Input & Output
A program usually always works with different data. Here we distinguish between the so called input and the output.
Input is always information, which is fed to the program or device, in this case our Joy-Car. This can be for example the recognition of an obstacle or a simple push of a button. The information (input - input) is fed to the device and can be processed accordingly by the program.
Output is information that is output by the device. The information (output) is a reaction of the device and can be for example the activation of a headlight or the simple output of a temperature information.
In the following example we wait for the input by pressing the button A. As soon as an input is recognized, we output the temperature of our micro:bit.
Of course it is not necessary to use a temperature as output. Outputs can also be light signals, calculation information or even, as in the next example, compass data. Here we expect again the keypress of button A to output the compass orientation.
The output of data, as in this case the compass information, does not necessarily have to be output on the small LED matrix of the micro:bit. Often it is much more convenient to display the data sets via the console output of the programming environment for a quick insight into the data. With the following example we output our compass orientation directly this way.
A direct output using "Output serial number(...)" can quickly become confusing, however, because the records are simply appended one after the other. The use of "Output serial line(...)" is more clearly arranged here, as in the next example. Here a new line is added for each output. Additionally, we slow down the output by adding a "pause (ms) ..." statement.
The information can also be processed first before being output. In the next example we first collect a total of 10 times the compass orientation in a For loop and add this value to our median variable. Then we form the total average by dividing our median variable by 10. Only after that, i.e. after we have completed our processing, is our output.
The size and complexity of the processing is irrelevant. While our average calculation from the previous example was very small, in the next example we extend our calculation by calculating the average of 50 measurements in total. To do this, we increase the runs of our For loop to 50 and also add our division for the average calculation.