Problème programme python

Fermé
piguin974 Messages postés 4 Date d'inscription vendredi 6 avril 2018 Statut Membre Dernière intervention 8 avril 2018 - Modifié le 6 avril 2018 à 21:27
piguin974 Messages postés 4 Date d'inscription vendredi 6 avril 2018 Statut Membre Dernière intervention 8 avril 2018 - 8 avril 2018 à 12:07
Bonjour,
Je débute en langue python j'obtiens une SyntaxError j'aimerais obtenir de l'aide pour pouvoir la résoudre merci de votre compréhension.
SyntaxError : if motion==0 or motion==1:


## License

The MIT License (MIT)

GrovePi for the Raspberry Pi: an open source platform for connecting Grove Sensors to the Raspberry Pi.
Copyright (C) 2017 Dexter Industries

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
'''

import time
import grovepi
from grovepi import *

# Connect the Grove Light Sensor to analog port A0
# SIG,NC,VCC,GND
light_sensor = 0

# Connect the LED to digital port D4
# SIG,NC,VCC,GND
led = 4

# Turn on LED once sensor exceeds threshold resistance
threshold = 10
# Connect the Motion Sensor to digital port D3
pir_sensor = 8
motion=0


grovepi.pinMode(pir_sensor,"INPUT")
grovepi.pinMode(light_sensor,"INPUT")
grovepi.pinMode(led,"OUTPUT")

while True:
    try:
        # Get sensor value
        sensor_value = grovepi.analogRead(light_sensor)
        # Calculate resistance of sensor in K
        resistance = (float)(1024 - sensor_value) * 10 / sensor_value
        if resistance > threshold: 
            motion=grovepi.digitalRead(pir_sensor) 
  if motion==0 or motion==1: # check if reads were 0 or 1 it can be 255 also because of IO Errors so remove those values
   if motion==1:
            # Send HIGH to [/contents/604-switch-commutateur switch] on LED
            grovepi.digitalWrite(led,1)
        else:
            # Send LOW to switch off LED
            grovepi.digitalWrite(led,0)
        print("sensor_value = %d resistance = %.2f" %(sensor_value,  resistance))
        time.sleep(.5)

    except IOError:
        print ("Error")





2 réponses

Bonjour.

C'est simplement un problème d'indentation.

try:
    # Bloc de code
except:
    # Bloc de code
1
piguin974 Messages postés 4 Date d'inscription vendredi 6 avril 2018 Statut Membre Dernière intervention 8 avril 2018
7 avril 2018 à 20:01
Bonjour merci pour la réponse.
j'ai trouvé le problème mais maintenant j'ai un autre problème ><'
Ma LED ne s'éteint pas je précise qu'avec d'autre programmes la LED s'éteint :
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

import time
import grovepi
from grovepi import *

# Connect the Grove Light Sensor to analog port A0
# SIG,NC,VCC,GND
light_sensor = 0

# Connect the LED to digital port D4
# SIG,NC,VCC,GND
led = 4

# Turn on LED once sensor exceeds threshold resistance
threshold = 10
# Connect the Motion Sensor to digital port D3
pir_sensor = 8
motion=0


grovepi.pinMode(pir_sensor,"INPUT")
grovepi.pinMode(light_sensor,"INPUT")
grovepi.pinMode(led,"OUTPUT")

while True:
try:
# Get sensor ValueError
sensor_value = grovepi.analogRead(light_sensor)
# Calculate resistance of sensor in K
resistance = (float)(1024 - sensor_value) * 10 / sensor_value
if resistance > threshold:
motion=grovepi.digitalRead(pir_sensor)
if motion==0 or motion==1: # check if reads were 0 or 1 it can be 255 also because of IO Errors so remove those values
if motion==1:
print("sensor_value = %d resistance = %.2f" %(sensor_value, resistance))
time.sleep(.5)
grovepi.digitalWrite(led,1)
else:
grovepi.digitalWrite(led,0)
print ('-')

except IOError:
print ("Error")
0
critou > piguin974 Messages postés 4 Date d'inscription vendredi 6 avril 2018 Statut Membre Dernière intervention 8 avril 2018
7 avril 2018 à 20:36
Bonjour.

Je ne connais pas ce module.

As-tu testé avec un exemple plus simple pour déjà voir si grovepi arrive bien à éteindre ta led ?
Ici par exemple un script simple qui permet de faire clignoter ta led.
https://github.com/DexterInd/GrovePi/blob/master/Software/Python/grove_led_blink.py

Si ça fonctionne, tu pourrais t'en inspirer.
0
piguin974 Messages postés 4 Date d'inscription vendredi 6 avril 2018 Statut Membre Dernière intervention 8 avril 2018
7 avril 2018 à 22:03
Bonsoir
Oui j'ai testé la grovepi j'arrive bien à éteindre ma LED et j'arrive à la faire clignoter sans problème.
Le problème et que mon programme python s'arrête à [ else: ] pour une raison que j'ignore...
Car il n'affiche pas print ('-') dans le terminal.
0
piguin974 Messages postés 4 Date d'inscription vendredi 6 avril 2018 Statut Membre Dernière intervention 8 avril 2018
8 avril 2018 à 12:07
J'ai trouvé le problème merci de ton aide :)
0
yg_be Messages postés 22719 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 23 avril 2024 1 476
6 avril 2018 à 21:54
bonsoir, peut-être:
if (motion==0) or (motion==1) :
0