Étape 7: Programmation PT. 1
Dans ce projet, nous allons utiliser fichier broches 2 et 3 pour commander le moteur tout d’abord, nous allons créer un python pour le contrôle de la goupille.
Naviguez jusqu’au répertoire apache, qui est dans/var/www/html/en tapant la commande suivante dans la fenêtre de la console/terminal :
cd /var/www/html
et créez un nouveau fichier appelé « lights.py »
sudo nano lights.py
puis tapez le script suivant :
<p>#!/usr/bin/env python<br>#lightsOn.py import RPi.GPIO as GPIO #Import Raspberry Pi's GPIO Functions import sys #Import sys for getting shell arguments import time #Import time for delays</p><p>#Checks if the user wants to turn the lights on or off #And then sets the pins accordingly if(int(sys.argv[1]) == 1): pin = 3 pin2 = 2 else: pin = 2 pin2 = 3</p><p>#Setup the pins GPIO.setwarnings(False); GPIO.setmode(GPIO.BCM) GPIO.setup(2, GPIO.OUT) GPIO.setup(3, GPIO.OUT)</p><p>#Turns the motor backwards slightly, then forwards. #This is done so that the motor will get enough momentum to flick the switch. GPIO.output(pin2, True)</p><p>GPIO.output(pin2, False) time.sleep(0.18) GPIO.output(pin2, True)</p><p>GPIO.output(pin, False) time.sleep(0.5) GPIO.output(pin, True)</p>
Notez que les Commentaires (ceux commençant par un #) n’est pas nécessaire, et vous n’avez pas besoin de les taper, à l’exception de celle au premier plan.
Puis, appuyez sur Ctrl+O , puis entrée pour enregistrer le fichier et Ctrl -X pour quitter l’éditeur.