1

I've hooked up a LCD display to a project I'm working on. I have many inputs/outputs and I want to display different messages depending on the state of the inputs. Now before I start to complicate / add more hardware. I need some assistance. When no inputs are active I want the LCD to display the message "Awaiting Input" then once input one is received I want to display "Input one active". I am currently using a while true loop but the problem occurs once I clear the LCD display as every time it loops it pulses the text on the screen. Is there a better way to do this outside of a while true loop?

Here is my current code

import I2C_LCD_driver
import time
import pigpio

mylcd = I2C_LCD_driver.lcd()

Debounce = 0.5

Input23 = 23
Input24 = 24
Input17 = 17
Output18 = 18
Output4 = 4

pi_GPIO = pigpio.pi()
pi_GPIO.set_mode(Input23, pigpio.INPUT)
pi_GPIO.set_pull_up_down(Input23 , pigpio.PUD_UP)
pi_GPIO.set_mode(Input24, pigpio.INPUT)
pi_GPIO.set_pull_up_down(Input24 , pigpio.PUD_UP)
pi_GPIO.set_mode(Input17, pigpio.INPUT)
pi_GPIO.set_pull_up_down(Input17 , pigpio.PUD_UP)
pi_GPIO.set_mode(Output18, pigpio.OUTPUT)
pi_GPIO.set_mode(Output4, pigpio.OUTPUT)

while True:

    if pi_GPIO.read(Input23):
        pi_GPIO.write(18, 0)
        mylcd.lcd_clear()
        mylcd.lcd_display_string("Input 1 Active", 1)
        time.sleep(Debounce)
    else:
        pi_GPIO.read(Input23)
        pi_GPIO.write(18, 1)
        mylcd.lcd_clear()
        mylcd.lcd_display_string("Awaiting Input", 1)
        mylcd.lcd_display_string("Detection", 2)
        time.sleep(Debounce)
Pang
  • 9,564
  • 146
  • 81
  • 122
Bradley
  • 327
  • 2
  • 11
  • Great question. you might want to consider whether the audience at https://raspberrypi.stackexchange.com/ might have more information? – JoSSte Nov 18 '18 at 17:23

1 Answers1

0

When I was doing something similar with Arduino, I used a flag. In that case, I did need to have a While True: equivalent because that's the main process of the board and had to run in an infinite loop while powered up.

The problem you are having now (I would guess) is that every time the loop hits the following code, you are creating the pulse:

mylcd.lcd_clear()
mylcd.lcd_display_string(...)

Any easy (though unelegant) fix for this is something like (this is Python 3, which works a bit differently for dictionary iterations than 2.7) this. I haven't had a chance to test this (so I've probably missed some things / made some typos), but hopefully you get the idea:

inputs = {"Input17": False,
          "Input23": False,
          "Input24": False}

while True:

    change_in_input = False

    # check for change in inputs
    for input in inputs:
        if bool(pi_GPIO.read(input)) != bool(inputs[input]):
            change_in_input = True
            # update inputs based on change(s)
            inputs[input] = pi_GPIO.read(input)

    if change_in_input:
        if not any(inputs.values()):
            pi_GPIO.write(18, 1)
            mylcd.lcd_clear()
            mylcd.lcd_display_string("Awaiting Input", 1)
            mylcd.lcd_display_string("Detection", 2)
        elif inputs["Input23"]:
            pi_GPIO.write(18, 0)
            mylcd.lcd_clear()
            mylcd.lcd_display_string("Input 1 Active", 1)

    time.sleep(Debounce)
NotAnAmbiTurner
  • 2,553
  • 2
  • 21
  • 44
  • When Input 23 is False I have a red LED showing once true it changes to Green. Thanks for the changes, but the LCD still pulses due to the mylcd.lcd_clear() been in the while true loop. – Bradley Nov 18 '18 at 17:31
  • It shouldn't still pulse - the whole point of `not_awaiting_input_displayed` is that the entire block of code is skipped when the display is already showing "Awaiting Input". Can you link the docs for the lcd display you're using? The LCD color is controlled by the `pi_GPIO.write()` statement? – NotAnAmbiTurner Nov 18 '18 at 17:37
  • Is it pulsing between "Awaiting Input" and "Detection"? Is it possible `time.sleep` turns off the LCD for a moment? – NotAnAmbiTurner Nov 18 '18 at 17:40
  • The "Awaiting Input" seems fine strange I know... Only when I activate the input "Input 1 active" Pulses – Bradley Nov 18 '18 at 17:41
  • Same problem. As long as Input23 is getting input, you're going to hit the `clear` and then the `redisplay`. There are common patterns to deal with things like this (https://arduino.stackexchange.com/questions/532/what-design-patterns-can-i-use-to-handle-user-input-and-display-updating) but without knowing more it's hard to say which would be best. It seems like you need a generic function to determine if inputs have changed since the last loop. – NotAnAmbiTurner Nov 18 '18 at 17:54