“检测颜色的RGB”的版本间的差异

来自Microduino Wikipedia
跳转至: 导航搜索
(创建页面,内容为“{| style="width: 800px;" |- | <p style="color: #4F4E4E;font-size:220%">'''检测颜色的RGB示例'''</p> <br> 检测要识别的颜色的RGB值,以及环境光的…”)
 
 
(未显示同一用户的5个中间版本)
第10行: 第10行:
 
*[[mCookie-Core]]
 
*[[mCookie-Core]]
 
*[[mCookie-Battery]]
 
*[[mCookie-Battery]]
*[[MCookie-Hub]]
+
*[[mCookie-Hub/zh|mCookie-Hub]]
*一张TF卡/MicroSD卡
+
*[[Sensor-Gen&RGB/zh|Sensor-Gen&RGB]]
 
<br>
 
<br>
 
<p style="color: #E87E05;font-size:155%">电路搭建</p>
 
<p style="color: #E87E05;font-size:155%">电路搭建</p>
将Battery、Core、MCookie_SD堆叠在一起,并插入SD卡,通过MicroUSB数据线接入电脑。
+
将Battery、Core、Hub堆叠在一起,使用4pin传感器线连接传感器与Hub扩展板的IIC接口,通过MicroUSB数据线接入电脑。
 
<br>
 
<br>
 
<p style="color: #E87E05;font-size:155%">代码</p>
 
<p style="color: #E87E05;font-size:155%">代码</p>
 
<source lang="cpp">
 
<source lang="cpp">
 +
#include <Microduino_Gesture.h>
 +
// Global Variables
 +
Gesture gestureSensor;
  
#include <SPI.h>
 
#include <SD.h>
 
  
File myFile;
+
void setup() {
  
void setup() {
+
   // Initialize Serial port
   // Open serial communications and wait for port to open:
 
 
   Serial.begin(9600);
 
   Serial.begin(9600);
  while (!Serial) {
 
    ; // wait for serial port to connect. Needed for native USB port only
 
  }
 
  
 
+
   // Initialize Gesture (configure I2C and initial values)
   Serial.print("Initializing SD card...");
+
   if ( gestureSensor.begin() ) {
 
+
     Serial.println(F("Gesture initialization complete"));
// Microduino SD module: pin 7
+
  } else {
   if (!SD.begin(D7)) {
+
     Serial.println(F("Something went wrong during Gesture init!"));
     Serial.println("initialization failed!");
 
     return;
 
 
   }
 
   }
  Serial.println("initialization done.");
 
 
  // open the file. note that only one file can be open at a time,
 
  // so you have to close this one before opening another.
 
  myFile = SD.open("test.txt", FILE_WRITE);
 
  
   // if the file opened okay, write to it:
+
   // Start running the Gesture light sensor (no interrupts)
   if (myFile) {
+
   if ( gestureSensor.enableLightSensor(false) ) {
    Serial.print("Writing to test.txt...");
+
     Serial.println(F("Light sensor is now running"));
    myFile.println("testing 1, 2, 3.");
 
    // close the file:
 
    myFile.close();
 
     Serial.println("done.");
 
 
   } else {
 
   } else {
    // if the file didn't open, print an error:
+
     Serial.println(F("Something went wrong during light sensor init!"));
     Serial.println("error opening test.txt");
 
 
   }
 
   }
  
   // re-open the file for reading:
+
   // Wait for initialization and calibration to finish
   myFile = SD.open("test.txt");
+
   delay(500);
  if (myFile) {
 
    Serial.println("test.txt:");
 
 
 
    // read from the file until there's nothing else in it:
 
    while (myFile.available()) {
 
      Serial.write(myFile.read());
 
    }
 
    // close the file:
 
    myFile.close();
 
  } else {
 
    // if the file didn't open, print an error:
 
    Serial.println("error opening test.txt");
 
  }
 
 
}
 
}
  
 
void loop() {
 
void loop() {
   // nothing happens after setup
+
   // 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);
 
}
 
}
 +
</source>
  
</source>
+
*该例程效果为:下载程序后,保持USB线连接,打开串口(波特率设置为9600),观察返回值。
 +
*用Sensor-Gen&RGB识别不同颜色的RGB值
 +
*推荐距离:被测颜色与Sensor-Gen&RGB垂直距离3cm左右为最佳,并根据实际情况进行调整
 +
*注:外界环境光对识别到的RGB值会有影响
 +
*在室内白色灯光且无阳光直射的环境下测试得到颜色RGB返回值为:
 +
[[file:Microduino-Sensor-Gen&RGB_1.JPG|600px|center|颜色RGB返回值]]
 +
<br>
  
若想真正确认SD卡中的内容,可以使用读卡器连接电脑,打开SD卡中的txt文件观察内容是否与程序一致
 
 
<br>
 
<br>
 
|}
 
|}
  
[[MCookie-SD Reference | 返回SD库语法手册]]
+
[[Sensor-Gen&RGB Reference | 返回Sensor-Gen&RGB库语法手册]]

2018年5月16日 (三) 10:15的最新版本

检测颜色的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值会有影响
  • 在室内白色灯光且无阳光直射的环境下测试得到颜色RGB返回值为:
颜色RGB返回值



返回Sensor-Gen&RGB库语法手册