mardi 1 mars 2016

Python: why doesn't if(a == 0): a = 1 if(a == 1): a = 0 work for making a toggle

I'm working on a project using python to read the digital inputs on the raspberry pi. I wanted to turn one of the buttons into a toggle, as in it switches a value between 1 and 0 whenever I press it. Everything is working fine except the section:

if(a == 0.0):
    a = 1.0
if(a == 1.0):
    a = 0.0

It seems like this should work with the rest of the code to make the value toggle between 1 and 0 whenever the button is pressed, but a prints as 0.0 every time, does anyone know why this is?

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)
GPIO.setup(24, GPIO.IN, pull_up_down = GPIO.PUD_UP)
a = 0.0
b = 0.0
c = 0

while True:
    if(GPIO.input(4) ==1 and c ==0):
        print 'Button 1 Pressed'
        if(a == 0.0):
            a = 1.0
        if(a == 1.0):
            a = 0.0
        c = 1
        print a
    if(GPIO.input(4) !=1):
        c = 0
    if(GPIO.input(24) ==0):
        print 'Button 2 Pressed'

Aucun commentaire:

Enregistrer un commentaire