马上注册 登录
中文乐高 返回首页

cttmayi的个人空间 https://bbs.cmnxt.com/?22617 [收藏] [复制] [分享] [RSS]

日志

color sorter NXC编程

已有 1025 次阅读2011-9-18 23:17 |个人分类:NXC| 编程, NXC

最近刚开始玩NXT,尝试用NXC写了一下color sorter. 可以实现基本功能

#define TOUCH_SENSOR_MOVE S2

#define TOUCH_SENSOR_DROP S1
#define COLOR_SENSOR S3

#define COLOR_MOVE OUT_C
#define COLOR_DROP OUT_B
task main()
{
SetSensorTouch(TOUCH_SENSOR_MOVE);
SetSensorTouch(TOUCH_SENSOR_DROP);
SetSensorColorFull(COLOR_SENSOR);

OnFwd(COLOR_MOVE,50);//马达旋转
until(Sensor(TOUCH_SENSOR_MOVE) == 1); //直到发现touch传感器有被按下
Off(COLOR_MOVE);
RotateMotorEx(COLOR_MOVE,50,55,0,false,true);//转动50度

while(true)
{
int degrees = 0;
unsigned int raw[];
int colorval,scaled;
unsigned int norm[];

ReadSensorColorEx(COLOR_SENSOR, colorval, raw, norm, scaled);//colorval可以返回程序detect到6种颜色,raw可以返回RGB的值

switch (colorval)
{
case INPUT_REDCOLOR:
degrees = 0;
break;
case INPUT_YELLOWCOLOR:
degrees = 90;
break;
case INPUT_GREENCOLOR:
degrees = 180;
break;
case INPUT_BLUECOLOR:
degrees = 270;
break;
default:
break;
}

RotateMotorEx(COLOR_MOVE,50,degrees,0,false,true);
OnFwd(COLOR_DROP,50);
until(Sensor(TOUCH_SENSOR_DROP) == 1);
RotateMotorEx(COLOR_DROP,50,360*0.8,0,false,true);
RotateMotorEx(COLOR_MOVE,50,(360-degrees)%360,0,false,true);
Wait(300);//Delay 300毫秒
}
}

路过

雷人

握手

鲜花

鸡蛋

评论 (0 个评论)

facelist

您需要登录后才可以评论 登录 | 马上注册

手机版|中文乐高 ( 桂ICP备13001575号-7 )

GMT+8, 2026-11-11 07:46 , Processed in 0.048271 second(s), 20 queries .

Powered by Discuz! X5.0

© 2001-2026 Discuz! Team.

返回顶部