The flickering leds are all very well, but I've decided that to get a better feel for what is going on a bar graph would be nice. This works like a level meter on audio products. Because I wanted to use this function in other things I came up with the function on the right.

The function display_bar accepts an integer value and displays a bar graph depending on a number of threshold values which are defined as constants. It decides what to display on the basis of a large number of conditional statements.

This works very well, and runs quickly, but you might think that there is a neater way of doing the job, perhaps with an array of threshold values. This is true, and such an approach might work well, but in the PIC micocontroller this solution is probably more appropriate. This is because the PIC microcontroller we are using has a large amount of space for programs but is quite restricted in terms of the space for variables.

This means that my solution on the right, which is "long on code" is arguably a good idea, since it uses hardly any memory for variable space. If I was to use an array I'd probably need to use variable space to hold the lookup table, and this might restrict the size of program I can write.

Exercise 10.2

If you open this program you will note that I've tidied up the code so that the ADC is read within a function. You can use this function anywhere you want to read the ADC. It returns an integer which is the current value on the port. Note that it will take a little while to return, and that it returns ten bit value, i.e. the number will be greater than will fit into an unsigned char type.