main:
if portA.0 = 1 then ‘If the button is pressed then...
pause 10 ‘Let button settle to prevent false readings.
if buttonState = 0
countVar = countVar + 1 ‘Increment countVar by 1.
if countVar > 2 then ‘When it goes above 2 return to 0.
countVar = 0
endif
buttonState = 1 ‘Button is not ready.
endif
else
buttonState = 0
endif
if countVar > 2 then
countVar = 0
endif
gosub displayNum ‘Call subroutine to display the count.
goto main
displayNum:
branch countVar,[display1, display2, display3]
return

Project 2: Potentiometer with LCD Readout

LCD modules are a simple way to display alpha-numeric characters. Unlike with the seven-segment LED, you don’t have to program each bit of each character to create readable text. The PicBASIC Pro compiler offers the handy BASIC command LCDOUT, which takes a text string and pushes it onto a compatible display. To use it in your code, you simply specify which pins are connected to the LCD module, and then pass strings to LCDOUT to display them.

Full documentation on using LCDOUT, including LCD module wiring, is in the PICBasic Pro Compiler Manual on microEngineering Labs’ website ( melabs.com/resources/pbpmanual/).

Following the pin-out diagram of the LCD, we wire it to available pins on the PIC. We connect the LCD’s power and ground, and ground the contacts that are for features we aren’t using.

Now we can program the micro to test the LCD by showing the traditional “Hello World” greeting. After declarations at the top, our main loop contains just two lines. (See full code online.)

 

main:

LCDOUT $fe, 1 , “Hello world.”

pause 1000 goto main

Unlike a button, a potentiometer is an analog input device. Turning its knob changes its electrical resistance. Many light sensors, proximity sensors, flex sensors, and other types of input devices also work as variable resistors. They’re all wired into a circuit and handled programmatically in the same basic way, so learning how to read these with a microprocessor opens up countless possibilities for inputs and interactivity.

We’ll program our circuit to read the value of the potentiometer and display a corresponding number, between 0 and 255, on the LCD. We wire one pole of the potentiometer to power (5V), the other pole to ground (0V), and the variable “wiper” to pin 11. In the code’s declarations, we set pin 11 to be an input pin and enable it for analog digital conversion (ADC). This means the chip will read variable voltage in the 0–5V range as a number between 0 and 255. Turn the potentiometer knob, and you change the number displayed.

In practice, the voltages read by the microprocessor are noisy, and immediately pushing raw readings to the display makes the numbers jumpy and unreadable. Our code solves this problem by tracking the previous display reading and updating it only if there’s a significant change to the input, with the threshold set at 7.

 

adcVar var byte ‘Analog reading value. prevadcVar var byte ‘Previous reading. main: adcin 2, adcVar ‘Read the ADC value of analog pin 2. if (ABS (adcVar - prevadcVar)) > 7 then ‘If significant change...

LCDout $fe, 1, “adcVar =” ‘Clear screen and print “adcVar =”. LCDout $fe, $C0, #adcVar ‘Print value itself on 2nd line.

prevadcVar = adcVar endif goto main

Now you can use all sorts of input devices and displays! Enjoy, and please join us for part 3, in which we’ll be making a digital alarm clock.

Materials list, supplementary code, additional diagrams, and suggested reading online at makezine.com/07/diycircuits_hello.

 

Sparkle Labs ( sparklelabs.com) is a product development firm in New York City. They build “hi-tech, hi-touch” environments and products, using new technologies.

References:

http://www.lbl.gov

Archives