定义式:#pragmaconfig(Sensor, S4, sonarSensor, sensorSONAR) 上面的是一个示例 下面又是一段程序 前面上了那么多课,下面这段程序不难读懂 - #pragma config(Sensor, S4, sonarSensor, sensorSONAR)
- task main()
- {
- do
- {
- motor[motorA]=100;
- motor[motorC]=100;
- }
- while(SensorValue(sonarSensor)>20);
- }
复制代码 下面是两段关于超声波传感器的程序 - #pragma config(Sensor, S4, sonarSensor, sensorSONAR)
- task main(){
- int speed = 0;
-
- for(;;){
- if(SensorValue(sonarSensor) > 35)
- motor[motorC] = motor[motorB] = speed = 100;
- else if(SensorValue(sonarSensor) < 35)
- motor[motorC] = motor[motorB] = speed = -100;
- else
- motor[motorC] = motor[motorB] = speed = 0;
-
- nxtDisplayCenteredTextLine(0, "Sonar Reading");
- nxtDisplayCenteredBigTextLine(2, "%d", SensorValue(sonarSensor));
-
- nxtDisplayCenteredTextLine(5, "%d", speed);
- nxtDisplayCenteredTextLine(7, "Motor Speed");
- }
- }
复制代码 - #pragma config(Sensor, S4, sonarSensor, sensorSONAR)
- task main()
- {
- int speed = 0;
- int sonarValue = 0;
- int distance = 35;
- while(true)
- {
- sonarValue = SensorValue(sonarSensor);
- nxtDisplayCenteredTextLine(0, "Sonar Reading");
- nxtDisplayCenteredBigTextLine(2, "%d", sonarValue);
-
- wait1Msec(100);
-
- speed = (SensorValue(sonarSensor) - distance);
-
- if(speed > 100)
- {
- speed = 100;
- }
-
- nxtDisplayCenteredTextLine(5, "%d", speed);
- nxtDisplayCenteredTextLine(7, "Motor Speed");
-
- motor[motorC] = speed;
- motor[motorB] = speed;
- }
- }
复制代码 上面两段程序只是展示不要求读懂,之后的课会教你慢慢读懂 当然你也可以拷到NXT里,看看运行是什么情况 我都差点忘了传感器还有两个,角度传感器和颜色传感器 好吧,关于传感器还有两节课~~
|