color sensor 接在nxt主機上的IN_4。二、軟體方面
根據[Third-party sensors: a review of some Hitechnic’s sensors]一文提到:
The new and totally redesigned HiTechnic Color Sensor Version 2 (V2) operates by using a single white LED (light emitting diode) to illuminate the target and analyses the color components of the light reflected by the target’s surface and calculates a Color Number that is returned to the NXT program.Sensor Register Layout
Adress Type Contents 42H byte Color number 43H byte Red reading 44H byte Green reading 45H byte Blue reading Procedures are already available in Bricxcc to use the sensor:
- SetSensorLowspeed() initializes the I2C communication with the sensor.
- SensorHTColorNum(port) returns the value of Color number.
- ReadSensorHTRawColor(port, rouge, vert, bleu) returns the RGB readings.
- ReadSensorHTColor(port, index, rouge, vert, bleu) returns the value of Color number, and the RGB readings. See the code example at the end of the article ...
所以在NXC的程式基本採用的指令,包含二個:
1. SetSensorLowspeed(IN_4)
2. ReadSensorHTColor(IN_4,colorvalue,r,g,b)
其中,IN_4是彩色感應器接向IN_4,而colorvalue是經由color sensor回應的讀值,r,g,b分別為red,green,blue三原色的數值。
三、程式碼
#define ColorSensorPort IN_4
task main()
{
byte r,g,b;
byte colorvalue;
SetSensorLowspeed(ColorSensorPort);
while(true)
{
ReadSensorHTColor(ColorSensorPort, colorvalue,r,g,b);
NumOut(0,LCD_LINE3,colorvalue);
if (colorvalue==0) PlayTone(880,25);
Wait(25);
ClearScreen();
}
}
上述程式是說:
當color sensor讀值(colorvalue)=0,則PlayTone一個短音。
colorvalue=0-->黑色
沒有留言:
張貼留言