
Arduino Quick Start


0x29. Once connected, the corresponding pins are G21 (SDA) and G22 (SCL).#include <M5Unified.h>
#include <M5GFX.h>
#include "Adafruit_TCS34725.h"
#define commonAnode true // set to false if using a common cathode LED.
byte gammatable[256]; // our RGB -> eye-recognized gamma color
static uint16_t color16(uint16_t r, uint16_t g, uint16_t b)
{
uint16_t _color;
_color = (uint16_t)(r & 0xF8) << 8;
_color |= (uint16_t)(g & 0xFC) << 3;
_color |= (uint16_t)(b & 0xF8) >> 3;
return _color;
}
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X);
void setup()
{
M5.begin();
M5.Power.begin();
M5.Lcd.setTextSize(2);
Serial.println("Color View Test!");
while (!tcs.begin(TCS34725_ADDRESS, &Wire)) { //PORT.A
Serial.println("No TCS34725 found ... check your connections");
M5.Lcd.drawString("No Found sensor.", 50, 100, 4);
delay(1000);
}
tcs.setIntegrationTime(TCS34725_INTEGRATIONTIME_154MS); // Sets the integration time for the
tcs.setGain(TCS34725_GAIN_4X); // Adjusts the gain on the TCS34725
}
void loop()
{
uint16_t clear, red, green, blue;
tcs.getRawData(&red, &green, &blue,
&clear); // Reads the raw red, green, blue and clear channel
// values
// Figure out some basic hex code for visualization. 生成对应的十六进制代码
uint32_t sum = clear;
float r, g, b;
r = red;
r /= sum;
g = green;
g /= sum;
b = blue;
b /= sum;
r *= 256;
g *= 256;
b *= 256;
uint16_t _color = color16((int)r, (int)g, (int)b);
M5.Lcd.setCursor(0, 20); // Place the cursor at (0,20)
M5.Lcd.fillRect(0, 20, 120, 80, BLACK); // Fill the screen with a black rectangle
M5.Lcd.print("C:");
M5.Lcd.println(clear);
M5.Lcd.print("R:");
M5.Lcd.println(red);
M5.Lcd.print("G:");
M5.Lcd.println(green);
M5.Lcd.print("B:");
M5.Lcd.println(blue);
M5.Lcd.print("0x");
M5.Lcd.print((int)r, HEX);
M5.Lcd.print((int)g, HEX);
M5.Lcd.print((int)b, HEX);
delay(1000);
}
