التخطي إلى المحتوى الرئيسي

code for m5stick c and mlx90614 sensor

 

code for m5stick c and mlx90614 sensor









/*

 * NCIR Temperature Sensor MLX90614

 * April 17, 2020

 * By Yousef Shabat

 */

#include <M5StickC.h>

#include <Wire.h>

 

// Screen Orientation

#define TOP_LEFT    1

#define TOP_RIGHT   3

 

// Global Variables

int rotation = TOP_LEFT;        // Screen Orientation

boolean lock_rotation = false;  // Rotation Lock

boolean lock_screen = false;    // Screen Lock

 

// Rotate Screen

void rotated() {

  M5.Lcd.fillScreen(BLACK);

  M5.Lcd.setRotation(rotation);

}

 

// Temperature Measurement

float measurement() {

  uint16_t result;

 

  Wire.beginTransmission(0x5A); // Send Initial Signal and I2C Bus Address

  Wire.write(0x07);             // Send data only once and add one address automatically.

  Wire.endTransmission(false);  // Stop signal

  Wire.requestFrom(0x5A, 2);    // Get 2 consecutive data from 0x5A, and the data is stored only.

  result = Wire.read();         // Receive DATA

  result |= Wire.read() << 8;   // Receive DATA

  

  return result * 0.02 - 273.15;

}

 

void setup() {

  // Initialize M5StickC

  M5.begin();

  M5.MPU6886.Init();              // Initialize 6-Axis IMU

  M5.Lcd.setRotation(rotation);   // Initialize Screen

  M5.Lcd.setTextColor(WHITE, BLACK);

 

  // Initialize I2C PIN for NCIR MLX90614 hat

  Wire.begin(0, 26);

}

 

void loop() {

  M5.update();

 

  // Toggle Screen Lock

  if (M5.BtnA.wasPressed()) {

    if (lock_screen) {

      lock_screen = false;

    } else {

      lock_screen = true;

    }

  }

 

  // Toggle Rotation Lock

  if (M5.BtnB.wasPressed()) {

    if (lock_rotation) {

      lock_rotation = false;

    } else {

      lock_rotation = true;

    }

  }

 

  if (lock_screen) {

    M5.Lcd.drawString("Screen Lock", 4, 2, 1);

  } else {

    M5.Lcd.drawString("           ", 4, 2, 1);

 

    if (lock_rotation) {

      M5.Lcd.drawString("Rotation Lock", 4, 70, 1);

    } else {

      M5.Lcd.drawString("             ", 4, 70, 1);

   

      // Change screen orientation with 6-axis IMU

      float accX = 0; float accY = 0; float accZ = 0;

      M5.MPU6886.getAccelData(&accX, &accY, &accZ);

      //Serial.println(accX);

      if (accX < 0 && rotation == TOP_LEFT) {

        rotation = TOP_RIGHT;

        rotated();

      }

      if (accX > 0 && rotation == TOP_RIGHT) {

        rotation = TOP_LEFT;

        rotated();

      }

    }

 

    // Temperature measurement and display

    float temp = measurement();

    String strTemp = String(temp, 1);

    if (-10 < temp && temp < 10) {

      strTemp = "   " + strTemp;

    }

    if (10 < temp && temp < 100) {

       strTemp = " " + strTemp;

    }

    M5.Lcd.drawRightString(strTemp, 150, 16, 7);

    //Serial.println(strTemp);

  }

 

  delay(100);

}

تعليقات