issue105:python
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
issue105:python [2016/01/31 14:58] – andre_domenech | issue105:python [2016/02/12 11:33] (Version actuelle) – auntiee | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | Welcome back to our Real World programming series. Last time, we programmed the RPi to turn on and off an LED when a button was pressed. Very simple, but this got us started. This month, we will do another simple project, a traffic light simulator using 3 LEDs, one Red, one Yellow and one Green. For the most part, the code is very similar to what we used last month, so you shouldn’t have any problems. If you have any questions, I suggest you look at last month’s article which should answer any of your concerns. | + | **Welcome back to our Real World programming series. Last time, we programmed the RPi to turn on and off an LED when a button was pressed. Very simple, but this got us started. This month, we will do another simple project, a traffic light simulator using 3 LEDs, one Red, one Yellow and one Green. For the most part, the code is very similar to what we used last month, so you shouldn’t have any problems. If you have any questions, I suggest you look at last month’s article which should answer any of your concerns. |
First, let’s look at the schematic and the breadboard (below right). | First, let’s look at the schematic and the breadboard (below right). | ||
Ligne 7: | Ligne 7: | ||
You should also know that the pins being used should work on a RPi v1a/b, RPi v1b+ and RPi v2b. | You should also know that the pins being used should work on a RPi v1a/b, RPi v1b+ and RPi v2b. | ||
- | The red LED cathode is connected to GPIO 17 (Physical pin 11), yellow LED cathode is connected to GPIO 23 (Physical pin 16), and the green LED cathode is connected to GPIO 22 (Physical pin 15). The Anodes of all three LEDs are connected to one side of 220 Ohm resistors and the other sides are connected to a common 3.3 VDC. We don’t need the ground voltage for this particular project. | + | The red LED cathode is connected to GPIO 17 (Physical pin 11), yellow LED cathode is connected to GPIO 23 (Physical pin 16), and the green LED cathode is connected to GPIO 22 (Physical pin 15). The Anodes of all three LEDs are connected to one side of 220 Ohm resistors and the other sides are connected to a common 3.3 VDC. We don’t need the ground voltage for this particular project.** |
- | Since I’ve driven only in the U.S. I’ve based the simulation on our traffic patterns. Long red light (10 seconds), green light is usually shorter than the red light time (8 seconds), and the yellow light is fairly short (2 seconds). These values are currently hard coded in the time.sleep() function calls. Feel free to change them as you see fit. | + | Heureux de vous revoir dans notre série sur la programmation dans le monde réel. La dernière fois, nous avons programmé le RPi pour allumer et éteindre une LED quand on a appuyé sur un bouton. Très simple, mais c'est un bon démarrage. Ce mois-ci, nous allons réaliser un autre projet simple, un simulateur de feux tricolores routiers utilisant 3 LED, une rouge, une jaune et une verte. Majoritairement, |
+ | |||
+ | D' | ||
+ | |||
+ | Notez que les couleurs des câbles correspondent à leur fonction, à l' | ||
+ | |||
+ | La cathode de la LED rouge est connecté à GPIO 17 (picot physique 11), la cathode de la LED jaune est connectée à GPIO 23 (picot physique 16) et la cathode de la LED verte est reliée à GPIO 22 (picot physique 15). Les anodes des trois LED sont connectées à un bout des résistances de 220 ohms et les autres terminaisons sont reliées au point haut de 3,3 volts. Nous n' | ||
+ | |||
+ | **Since I’ve driven only in the U.S. I’ve based the simulation on our traffic patterns. Long red light (10 seconds), green light is usually shorter than the red light time (8 seconds), and the yellow light is fairly short (2 seconds). These values are currently hard coded in the time.sleep() function calls. Feel free to change them as you see fit. | ||
Now let’s start working through the code. | Now let’s start working through the code. | ||
Ligne 31: | Ligne 39: | ||
GreenLedPin = 22 | GreenLedPin = 22 | ||
- | The first 9 lines are our standard import statements and a few comment lines. The next three lines define the BCM pin numbers for our LED pins. If you wish to use physical pin numbers, be sure to change the GPIO.setmode() line in the next routine (top right). | + | The first 9 lines are our standard import statements and a few comment lines. The next three lines define the BCM pin numbers for our LED pins. If you wish to use physical pin numbers, be sure to change the GPIO.setmode() line in the next routine (top right).** |
- | As I mentioned above, the GPIO.setmode needs to be changed from ‘GPIO.BCM’ to ‘GPIO.BOARD’ if you want to use the physical pin numbers instead of the BCM numbers in our definitions. The next three lines set the LED pins as output pins, and then turn all three LEDs off to start the program by setting the output value to HIGH. | + | Comme je n'ai conduit qu'aux États-Unis, |
+ | |||
+ | Maintenant, commençons à travailler sur le code. | ||
+ | |||
+ | # | ||
+ | |||
+ | # Traffic Light Simulator | ||
+ | # Written by G. D. Walters | ||
+ | |||
+ | # | ||
+ | |||
+ | import RPi.GPIO as GPIO | ||
+ | import os | ||
+ | import time | ||
+ | import datetime | ||
+ | |||
+ | # | ||
+ | |||
+ | RedLedPin = 17 | ||
+ | YellowLedPin = 23 | ||
+ | GreenLedPin = 22 | ||
+ | |||
+ | Les 9 premières lignes sont des déclarations classiques d' | ||
+ | |||
+ | **As I mentioned above, the GPIO.setmode needs to be changed from ‘GPIO.BCM’ to ‘GPIO.BOARD’ if you want to use the physical pin numbers instead of the BCM numbers in our definitions. The next three lines set the LED pins as output pins, and then turn all three LEDs off to start the program by setting the output value to HIGH. | ||
def LEDLoop(): | def LEDLoop(): | ||
Ligne 57: | Ligne 89: | ||
• Sleep for a designated period, | • Sleep for a designated period, | ||
• Set the output value of the pin back to 1 or high, | • Set the output value of the pin back to 1 or high, | ||
- | • Then print that the LED is now off. | + | • Then print that the LED is now off.** |
- | This is then duplicated for the Yellow and Red LEDs. The loop() routine simply forces the LEDLoop() routine to be called over and over until the user hits < | + | Comme indiqué dans l' |
+ | |||
+ | def LEDLoop(): | ||
+ | print "Green On..." | ||
+ | GPIO.output(GreenLedPin, | ||
+ | time.sleep(8) | ||
+ | GPIO.output(GreenLedPin, | ||
+ | print "Green Off..." | ||
+ | print " | ||
+ | GPIO.output(YellowLedPin, | ||
+ | time.sleep(2) | ||
+ | GPIO.output(YellowLedPin, | ||
+ | print " | ||
+ | print" | ||
+ | GPIO.output(RedLedPin, | ||
+ | time.sleep(10) | ||
+ | GPIO.output(RedLedPin, | ||
+ | print "Red Off..." | ||
+ | |||
+ | La routine LEDLoop est très simple : | ||
+ | • imprimer « < | ||
+ | • allumer la LED en mettant la valeur de sortie à 0 (niveau bas), | ||
+ | • puis une période d' | ||
+ | • remettre la sortie à la valeur 1 (niveau haut), | ||
+ | • puis imprimer que la LED est éteinte. | ||
+ | |||
+ | **This is then duplicated for the Yellow and Red LEDs. The loop() routine simply forces the LEDLoop() routine to be called over and over until the user hits < | ||
def loop(): | def loop(): | ||
Ligne 71: | Ligne 129: | ||
If you want, you could duplicate the 3 LEDS and make an intersection simulation before next time. | If you want, you could duplicate the 3 LEDS and make an intersection simulation before next time. | ||
- | Next time, we’ll have something that is a bit more challenging. Until then, happy programming. | + | Next time, we’ll have something that is a bit more challenging. Until then, happy programming.** |
+ | Puis, ceci est copié pour les LED jaune et rouge. La routine loop() force simplement l' | ||
- | ENCART APP | + | def loop(): |
+ | while True: | ||
+ | LEDLoop() | ||
+ | La routine destroy() et la boucle principale sont les mêmes que le mois dernier : nous mettons toutes les sorties de LED au point haut, pour les éteindre, puis nous appelons GPIO.cleanup(). | ||
- | The Official Full Circle App fot Ubuntu Touch | + | Je ne suis pas sûr que nous puissions construire un programme plus simple pour faire ce que nous devons faire. |
+ | |||
+ | Si vous voulez, vous pouvez dupliquer les 3 LED et programmer une simulation de carrefour avant la prochaine fois. | ||
+ | |||
+ | La prochaine fois, nous aurons quelque chose d'un peu plus corsé. Jusque-là, bonne programmation. | ||
+ | |||
+ | **ENCART APP | ||
+ | |||
+ | |||
+ | The Official Full Circle App for Ubuntu Touch | ||
Brian Douglass has created a fantastic app for Ubuntu Touch devices that will allow you to view current issues, and back issues, and to download and view them on your Ubuntu Touch phone/ | Brian Douglass has created a fantastic app for Ubuntu Touch devices that will allow you to view current issues, and back issues, and to download and view them on your Ubuntu Touch phone/ | ||
Ligne 86: | Ligne 157: | ||
https:// | https:// | ||
- | fullcircle.bhdouglass | + | fullcircle.bhdouglass** |
+ | |||
+ | ENCART APP | ||
+ | |||
+ | L' | ||
+ | |||
+ | Brian Douglass a créé une appli fantastique pour les appareils Ubuntu Touch, qui vous permettra de voir les numéros actuels et les numéros plus anciens, et de les télécharger et de les lire sur votre téléphone/ | ||
+ | |||
+ | Installation | ||
+ | |||
+ | Soit vous cherchez « full circle » dans le magasin Ubuntu Touch et vous cliquez sur Installer, soit vous affichez l'URL ci-dessous sur votre appareil et vous cliquez sur Installer pour être transféré sur la page des téléchargements. | ||
+ | |||
+ | https:// |
issue105/python.1454248699.txt.gz · Dernière modification : 2016/01/31 14:58 de andre_domenech