Skip to content

Instantly share code, notes, and snippets.

@omiq

omiq/gyro.py

Created Mar 22, 2018
Embed
What would you like to do?
Example of using Grove sensors, displays and buttons with Python on Raspberry Pi
from LSM6DS3 import *
from time import sleep
from grove_rgb_lcd import *
import grovepi
# this is the chip the sensor is based on
mySensor = LSM6DS3()
# two buttons attached to digital 3 and 4
grovepi.pinMode(3, "INPUT")
grovepi.pinMode(4, "INPUT")
# 4xLED plugged into pin 5
display = 5
grovepi.pinMode(display, "OUTPUT")
grovepi.fourDigit_init(display)
grovepi.fourDigit_brightness(display, 8)
grovepi.fourDigit_number(display, 0000, False)
# let's begin ...
print("GYRO TEST")
# loop forever
while True:
# print the gyro readings?
'''print(mySensor.readRawAccelX(), \
mySensor.readRawAccelY(), \
mySensor.readRawAccelZ(), \
mySensor.calcAnglesXY())'''
# read buttons plugged into digital pins
button1 = grovepi.digitalRead(3)
button2 = grovepi.digitalRead(4)
# set up the digits for the 4xLED
display_num = 0
if button1:
display_num += 100
if button2:
display_num += 1
# display button state
grovepi.fourDigit_number(display, display_num, False)
print(button1, button2)
# read the sensor angle and set the brightness
angle = float(mySensor.calcAnglesXY())
if angle > 1:
setRGB(0, 50, 0)
setText("- {}".format(str(angle)))
elif angle < 0:
setRGB(0, 255, 0)
setText("| {}".format(str(angle)))
else:
setRGB(0, 100, 0)
setText("/ {}".format(str(angle)))
# tiny delay
sleep(0.08)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment