利用陣列儲存每0.01秒的超音波值,當記錄結束後,使用左右鍵觀看資料。
It saves the ultrasonic sensor data with array per 0.01 seconds, and the left and right buttons can be used to view the data after the logging.
task main()
{
SetSensorUltrasonic(S4); //設定超音波感應器為 四 號
int value[100]; //設定要存 100 個值→編號 0~ 99
int i = 0; //計數用變數
while(i < 100)//存 100 個值
{
value[i] = SensorUS(S4); //存當下的感應器值到變數第 i 個
Wait(10); //等0.01秒→結束迴圈後為 0.01*100 = 1 秒
i++; //計數加一
}
i = 0; //為顯示,計數歸零,從 0~7 開始顯示
while(1)
{
if(ButtonPressed(BTNLEFT,true))//按左鍵→顯示前 8 個值
{
if(i > 0)i--;// 沒有負的編號
}
else if(ButtonPressed(BTNRIGHT,true))//按右鍵→顯示後 8 個值
{
if(i < 13)i++;//每次八個→100除8 = 12...4→有十三頁
}
//八個值,以第一次為但→i = 0→0,0+1,0+2,0+3,0+4,0+5,0+6,0+7 = 0,1,2,3,4,5,6,7
NumOut(0,LCD_LINE1,value[i]);
NumOut(0,LCD_LINE2,value[i+1],false);
NumOut(0,LCD_LINE3,value[i+2],false);
NumOut(0,LCD_LINE4,value[i+3],false);
if(i<13)//每次八個→100除8 = 12...4→有十三頁,第十三頁只有四個值
{
NumOut(0,LCD_LINE5,value[i+4],false);
NumOut(0,LCD_LINE6,value[i+5],false);
NumOut(0,LCD_LINE7,value[i+6],false);
NumOut(0,LCD_LINE8,value[i+7],false);
}
}
}
MiNdsTorms
Lego Mindstorms Application Mechanics Robolab NXT-G NXC
2014年3月25日 星期二
不使用「內建畫圓」來畫圓 Drawing a circle without "drawing circle command"
圓的方程式是:
而由三角函數定義,我們可以得知 x,y 可由角度代表,故我們個得圓的參數式為:
座標 x 的平方+ 座標 y 的平方 = 半徑 r 的平方
x = rcos(theta)、y = rsin(theta)
將其代入原式可得
rcos(theta)平方 + rsin(theta)平方 = r平方
= r 平方(cos(theta)平方+sin(theta)平方)
= r 平方
左式的 cos(theta)平方+sin(theta)平方 = 1,故左式=右式
使用 PointOut(x,y,options)去在螢幕上畫點
範例:
task main()
{
float thata = 0;
int r = 1;
while(theta < 2*PI)
{
PointOut(r*cos(theta),r*sin(theta),false);
theta = theta + 0.01;
}
while(1){}
}
2013年7月10日 星期三
輸入數字組 Inputting set of numbers
我們將輸入一組數字並顯示在螢幕上。應用左鍵作「減」、右鍵作「加」及中鍵作「確定」並利用陣列的方式來讓我們可以使用迴圈四次來輸入四個數字,最後使用螢幕顯示指令來將輸入結果輸出。
We will input a set of numbers and display the set. We can apply the left button to be "minus", right button to be "plus" and center button to be "confirm". We can use the array to let us to apply loop for 4 times to input 4 numbers and output the result with display command finally.
Example:
task main()
{
int a[4]={0,0,0,0},i=0;
while(i<4)
{
while(!ButtonPressed(BTNCENTER,true))
{
if(ButtonPressed(BTNRIGHT,true))a[i]++;
if(ButtonPressed(BTNLEFT,true))a[i]--;
NumOut(i*10,0,a[i]);
}
}
while(ButtonPressed(BTNCENTER,true))
i++;
}
while(i>0)
{
NumOut(i*10,10,a[i]);
i--;
}
Wait(5000);
}
訂閱:
文章 (Atom)