TB2 003: POTS

Part 1

There are five potentiometers (pots) on the TB2, but we’ll just use the leftmost one for now.

The Arduino Due that powers the TB2 has 12 analog input pins. They are capable of returning a 12bit reading (4096 values), but for compatibility with other Arduino boards they are set to a resolution of 10bits (1024 values in a range from 0 – 1023).

We want to display a reading on the LCD display, so we’ll include code for the LCD, too. We start by including the LCD library.

The leftmost pot is attached to analog pin 0, (also A0). We’ll call the pin A0 pot1.

Next, we’ll initialize the LCD.

In the setup() function, we’ll reuse some code from the first tutorial to start communication with the LCD and to write Groovesizer on the first line.

In the loop() function, we get a value from the analog pin A0 (now called pot1) with the analogRead() function. We store the value in a variable called reading.

The we set the LCD to write  to the leftmost position in the second row with setCursor().

Finally, we write the value we read from pot1 and stored in the reading variable and close the loop with }.

Here’s the code in full up to this point.

Try out the code by uploading it to the TB2, and moving the leftmost pot. The current pot1 value is written to the  LCD each time through the loop, but there’s a problem that we’ll address next.

Part 2

When we wrote the pot1 value to the LCD with the last section of code, we didn’t clear the LCD between writes, so we got some garbage characters left over from previous reads. It makes for some confusing and unexpected values.

Let’s try a quick (and unsatisfactory!) fix now. Before we write the latest value of reading, we’ll first clear the four LCD characters where reading will be written. We set the cursor position and print 4 spaces before setting the cursor to the same position and writing the current value of the reading variable.

Here’s the code in full.

It’s a step forward in that we can now see the real value of reading without the leftover characters. But it’s also a step backward, because now the value of reading appears to be only half as bright and to be flickering. We’ll address this problem next.

Part 3

The cause of the reduction in brightness and the flickering is that we are now writing a blank character before writing the actual character for the value of reading each time through the loop. What we really want to do is to only clear characters where none should be written, and to update characters that should be written without clearing them first.

That last explanation makes the problem sound harder than it is, but we will have to write our own function to offer an elegant solution to it.

Just like we created the compulsory setup() and loop() functions before, we are now going to write our own showValue() function. The idea is that we can then simply call the showValue() function and pass it the value we want to show, and it will take care of clearing unused characters by itself.

In empty form, our function will look like this.

Like setup() and loop(), the function is preceded by void, meaning that the function will only do something and not return a value. You’ll see that we now have int val in the parentheses, because we want to pass our function an integer value, and we’re going to call that value val in the function.

Inside the function, we’re going to use a if/else-if/else structure to check for the value of val, and to write spaces in front of the value of val depending on its size. Note that we don’t have to set the cursor position; we’ll do it once before we call the showValue() function, and the print(reading) function will automatically write to the next cursor position after we’ve printed the spaces .

Here’s the complete function.

Now we can simply call the showValue() function from within the loop() function.

Here’s the code in full.

Much better! You can probably still see some flickering on the final character now and again, however. This is because it’s possible that your pot ends up between two values. In the TB2 code, I minimize this flickering by only updating the display if a value has changed, and by locking the pots after a short interval.

Part 4

Finally, let’s read and do something with all 5 the pots. We’ll store our readings for the 5 pots in an array.

We’ll also have an array to store the last pot value we used so that we only update a value if it has changed.

We’ll create a function called getPots() to update all 5 pots in one go each time through the loop. We can use a for-loop to iterate over the pots, because they’re attached to consecutive analog pins from A0 -A 4.

We’ll print the values of pot 1 to pot 4 (in the code their values correspond to pot[0] to pot[3] because arrays are zero-indexed), but we’ll reduce the range from 1024 values to 256 values by bitshifting two places to the right. We’ll use pot 5 (pot[4]) to adjust the brightness off the LED, again within a range of 0 – 255.

Here’s all the code.


Leave a Reply

Your email address will not be published. Required fields are marked *