issue117:python
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue117:python [2017/01/31 11:08] – créée auntiee | issue117:python [2017/02/08 15:04] (Version actuelle) – andre_domenech | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | Welcome back or, if you are new to the series, welcome. This month we will be doing three projects driving up to nine LEDs. They are: | + | **Welcome back or, if you are new to the series, welcome. This month we will be doing three projects driving up to nine LEDs. They are: |
• Two blinking LEDs | • Two blinking LEDs | ||
• Cylon Lights | • Cylon Lights | ||
Ligne 6: | Ligne 6: | ||
The original project (bar graph) is from a book that I reviewed a few months ago: Arduino Project Handbook by Mark Geddes (No Starch Press). I really enjoyed the book so I wanted to use at least one of his projects. | The original project (bar graph) is from a book that I reviewed a few months ago: Arduino Project Handbook by Mark Geddes (No Starch Press). I really enjoyed the book so I wanted to use at least one of his projects. | ||
- | Let’s get started by laying out the parts list and looking at the hardware layout. | + | Let’s get started by laying out the parts list and looking at the hardware layout.** |
- | The Parts List | + | Ravi de vous retrouver, ou, pour les nouveaux arrivants, bienvenue. Ce mois-ci, nous réaliserons trois projets pilotant jusqu' |
+ | • deux LED clignotantes, | ||
+ | • les lumières des Cylons, | ||
+ | • un barre-graphe. | ||
+ | |||
+ | Le projet d' | ||
+ | |||
+ | Commençons par détailler la liste de composants et voir la disposition du matériel. | ||
+ | |||
+ | **The Parts List | ||
For the projects this time, you will need: | For the projects this time, you will need: | ||
Ligne 16: | Ligne 25: | ||
• 10K Potentiometer | • 10K Potentiometer | ||
• Breadboard | • Breadboard | ||
- | • Jumpers | + | • Jumpers** |
+ | |||
+ | La liste de composants | ||
+ | |||
+ | Pour les projets d' | ||
+ | • un Arduino Uno ou Mega, | ||
+ | • 9 LED (de préférence, | ||
+ | • 9 résistances de 220 Ω, | ||
+ | • un potentiomètre de 10 kΩ, | ||
+ | • une plaque d' | ||
+ | • des cavaliers. | ||
- | The Hardware Layout | + | **The Hardware Layout |
Shown right is the Fritzing breadboard layout (I am also including the schematic, shown bottom right, for those who like to see that sort of thing.) for the Bargraph project. We can use the same component layout for all three projects, since our code will ignore any extra components. | Shown right is the Fritzing breadboard layout (I am also including the schematic, shown bottom right, for those who like to see that sort of thing.) for the Bargraph project. We can use the same component layout for all three projects, since our code will ignore any extra components. | ||
Ligne 24: | Ligne 43: | ||
Notice that the long Anode LED leads are connected to the 220 ohm resistors which are then connected to the Arduino pins 2-10 (positive LED pins), and the short Cathode LED leads (negative LED pins) are all connected to ground. | Notice that the long Anode LED leads are connected to the 220 ohm resistors which are then connected to the Arduino pins 2-10 (positive LED pins), and the short Cathode LED leads (negative LED pins) are all connected to ground. | ||
- | We’ll go through the various components when we discuss each project. | + | We’ll go through the various components when we discuss each project.** |
- | Project 1 - Two Blinking LEDs | + | La disposition du matériel |
- | This first project is really simple in both logic and implementation. The idea is to alternately turn on and off two LEDs. In this case the LEDs are the ones connected to Arduino pins 2 and 3. We’ll use the potentiometer to send a value between 0 and 1023 for the delay through Arduino analogue pin A0. The higher the value the longer the delay. Since a delay value below 30 can cause the LEDs to blink so fast that you can’t tell they are blinking, we will check the value and if it is less than 30, we’ll force it to 30. | + | À droite, vous trouvez la disposition du matériel fait avec Fritzing pour le projet de barre-graphe (J'ai inclus aussi le schéma de câblage, présenté en bas à droite, pour ceux qui aiment voir ce genre d'info.). Nous pouvons utiliser la même disposition pour les trois projets, car notre code ignorera les composants en trop. |
- | The Code | + | Notez que toutes les pattes longues des anodes des LED sont connectées à des résistances de 220 Ω qui sont ensuite connectées aux picots 2-10 (contact positif des LED) et que les pattes courtes des cathodes des LED (contact négatif des LED) sont connectées à la masse. |
+ | |||
+ | Nous parlerons des divers autres composants pendant la présentation de chaque projet. | ||
+ | |||
+ | **Project 1 - Two Blinking LEDs | ||
+ | |||
+ | This first project is really simple in both logic and implementation. The idea is to alternately turn on and off two LEDs. In this case the LEDs are the ones connected to Arduino pins 2 and 3. We’ll use the potentiometer to send a value between 0 and 1023 for the delay through Arduino analogue pin A0. The higher the value the longer the delay. Since a delay value below 30 can cause the LEDs to blink so fast that you can’t tell they are blinking, we will check the value and if it is less than 30, we’ll force it to 30.** | ||
+ | |||
+ | Projet 1 - Deux LED clignotantes | ||
+ | |||
+ | Le premier projet est vraiment simple à comprendre et à réaliser. L' | ||
+ | |||
+ | **The Code | ||
const int ledPin1 = 2; | const int ledPin1 = 2; | ||
Ligne 45: | Ligne 76: | ||
In the setup routine, we start the serial monitor to transmit at 9600 baud and the two digital pins as OUTPUT pins. | In the setup routine, we start the serial monitor to transmit at 9600 baud and the two digital pins as OUTPUT pins. | ||
+ | |||
+ | void loop() { | ||
+ | int speedReading = analogRead(analogPin); | ||
+ | if (speedReading < 30) { | ||
+ | speedReading = 30; | ||
+ | }** | ||
+ | |||
+ | Le code | ||
+ | |||
+ | const int ledPin1 = 2; | ||
+ | const int ledPin2 = 3; | ||
+ | const int analogPin = A0; | ||
+ | |||
+ | Dans les trois premières lignes, nous déclarons les constantes dont nous aurons besoin. | ||
+ | |||
+ | void setup() { | ||
+ | Serial.begin(9600); | ||
+ | pinMode(ledPin1, | ||
+ | pinMode(ledPin2, | ||
+ | } | ||
+ | |||
+ | Dans la routine setup, nous démarrons le moniteur série pour transmettre à 9600 bauds et les deux broches numériques en bornes de sortie. | ||
void loop() { | void loop() { | ||
Ligne 52: | Ligne 105: | ||
} | } | ||
- | Now we read, using the analogRead function call, the value of the potentiometer, | + | |
- | | + | **Now we read, using the analogRead function call, the value of the potentiometer, |
+ | Serial.println(speedReading); | ||
delay(speedReading); | delay(speedReading); | ||
digitalWrite(ledPin1, | digitalWrite(ledPin1, | ||
Ligne 63: | Ligne 117: | ||
Finally, we print to the Serial Monitor the value of the pot, turn on the first LED, delay however many milliseconds the pot value is, turn off the led, then turn on the next one, delay and turn if off and then repeat the entire process. | Finally, we print to the Serial Monitor the value of the pot, turn on the first LED, delay however many milliseconds the pot value is, turn off the led, then turn on the next one, delay and turn if off and then repeat the entire process. | ||
- | See how simple that was? | + | See how simple that was?** |
- | Project 2 - Cylon Lights | + | Maintenant nous lisons la valeur du potentiomètre en utilisant l' |
+ | |||
+ | Serial.println(speedReading); | ||
+ | delay(speedReading); | ||
+ | digitalWrite(ledPin1, | ||
+ | digitalWrite(ledPin2, | ||
+ | delay(speedReading); | ||
+ | digitalWrite(ledPin2, | ||
+ | } | ||
+ | |||
+ | Enfin, nous affichons la valeur du potentiomètre sur le moniteur série, allumons la première LED, attendons une durée égale à la valeur du potentiomètre, | ||
+ | |||
+ | Vous voyez comme c'est simple ? | ||
+ | |||
+ | **Project 2 - Cylon Lights | ||
In this project, we will light the LEDs in a sweep motion (0 to 8 and 8 to 0), right and left, reminiscent of the Cylons from the original 1978 Battlestar Galactica television show. (I showed the running project to a friend and his response was that it looks like the lights on a police car. I guess it’s just a matter of perspective.) Again, it is a VERY simple project. We will be using all 9 of the LEDs in this project. Since I worked with the bargraph project first, I simply modified the code to create this one. | In this project, we will light the LEDs in a sweep motion (0 to 8 and 8 to 0), right and left, reminiscent of the Cylons from the original 1978 Battlestar Galactica television show. (I showed the running project to a friend and his response was that it looks like the lights on a police car. I guess it’s just a matter of perspective.) Again, it is a VERY simple project. We will be using all 9 of the LEDs in this project. Since I worked with the bargraph project first, I simply modified the code to create this one. | ||
- | We use two simple for loops to switch the LEDs on and off in order, starting with the Arduino pin 2, going up to pin 10, and then back down to pin 2. | + | We use two simple for loops to switch the LEDs on and off in order, starting with the Arduino pin 2, going up to pin 10, and then back down to pin 2.** |
+ | |||
+ | Projet 2 - Les lumières des Cylons | ||
+ | |||
+ | Dans ce projet, nous allumerons les LED par une variation douce (de 0 à 8 et de 8 à 0), à droite et à gauche, pour rappeler les Cylons de la série télé Battlestar Galactica de 1978. (J'ai montré le projet en fonctionnement à un ami et sa réponse a été que ça ressemblait aux lumières d'une voiture de police. C'est juste une question de point de vue.) C'est encore un projet TRÉS simple. Nous utiliserons les neuf LED dans ce projet. Comme j'ai commencé par le projet de barre-graphe, | ||
+ | |||
+ | Nous utilisons des simples boucles for pour allumer et éteindre les LED dans l' | ||
- | The Code | + | **The Code |
const int ledCount = 9; | const int ledCount = 9; | ||
Ligne 89: | Ligne 163: | ||
for (counter value low, counter value high, amount to increment or decrement) | for (counter value low, counter value high, amount to increment or decrement) | ||
- | By this time, it should be simple for you to figure it all out. | + | By this time, it should be simple for you to figure it all out.** |
- | Project 3 - Bar Graph | + | Le code |
+ | |||
+ | const int ledCount = 9; | ||
+ | |||
+ | const int delayTime = 90; | ||
+ | |||
+ | int ledPins[] = {2, | ||
+ | |||
+ | Ici, nous déclarons les diverses variables que nous utiliserons. Les deux premières sont définies comme constantes et la troisième est déclarée comme un tableau qui contient les numéros des picots de l' | ||
+ | |||
+ | Dans la routine setup (ci-dessous), | ||
+ | |||
+ | C'est dans la routine loop (page suivante) que la « magie » s' | ||
+ | |||
+ | Comme vous pouvez le voir (page suivante, en haut à droite), la boucle for en C fonctionne comme ceci... | ||
+ | |||
+ | for (valeur basse du compteur, valeur haute du compteur, quantité à incrémenter ou décrémenter). | ||
+ | |||
+ | À ce stade, il devrait être simple pour vous de déchiffrer le tout. | ||
+ | |||
+ | **Project 3 - Bar Graph | ||
As I said earlier, this project is from the book Arduino Project Handbook by Mark Geddes. It’s a very easy project code wise. | As I said earlier, this project is from the book Arduino Project Handbook by Mark Geddes. It’s a very easy project code wise. | ||
- | We will be using all the hardware in this one. The idea it to give a “graphical” representation of the voltage value of the potentiometer using the nine LEDs. The lower the voltage going into the A0 pin, the fewer LEDs are lit. The higher the voltage, more are lit. The Arduino C language gives us a wonderful function called MAP that makes this a breeze. However, it can be a bit confusing at first. | + | We will be using all the hardware in this one. The idea it to give a “graphical” representation of the voltage value of the potentiometer using the nine LEDs. The lower the voltage going into the A0 pin, the fewer LEDs are lit. The higher the voltage, more are lit. The Arduino C language gives us a wonderful function called MAP that makes this a breeze. However, it can be a bit confusing at first.** |
+ | |||
+ | Projet 3 - Le barre-graphe | ||
+ | |||
+ | Comme je l'ai dit plus haut, ce projet vient du livre Arduino Project Handbook de Mark Geddes. C'est un projet très facile du point de vue du code. | ||
+ | |||
+ | Dans celui-ci, nous utiliserons tout le matériel. L' | ||
- | The Map function | + | **The Map function |
The map function takes a value, a low and high range of the input, and a low and high range that the output should be mapped to. Our code is as follows… | The map function takes a value, a low and high range of the input, and a low and high range that the output should be mapped to. Our code is as follows… | ||
Ligne 108: | Ligne 208: | ||
• The values 0 to 9 (ledCount) are the values that can be expected as the output. There is some math magic being done in the function that produces the output map. The following table shows the Input v.s. Output values. | • The values 0 to 9 (ledCount) are the values that can be expected as the output. There is some math magic being done in the function that produces the output map. The following table shows the Input v.s. Output values. | ||
- | So you can see that anytime that the voltage value into pin A0 is, for example, between 455 and 568, the output will be a 4, and in this case, the first four LEDs will be lit. | + | So you can see that anytime that the voltage value into pin A0 is, for example, between 455 and 568, the output will be a 4, and in this case, the first four LEDs will be lit.** |
+ | La fonction MAP | ||
- | The Code | + | La fonction map prend une valeur, l' |
+ | |||
+ | int ledLevel = map(sensorReading, | ||
+ | |||
+ | • ledLevel est la sortie convertie, | ||
+ | • sensorReading est le niveau d' | ||
+ | • les valeurs 0 et 1023 sont les limites de la plage des valeurs qui peuvent être attendues sur l' | ||
+ | • Les valeurs 0 et 9 (ledCount) sont les bornes des valeurs qui peuvent être attendues à la sortie. Il y a un peu de magie mathématique à l' | ||
+ | |||
+ | Ainsi, vous pouvez voir que, chaque fois que la tension d' | ||
+ | |||
+ | **The Code | ||
You have seen the first three lines and the setup routine already, so we’ll just skip over the discussion. | You have seen the first three lines and the setup routine already, so we’ll just skip over the discussion. | ||
Ligne 117: | Ligne 229: | ||
That’s it. You now have learned a lot about the Arduino language and controlling LEDs. | That’s it. You now have learned a lot about the Arduino language and controlling LEDs. | ||
- | Next time, we will be working with some of the motors that we used when we were learning the RPi, so dust them off and be ready. Until then, have fun! | + | Next time, we will be working with some of the motors that we used when we were learning the RPi, so dust them off and be ready. Until then, have fun!** |
+ | |||
+ | Le code | ||
+ | |||
+ | Vous avez déjà vu les trois premières lignes et la routine setup, aussi je passe sur leur présentation. | ||
+ | |||
+ | Et voilà. Vous en savez beaucoup maintenant sur le langage de l' | ||
+ | |||
+ | La prochaine fois, nous travaillerons avec les moteurs que nous avons utilisés quand nous apprenions le RPi ; aussi, dépoussiérez-les et soyez prêts. Jusque-là, amusez-vous bien ! |
issue117/python.1485857281.txt.gz · Dernière modification : 2017/01/31 11:08 de auntiee