Modifications

Sauter à la navigation Sauter à la recherche
1 242 octets ajoutés ,  24 février 2015 à 12:05
Ligne 36 : Ligne 36 :  
== Réaliser un Niveau ==
 
== 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>
 
[[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>
 +
 +
The example above is only sensitive to the angle in the x direction but if we use the y() value and more LEDs we can turn the pyboard into a spirit level.
 +
 +
<nowiki>xlights = (pyb.LED(2), pyb.LED(3))
 +
ylights = (pyb.LED(1), pyb.LED(4))
 +
 +
accel = pyb.Accel()
 +
SENSITIVITY = 3
 +
 +
while True:
 +
    x = accel.x()
 +
    if x > SENSITIVITY:
 +
        xlights[0].on()
 +
        xlights[1].off()
 +
    elif x < -SENSITIVITY:
 +
        xlights[1].on()
 +
        xlights[0].off()
 +
    else:
 +
        xlights[0].off()
 +
        xlights[1].off()
 +
 +
    y = accel.y()
 +
    if y > SENSITIVITY:
 +
        ylights[0].on()
 +
        ylights[1].off()
 +
    elif y < -SENSITIVITY:
 +
        ylights[1].on()
 +
        ylights[0].off()
 +
    else:
 +
        ylights[0].off()
 +
        ylights[1].off()
 +
 +
    pyb.delay(100)</nowiki>
 +
 +
We start by creating a tuple of LED objects for the x and y directions. Tuples are immutable objects in python which means they can’t be modified once they are created. We then proceed as before but turn on a different LED for positive and negative x values. We then do the same for the y direction. This isn’t particularly sophisticated but it does the job. Run this on your pyboard and you should see different LEDs turning on depending on how you tilt the board.
 +
 
{{MicroPython-Hack-Accelerometre-TRAILER}}
 
{{MicroPython-Hack-Accelerometre-TRAILER}}
29 836

modifications

Menu de navigation