1

I have a Raspberry Pi set up with an Analogue to Digital converter and have a two potentiometer joystick wired to the input.

I can get the values of each potentiometer to display on screen using 'Print' however it displays thousands of lines per minute as the values change when the potentiometers move so it is impossible to read the values as they go flying past.

I was wondering if there is another way to display these values so that it is just one area of the screen and the values change there making them easy to read rather than printing a new line each time?

  • Try to use the `\r` carriage return character. It moves the printing cursor to the beginning of the line so you can print on top of the previous print. This might not work since I don't know the internals of Raspberry Pi and how it prints – eli Jun 18 '16 at 13:37
  • 1
    You'll need to write a terminal interface - in Python I recommend https://github.com/erikrose/blessings – bbayles Jun 18 '16 at 13:37
  • 1
    Possible duplicate of http://stackoverflow.com/questions/5419389/how-to-overwrite-the-previous-print-to-stdout-in-python – Moses Koledoye Jun 18 '16 at 13:40
  • Sometimes an instrument's code is written so it outputs the reading just a few times per second, the readings being put through a filter. One basic filter is a simple average. You could try averaging, say, 10, 100 or 1000 readings and printing the result. Even if you overwrite the same position on the display, having the least significant digit flying around is not very useful. – Weather Vane Jun 18 '16 at 15:13

1 Answers1

0

You could try something like the snippet below. Assuming MCP3008 is your converter, it will read the value every 0.1 secs (sleep(0.1)) and if the values are different by 5% (threshold = 0.05) it will print the new value and overwrite the old line (end = '\r')

from gpiozero import MCP3008
from time import sleep

threshold = 0.05
pot = MCP3008()

last_value = pot.value
print(last_value, end='\r')    
while True:
    new_value = pot.value
    if abs((last_value - new_value) / new_value) > threshold:
        print(new_value, end='\r')
        last_value = new_value
    else:
        sleep(0.1)
Maximilian Peters
  • 30,348
  • 12
  • 86
  • 99