|
楼主 |
发表于 2010-3-30 21:49:02
|
显示全部楼层
用的NXT-C,照着软件里图纸搭的,用到数组主要为了保存读取到的颜色值!
本来想用task任务让发射,闪灯,音效同步进行.可惜没做到!而且发射的精度也不好,试过发射到碗口大小的杯子里,
只能达到7中4的命中率.
下面是我的代码;
#define Color_Sensor_Port IN_2 //Define Port 2 as color sensor port
#define Touch_Sensor_Port IN_3 //Define Port 3 as color sensor port
#define Ultrasonic_Sensor_Port IN_4 //Define port 4 as ultrasonic sensor port
#define NEAR 15 //target distance is 15cm
#define MAXVOL 4 // Define volume value; maximum
#define MIDVOL 2 // Define volume value; middle
#define MINVOL 0 // Define volume value; minimum
#define Black 1 //Define black mark value
#define Blue 2 //Define Blue mark value
#define Green 3 //Define Green mark value
#define Yellow 4 //Define Yellow mark value
#define Red 5 //Define Red mark value
#define White 6 //Define White mark value
/* black:1, blue:2 green:3 yellow:4 red:5 white:6 */
int raw[3]={0,0,0};
int norm[3]={0,0,0};
int scaled[3]={0,0,0};
int Color[7]={0,0,0,0,0,0,0};
int Loop=0;
//////////////////////////////////////////////////////////////////////////////////////////////////
void Attack()
{
RotateMotorEx(OUT_B,20,45,0,false,false); //以20的速度使子弹到达炮口
RotateMotorEx(OUT_B,50,-1,0,false,false); //快速退1°为发射留有缓冲空间
RotateMotorEx(OUT_B,100,80,0,false,false); //以100的速度快速发射
RotateMotorEx(OUT_B,20,-125,0,false,false); //发射完成之后,回归原位
Wait(200); //回顾原位时间200ms
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
void Answer()
{
PlayFileEx("answer.rso",MAXVOL,FALSE); //Play voice file of Hahaha;Volume:MAXVOL;Not Loop;
Wait(2500);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
int Read_Color(int Shoot)
{
int colorval;
ReadSensorColorEx(Color_Sensor_Port, colorval, raw, norm, scaled);
return colorval;
}
void Color_Anylize(int Shoot)
{
do{
Color[Loop]=Read_Color(Shoot);
NumOut(20, 10, Color[Loop]); //Diplay the colorval on the LCD
}while((Color[Loop]==1)||(Color[Loop]==6)||(Color[Loop]==0));
}
void Color_Lamp()
{
if( Color[Loop]==2)
{
SetSensorColorBlue(Color_Sensor_Port); // Blue lamp light
Wait(SEC_1);
}
else if(Color[Loop]==3)
{
SetSensorColorGreen(Color_Sensor_Port); // Green lamp light
Wait(SEC_1);
}
else if(Color[Loop]==4) //can not light yellow lamp
{
SetSensorColorGreen(Color_Sensor_Port); // Green lamp light
SetSensorColorBlue(Color_Sensor_Port); // Blue lamp light
Wait(SEC_1);
}
else if(Color[Loop]==5)
{
SetSensorColorRed(Color_Sensor_Port); //Red lamp light
Wait(SEC_1);
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
void Color_Shoot(int Shoot)
{
repeat(4)
{
SetSensorColorGreen(Color_Sensor_Port); // Green lamp light
Wait(10);
SetSensorColorNone(Color_Sensor_Port); // light off
Wait(10);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
void Touch_Sensor()
{
int color=-1 ;
repeat(7)
{
until(SENSOR_3==1);
until(SENSOR_3==0);
Color_Anylize(color);
Color_Lamp();
SetSensorColorFull(Color_Sensor_Port); //set the value of color sensor
Loop++;
}
Loop=0;
NumOut(10, 20, Color[0]); //Diplay the colorval on the LCD
NumOut(10, 30, Color[1]); //Diplay the colorval on the LCD
NumOut(10, 40, Color[2]); //Diplay the colorval on the LCD
NumOut(20, 20, Color[3]); //Diplay the colorval on the LCD
NumOut(20, 30, Color[4]); //Diplay the colorval on the LCD
NumOut(20, 40, Color[5]); //Diplay the colorval on the LCD
NumOut(30, 20, Color[6]); //Diplay the colorval on the LCD
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
void Shooting()
{
repeat(7)
{
Color_Lamp();
Attack();
Loop++;
}
Loop=0;
}
void Initialize()
{
SetSensorColorFull(Color_Sensor_Port); //set the value of color sensor
TextOut(0,LCD_LINE1,"Shooting Boy");
SetSensor(Touch_Sensor_Port,SENSOR_TOUCH);
SetSensorLowspeed(Ultrasonic_Sensor_Port);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
task main()
{
Initialize();
Touch_Sensor();
while(SensorUS(Ultrasonic_Sensor_Port>NEAR); //Wait,until distance is smaller than 15 cm
Answer();
Shooting();
} |
|