HTML

accelerationExampleOne.py

Nov 18th, 2016
217
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.38 KB | None | 0 0
  1. # A simple program that changes the Finch LED based on orientation.
  2.  
  3. from finch import Finch
  4. from random import randint
  5.  
  6. # Instantiate the Finch object and connect to Finch
  7. tweety = Finch()
  8.  
  9. left, right = tweety.obstacle()
  10.  
  11. # Do the following while no obstacles are detected by Finch
  12. while not left and not right:
  13.     # Get the accelerations
  14.     x, y, z, tap, shake = tweety.acceleration()
  15.  
  16.     # Print the acceleration data
  17.     print("X is %.2f gees, Y is %.2f gees, Z is %.2f gees, tap is %r shake is %r" % (x, y, z, tap, shake));
  18.  
  19.     # Use the acceleration data to set the LED:
  20.     # beak up
  21.     if x < -0.7 and y > -0.3 and y < 0.3 and z > -0.3 and z < 0.3:
  22.         tweety.led(255,0,0);
  23.     # beak down
  24.     elif x > 0.7 and y > -0.3 and y < 0.3 and z > -0.3 and z < 0.3:
  25.         tweety.led(0,255,0);
  26.     # level
  27.     elif x > -0.5 and x < 0.5 and y > -0.5 and y < 0.5 and z > 0.7:
  28.         tweety.led(0,0,255);
  29.     # upside down
  30.     elif x > -0.5 and x < 0.5 and y > -0.5 and y < 0.5 and z < -0.7:
  31.         tweety.led(0,255,255);
  32.     # left wheel down
  33.     elif x > -0.5 and x < 0.5 and y > 0.7 and z < 0.5 and z > -0.5:
  34.         tweety.led(255,255,0);
  35.     # right wheel down
  36.     elif x > -0.5 and x < 0.5 and y < -0.7 and z < 0.5 and z > -0.5:
  37.         tweety.led(255,0,255);
  38.  
  39.     # Get obstacles to use to exit loop
  40.     left, right = tweety.obstacle()
  41.    
  42. tweety.close()
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment