检测颜色的RGB
检测颜色的RGB示例
所需硬件
电路搭建 将Battery、Core、Hub堆叠在一起,使用4pin传感器线连接传感器与Hub扩展板的IIC接口,通过MicroUSB数据线接入电脑。
代码 #include <Microduino_Gesture.h>
// Global Variables
Gesture gestureSensor;
void setup() {
// Initialize Serial port
Serial.begin(9600);
// Initialize Gesture (configure I2C and initial values)
if ( gestureSensor.begin() ) {
Serial.println(F("Gesture initialization complete"));
} else {
Serial.println(F("Something went wrong during Gesture init!"));
}
// Start running the Gesture light sensor (no interrupts)
if ( gestureSensor.enableLightSensor(false) ) {
Serial.println(F("Light sensor is now running"));
} else {
Serial.println(F("Something went wrong during light sensor init!"));
}
// Wait for initialization and calibration to finish
delay(500);
}
void loop() {
// Read the light levels (ambient, red, green, blue)
Serial.print("Ambient: ");
Serial.print(gestureSensor.readAmbientLight());
Serial.print(" Red: ");
Serial.print(gestureSensor.readRedLight());
Serial.print(" Green: ");
Serial.print(gestureSensor.readGreenLight());
Serial.print(" Blue: ");
Serial.println(gestureSensor.readBlueLight());
// Wait 1 second before next reading
delay(1000);
}
该例程效果为:下载程序后,保持USB线连接,打开串口(波特率设置为9600),观察返回值。
用Sensor-Gen&RGB识别不同颜色的RGB值
推荐距离:被测颜色与Sensor-Gen&RGB垂直距离3cm左右为最佳,并根据实际情况进行调整
注:外界环境光对识别到的RGB值会有影响
|