Está en la página 1de 3

task main(){ SetSensorUltrasonic(IN_3); while(true){ //the ultrasonic sensor needs time to read the values //that is why we have

the 500 millisecond wait Wait(500); ClearScreen(); NumOut(0, 0, SensorUS(IN_3)); if(SensorUS(IN_3) < 50){ Off(OUT_AC); } else { OnFwd(OUT_AC, 100); } } } Touch Sensor The NXT Touch Sensor has 2 states, pressed or unpressed. In code this is repres ented with a 1 for pressed, and a 0 for unpressed. There are no other states fo r this sensor. Below is a simple program that keeps your program from running u ntil you press the Touch Sensor. (This program can be found at, "\Sample Programs\NXT\Touch\Wait for Push.c") #pragma config(Sensor, S1, touchSensor, sensorTouch) //*!!Code automatically generated by 'ROBOTC' configuration wizard !!*// task main() { while(SensorValue(touchSensor) == 0) (hasn't been pressed): { // DO NOTHING (wait for press) } while(SensorValue(touchSensor) == 1) ressed): { // DO NOTHING (wait for release) } // YOUR CODE GOES HERE // Otherwise (the touch sensor has bee n activated [pressed] ): motor[motorB] = 75; motor[motorC] = 75; wait1Msec(1000); efore moving to further code. } /* Run motors B and C forwards */ /* with a power level of 75. */ // Wait 1000 milliseconds (1 second) b

// While the Touch Sensor is inactive

// While the Touch Sensor is active (p

Light Sensor The NXT Light Sensor has a range of states between 0 and 100. The lower the num ber, the darker the reading is. The higher the number, the lighter the reading is. Below is a simple line-following program that uses only one NXT Light Senso r. (This program can be found at, "\Sample Programs\NXT\Light\Line Tracking.c") #pragma config(Sensor, S3, lightSensor, sensorLightActive) //*!!Code automatically generated by 'ROBOTC' configuration wizard !!*// task main() { wait1Msec(50); initialize the light sensor.

// The program waits 50 milliseconds to

while(true) // Infinite loop { if(SensorValue(lightSensor) < 45) // If the Light Sensor reads a value le ss than 45: { motor[motorB] = 60; // Motor B is run at a 60 power lev el. motor[motorC] = 20; // Motor C is run at a 20 power lev el. } else // If the Light Sensor reads a value gr eater than or equal to 45: { motor[motorB] = 20; // Motor B is run at a 20 power lev el. motor[motorC] = 60; // Motor C is run at a 60 power lev el. } } }

Sound Sensor The NXT Sound Sensor has a range of states between 0 and 100. The lower the num ber, the quieter the reading is. The higher the number, the louder the reading is. Below is a simple program that maps the Sound Sensor reading to the motor s peeds. (This program can be found at, "\Sample Programs\NXT\Sound\Sound Drive.c") #pragma config(Sensor, S2, soundSensor, sensorSoundDB) //*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//

task main() { wait1Msec(1000); the Sound Sensor. while(true) { motor[motorB] power level equal motor[motorC] Sound Sensor. } }

// A one-second wait is required to cleanly initialize // Infinite loop = SensorValue[soundSensor]; */ = SensorValue[soundSensor]; */ /* Motors B and C are run at a /* to the value read in by the

Ultrasonic (sonar) Sensor The NXT Ultrasonic Sensor has a range of states between 0 and 255. This number is representative of the current reading in centimeters. However, a reading of 255 means that the current sensor reading is out of range. This is a "range err or" and means that the echo is not being read back (looking down a long hall for example). A reading outside of these numbers indicates that there is no sensor attached or some error with the connection. (This program can be found at, "\Sample Programs\NXT\Sonar\Detecting Obstacles w ith Sonar.c") #pragma config(Sensor, S4, sonarSensor, sensorSONAR) //*!!Code automatically generated by 'ROBOTC' configuration wizard !!*// task main() { int distance_in_cm = 20; it to 20(cm).

// Create variable 'distance_in_cm' and initialize

while(SensorValue[sonarSensor] > distance_in_cm) /* While the Sonar Sensor r eadings are greater */ } /* than the specified, 'dis tance_in_cm': */ motor[motorB] = 75; // Motor B is run at a 75 power leve l motor[motorC] = 75; // Motor C is run at a 75 power leve l } motor[motorB] = 75; // Motor B is stopped at a 0 power level motor[motorC] = 75; // Motor C is stopped at a 0 power level } Copyright 2010 Robotics Academy/Robomatter - http://robotc.net/

También podría gustarte