Modifications

Sauter à la navigation Sauter à la recherche
1 499 octets ajoutés ,  24 février 2015 à 12:03
aucun résumé de modification
Ligne 11 : Ligne 11 :  
7</nowiki>
 
7</nowiki>
   −
y
+
This returns a signed integer with a value between around -30 and 30. Note that the measurement is very noisy, this means that even if you keep the board perfectly still there will be some variation in the number that you measure. Because of this, you shouldn’t use the exact value of the x() method but see if it is in a certain range.
 +
 
 +
We will start by using the accelerometer to turn on a light if it is not flat.
 +
 
 +
<nowiki>accel = pyb.Accel()
 +
light = pyb.LED(3)
 +
SENSITIVITY = 3
 +
 
 +
while True:
 +
    x = accel.x()
 +
    if abs(x) > SENSITIVITY:
 +
        light.on()
 +
    else:
 +
        light.off()
 +
 
 +
    pyb.delay(100)</nowiki>
 +
 
 +
We create Accel and LED objects, then get the value of the x direction of the accelerometer. If the magnitude of x is bigger than a certain value {{fname|SENSITIVITY}}, then the LED turns on, otherwise it turns off. The loop has a small {{fname|pyb.delay()}} otherwise the LED flashes annoyingly when the value of x is close to {{fname|SENSITIVITY}}. Try running this on the pyboard and tilt the board left and right to make the LED turn on and off.
 +
 
 
}}
 
}}
 +
=== Exercice ===
 +
Change the above script so that the blue LED gets brighter the more you tilt the pyboard. HINT: You will need to rescale the values, intensity goes from 0-255.
    +
== Réaliser un Niveau ==
 +
[[Fichier:MicroPython-Hack-accelerometre-niveau.jpg]]<small><br/>Sous licence GPL via Wikimedia Commons - [http://commons.wikimedia.org/wiki/File:Water_level_1.jpg#mediaviewer/File:Water_level_1.jpg Water_level.jpg]</small>
 
{{MicroPython-Hack-Accelerometre-TRAILER}}
 
{{MicroPython-Hack-Accelerometre-TRAILER}}
29 836

modifications

Menu de navigation