Friday, May 22, 2020

ATOM Matrix: Using the MPU6886 Gyroscope

Reading data from the gyroscope is essentially the same as reading temperature or acceleration:

  • Declare three floats, here called gyroX, gyroY, and gyroZ
  • Pass the addresses into the getGyroData method
The results will be in degrees per second.

This post demonstrates how to read info from the gyroscope and use that info to control the brightness of the 5x5 display. The rate of rotation about the z-axis is read, and this is used to either increase or decrease the screen's brightness. A wasReleased check is performed to reset brightness to zero.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
 * GyroscopeTest01.ino
 * 
 * By: Mike Klepper
 * Date: 7 May 2020
 * 
 * Adjust display's brightness by measuring twists along the z-axis
 * 
 * See post on patriot-geek.blogspot.com
 * for details
 */


#include "M5Atom.h"

int GRB_COLOR_WHITE = 0xffffff;
int GRB_COLOR_BLACK = 0x000000;
int GRB_COLOR_RED = 0x00ff00;
int GRB_COLOR_ORANGE = 0xa5ff00;
int GRB_COLOR_YELLOW = 0xffff00;
int GRB_COLOR_GREEN = 0xff0000;
int GRB_COLOR_BLUE = 0x0000ff;
int GRB_COLOR_PURPLE = 0x008080;

int activeColor = GRB_COLOR_PURPLE;

const int TOLERANCE = 2;
const int SCALING_DIVISOR = 20;
const int MAX_BRIGHTNESS = 60;

float gyroX = 0;
float gyroY = 0;
float gyroZ = 0;

bool IMU6886Flag = false;

int currentBrightness = 0;

void setup() 
{
    M5.begin(true, false, true);
    delay(20);
    
    IMU6886Flag = M5.IMU.Init() == 0;
  
    // Initialize display
    M5.dis.clear();
    M5.dis.setBrightness(currentBrightness);
    
    for(int i = 0; i < 25; i++)
    {
        M5.dis.drawpix(i, activeColor);
    }

    if(!IMU6886Flag)
    {
        Serial.println("Error initializing the IMU! :-(");
    }
}

void loop() 
{
    if(IMU6886Flag)
    {
        M5.IMU.getGyroData(&gyroX, &gyroY, &gyroZ);
    
        float trackedQuantity = gyroZ;
    
        if(abs(trackedQuantity) > TOLERANCE)
        {
            currentBrightness += trackedQuantity / SCALING_DIVISOR;
             
            if(currentBrightness > MAX_BRIGHTNESS)
                currentBrightness = MAX_BRIGHTNESS;
      
            if(currentBrightness < 0)
                currentBrightness = 0;

            M5.dis.setBrightness(currentBrightness);
        }
    }
      
    if(M5.Btn.wasReleased())
    {
        Serial.println("wasReleased");
        currentBrightness = 0;
        M5.dis.setBrightness(currentBrightness);
    }
    
    delay(20);
    M5.update();
}

Click here to go to the table of contents for this series.

2 comments:

  1. Liked your series of posts showing how to use all the features of the Matrix.
    This item though I had to add a routine to drawpix as changing setBrightness and update() did not affect the display.

    You have a very clean & clear coding that I would like to emulate.

    ReplyDelete
    Replies
    1. Thanks dSetter! I hope to post more content in the future!

      Delete