買書捐殘盟

2011年11月24日 星期四

Google App Inventor: 亂數產生器

亂數產生器,隨機產生一個或一組數字,在教室情境中很受歡迎。
當老師想要點名,卻又不想流於形式,亂數產生器得到的號碼,讓學生無法得知下一刻是誰被點到,那種急速上升刺激與快感,難以形容。

曾在Market找相關的軟體,卻未盡滿足我的需要,所以心中浮現用app inventor來開發,相信難度不高,且能客製化自己的需要來安排各種功能。

首先,我的軟體介面如下:

(圖1 軟體介面)

圖1中:
1.[亂數範圍]可由使用者自行決定起訖範圍。
2.[剔除不要的號碼]欄位,由使用者自行決定亂數產生時,以不出現此欄位指定的號碼為主。
3.當按下[隨機產生號碼]按鈕,即產生斗大的一個號碼於畫面中。

程式碼:

上述程式中:
1.StartNum及EndNum變數,分別指定為主畫面使用者輸入起訖號碼
2.RndNo變數,由[random integer函式],隨機產生一數字並指定回RndNo,範圍如上述1.
3.RemoveElementNum變數,使用者指定[剔除號碼]後,系統自動統計其個數,並指定給RemoveElementNum
4.for range迴圈:
檢視[隨機產生的數字]是否與[欲剔除的數字]相等?若相等,則重新產生一個新的數字。直至不重複為止。

2011年11月19日 星期六

Hitechnic Color Sensor 測試

一、硬體方面
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

AdressTypeContents
42HbyteColor number
43HbyteRed reading
44HbyteGreen reading
45HbyteBlue 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-->黑色