Spirit Level, Thermometer, Countdown Timer:
skeleton functions will be provided to you to complete the code within them. 3.1 Spirit level Firstly, you will need to write a function: int average(int *array, int nLen);
which returns the average value of an array of integer values. (To prevent overflow, it is suggested to use a long internal variable).
The Spirit Level function will run a loop. For each iteration, the Z-component of gravitational acceleration will be sampled four times at 50ms intervals and stored in a suitably sized array.
Using the average()function you have written, determine the average value of the 4 Zvalue samples and analyze it. The green LED should be illuminated if the board is flat. The yellow LED should be illuminated if the board is at a slight angle. The red LED should be illuminated if the board is at a steep angle. The loop can be exited by pressing and holding S2.
int average(int *array, int nLen) {
int total = 0;
for (int i = 0; i < nLen; i++) total += array[i];
return total / nLen;
}
Comments
Leave a comment