Modifications

Sauter à la navigation Sauter à la recherche
436 octets ajoutés ,  20 février 2015 à 23:20
Ligne 42 : Ligne 42 :     
== C'est Disco sur votre PyBoard ==
 
== C'est Disco sur votre PyBoard ==
So far we have only used a single LED but the pyboard has 4 available. Let’s start by creating an object for each LED so we can control each of them. We do that by creating a list of LEDS with a list comprehension.
+
Jusqu'à maintenant, nous avons uniquement utilisé une seule LED mais la pyboard dispose de 4 LEDs. Commençons par créer un objet pour chaque LED comme cela, il sera possible de toutes les commander séparément. Nous faisons cela en créant une liste de LEDs à l'aide d'une "list comprehension" ([http://domeu.blogspot.be/2009/10/comprehension-list-iterateurs-et.html voyez cet exemple]).
    
  <nowiki>leds = [pyb.LED(i) for i in range(1,5)]</nowiki>
 
  <nowiki>leds = [pyb.LED(i) for i in range(1,5)]</nowiki>
   −
If you call pyb.LED() with a number that isn’t 1,2,3,4 you will get an error message. Next we will set up an infinite loop that cycles through each of the LEDs turning them on and off.
+
Vous recevrez un message d'erreur si vous appelez pyb.LED() avec un nombre autre que 1,2,3,4. nous allons ensuite utiliser une boucle infinie pour allumer et éteindre les LEDs en cycles.
    
  <nowiki>n = 0
 
  <nowiki>n = 0
Ligne 54 : Ligne 54 :  
   pyb.delay(50)</nowiki>
 
   pyb.delay(50)</nowiki>
   −
Here, n keeps track of the current LED and every time the loop is executed we cycle to the next n (the % sign is a modulus operator that keeps n between 0 and 3.) Then we access the nth LED and toggle it. If you run this you should see each of the LEDs turning on then all turning off again in sequence.
+
Dans ce code, la variable {{fname|n}} identifie la LED actuellement manipulée. A chaque itération de la boucle nous passons à la LED suivante (le signe % est l'opérateur "Modulus", retournant le reste de la division, aussi appelé ''reste de la division euclidienne''). Cet opérateur permet de garder la valeur de n entre 0 et 3.  
 +
 
 +
Ensuite, nous accédons à la Nième LED et nous modifions son état avec la méthode {{fname|toggle}}. Si vous exécutez ce code, vous devriez voir toutes les LEDs s'allumer puis toutes les LEDs s'éteindre.
 +
 
 +
{{traduction}}
    
One problem you might find is that if you stop the script and then start it again that the LEDs are stuck on from the previous run, ruining our carefully choreographed disco. We can fix this by turning all the LEDs off when we initialise the script and then using a try/finally block. When you press CTRL-C, Micro Python generates a VCPInterrupt exception. Exceptions normally mean something has gone wrong and you can use a try: command to “catch” an exception. In this case it is just the user interrupting the script, so we don’t need to catch the error but just tell Micro Python what to do when we exit. The finally block does this, and we use it to make sure all the LEDs are off. The full code is:
 
One problem you might find is that if you stop the script and then start it again that the LEDs are stuck on from the previous run, ruining our carefully choreographed disco. We can fix this by turning all the LEDs off when we initialise the script and then using a try/finally block. When you press CTRL-C, Micro Python generates a VCPInterrupt exception. Exceptions normally mean something has gone wrong and you can use a try: command to “catch” an exception. In this case it is just the user interrupting the script, so we don’t need to catch the error but just tell Micro Python what to do when we exit. The finally block does this, and we use it to make sure all the LEDs are off. The full code is:
29 917

modifications

Menu de navigation