Python Forum
class Update input (Gpio pin raspberry pi)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
class Update input (Gpio pin raspberry pi)
#1
Hi (Newbie here self taught first in C - Arduino and now try to learn Python but with my mind thinking like programing C and want to change that to pythonic way of thinking)

i want to print on screen my gpio input .
with simple python code with "def" only i can do that easy

But i want to start using classes and so i tried the code below but i can print the Gpio state of what it is when the loop starts. i am not getting any changes
if the loop starts and the button is not pressed then i get always "0" even if i press the button
if the loop starts and have the button pressed then i am getting always "1" even if i stop pressing the button

What i am doing wrong?
thanks in advance

My code :

import RPi.GPIO as GPIO
import time
GPIO.cleanup()
Button_1 = 23  # incoming signal from a button
# OutPut = 21  # Led for output
################################################################################################
###############################  SETUP GPIO METHOD  ############################################
################################################################################################
# GPIO.setmode(GPIO.BOARD) # # NUMBER OF PINS BASED ON BOARD NUMBERING
GPIO.setmode(GPIO.BCM)  # NUMBER OF PINS BASED ON IC
#############################  SETUP GPIO PINS INPUT  ##########################################
GPIO.setup(Button_1, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
#GPIO.setup(Button_1, GPIO.IN, pull_up_down=GPIO.PUD_UP)
############################  SETUP GPIO PINS OUTPUT  ##########################################
# GPIO.setup(OutPut, GPIO.OUT)
# GPIO.output(OutPut, GPIO.LOW)
################################################################################################
class Status:
    def __init__(self):
        self.button1 = GPIO.input(Button_1)
    def getStatus(self):
        return self.button1

def main():
    while True:
        print(status.getStatus())

print("Starting....")
time.sleep(2.0)
status = Status()
main()
Reply
#2
Classes aren't "Pythonic", they are object oriented. You can do object oriented programming in Python, C++ and even C.

Your program is doing this:
status = GPIO.input("Button_1")
while True:
    print(status)
I would write your code more like this:
import RPi.GPIO as GPIO
import time

class Button:
    """A GPIO input"""
    def __init__(self, pin):
        self.pin = pin
        GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

    def status(self):
        """Return state of input"""
        return GPIO.input(self.pin)


def main():
    print("Starting....")
    GPIO.cleanup()
    GPIO.setmode(GPIO.BCM)
    button1 = Button(23)
    time.sleep(2.0)
    while True:
        time.sleep(2.0)
        print(button1.status())
 
if __name__ == "__main__":
    main()
Larz60+ likes this post
Reply
#3
Thanks for the HELP !! Heart
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  function return boolean based on GPIO pin reading caslor 2 1,131 Feb-04-2023, 12:30 PM
Last Post: caslor
  Webhook, post_data, GPIO partial changes DigitalID 2 955 Nov-10-2022, 09:50 PM
Last Post: deanhystad
  NotImplementedError: pseudo-class is not implemented - how to Update Python to solve apollo 1 3,027 May-16-2021, 08:03 AM
Last Post: buran
  Raspberry pi Sensehat auto update mrbronz61 4 2,808 Apr-10-2021, 09:13 AM
Last Post: mrbronz61
  Seemingly unstable GPIO output while executing from RetroPie LouF 6 3,862 Feb-19-2021, 06:29 AM
Last Post: LouF
Question Python + Google Sheet | Best way to update specific cells in a single Update()? Vokofe 1 2,628 Dec-16-2020, 05:26 AM
Last Post: Vokofe
  Picture changing triggered by GPIO q_nerk 2 2,522 Dec-14-2020, 03:32 PM
Last Post: DeaD_EyE
  Grabing email from a class with input from name Nickd12 2 1,780 Oct-13-2020, 06:22 PM
Last Post: Nickd12
  GPIO high if network IP has good ping duckredbeard 3 2,282 Oct-12-2020, 10:41 PM
Last Post: bowlofred
  raspberry pi tank gpio help jatgm1 1 2,371 May-06-2020, 09:00 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020