I am working with python idle 2.7.9 with adafruit lcd. I have this code but I ke
ID: 3668097 • Letter: I
Question
I am working with python idle 2.7.9 with adafruit lcd. I have this code but I keep getting this error,message line 47, in if lcd.is_pressed(buttons[0]): AttributeError: 'Adafruit_CharLCDPlate' object has no attribute 'is_pressed.
Here is the code.
import time
from time import sleep
from Adafruit_CharLCDPlate import Adafruit_CharLCDPlate
def DisplayMessage(x):
lcd.clear()
lcd.message(x)
sleep(2)
lcd = Adafruit_CharLCDPlate()
Name = 'Put Your Name Here'
Class = 'ECT109'
Assignment = 'Week 6 Lab 3'
DisplayMessage(Name)
DisplayMessage(Class)
DisplayMessage(Assignment)
#-----------------------------------------------------
buttons=( (LCD. UP, 'UP',(0,0,1)),
(LCD. DOWN, 'DOWN',(0,1,0)),
(LCD. LEFT, 'LEFT',(1,0,0)),
(LCD. RIGHT, 'RIGHT',(1,0,1)),
(LCD.SELECT, 'SELECT',(1,1,1)))
DisplayMessage('Press a button')
while True:
#when pressed UP BUTTON
if lcd.is_pressed(buttons[0]):
lcd.backlight(lcd.ON)
lcd.backlight(lcd.GREEN)
DisplayMessage('Up')
lcd.backlight(lcd.OFF)
#if DOWN BUTTON is pressed
elif lcd.is_pressed(buttons[1]):
lcd.backlight(lcd.ON)
lcd.backlight(lcd.RED)
DisplayMessage('down')
lcd.backlight(lcd.OFF)
#if LEFT BUTTON is pressed
elif lcd.is_pressed(buttons[2]):
lcd.backlight(lcd.ON)
lcd.backlight(lcd.BLUE)
DisplayMessage('left')
lcd.backlight(lcd.OFF)
#if RIGHT BUTTON is pressed
elif lcd.is_pressed(buttons[3]):
lcd.backlight(lcd.ON)
lcd.backlight(lcd.YELLOW)
DisplayMessage('right')
lcd.backlight(lcd.OFF)
#if SELECT BUTTON is pressed
else
DisplayMessage('-use to exit loop')
break
Explanation / Answer
import time
from time import sleep
from Adafruit_CharLCDPlate import Adafruit_CharLCDPlate
def DisplayMessage(x):
lcd.clear()
lcd.message(x)
sleep(2)
lcd = Adafruit_CharLCDPlate()
Name = 'Put Your Name Here'
Class = 'ECT109'
Assignment = 'Week 6 Lab 3'
DisplayMessage(Name)
DisplayMessage(Class)
DisplayMessage(Assignment)
#-----------------------------------------------------
buttons=( (LCD. UP, 'UP',(0,0,1)),
(LCD. DOWN, 'DOWN',(0,1,0)),
(LCD. LEFT, 'LEFT',(1,0,0)),
(LCD. RIGHT, 'RIGHT',(1,0,1)),
(LCD.SELECT, 'SELECT',(1,1,1)))
DisplayMessage('Press a button')
while True:
#when pressed UP BUTTON
if lcd.is_pressed(buttons[0]):
lcd.backlight(lcd.ON)
lcd.backlight(lcd.GREEN)
DisplayMessage('Up')
lcd.backlight(lcd.OFF)
#if DOWN BUTTON is pressed
elif lcd.is_pressed(buttons[1]):
lcd.backlight(lcd.ON)
lcd.backlight(lcd.RED)
DisplayMessage('down')
lcd.backlight(lcd.OFF)
#if LEFT BUTTON is pressed
elif lcd.is_pressed(buttons[2]):
lcd.backlight(lcd.ON)
lcd.backlight(lcd.BLUE)
DisplayMessage('left')
lcd.backlight(lcd.OFF)
#if RIGHT BUTTON is pressed
elif lcd.is_pressed(buttons[3]):
lcd.backlight(lcd.ON)
lcd.backlight(lcd.YELLOW)
DisplayMessage('right')
lcd.backlight(lcd.OFF)
#if SELECT BUTTON is pressed
else
DisplayMessage('-use to exit loop')
break
changes:
only the change in above code to be done is:
put busnum=1 as argument in Adafruit_CharLCDPlate()
so it becomes:
lcd= Adafruit_CharLCDPlate(busnum=1)
and then declare
lcd.clear()
lcd.message(x)
sleep(2)
Then as we have labeled the button yo can ditectly use
elif lcd.buttonPressed(lcd.UP):
once these changes are done, the code will not give the previous error
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.