1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
| //*!!Sensor, S1, soundsensor, sensorSoundDB, , !!*//
//*!!Sensor, S3, lightSensor, sensorLightActive, , !!*//
//*!!Sensor, S4, sonarSensor, sensorSONAR, , !!*//
//*!! !!*//
//*!!Start automatically generated configuration code. !!*//
const tSensors sonarSensor = (tSensors) S4; //sensorSONAR //*!!!!*//
const tSensors lightSensor = (tSensors) S3; //sensorLightActive //*!!!!*//
//*!!CLICK to edit 'wizard' created sensor & motor configuration. !!*//
//******************************************************************************//
// Mika77 //
// RobotC on NXT //
// //
// This program allows your robot do a sumo robot and fight in a ring //
// delimited by black border //
// //
// //
//******************************************************************************//
// Motors & Sensors //
// //
// [I/O Port] [Name] [Type] [Description] //
// Port 3 lightsensor Light Sensor none //
// Port 4 sonarsensor Sonar Sensor port 1 sonarsensor //
// Port A none Motor Right Motor //
// Port B none Motor Left Motor //
// //
//******************************************************************************//
task main()
{
wait1Msec(500); //the program waits 500 milliseconds before running further code
while (true) //a while loop infinite (true) is declared
{
if(SensorValue(sonarSensor) > 25) //if the sonar sensor reads a value less than 38 the if code will be run
{
motor[motorA] = -50; //motor A is runing at a 50 power level
motor[motorB] = 0; //motor B is runing at a 0 power level
wait1Msec (300); // wait 300 Msec
motor[motorA] = 0; //motor A is runing at a 0 power level
motor[motorB] = -50; //motor B is runing at a 50 power level
wait1Msec (300); // wait 300 Msec
}
else
{
motor[motorA] = -100; //motor A is running at 100 power level
motor[motorB] = -100; //motor B is running at 100 power level
}
int x,y; // int declare integer x and y
x = SensorValue(lightSensor); // x = light
nxtDisplayTextLine(1, "Light Reading:%d", x); // display light sensor reading
y = SensorValue(sonarSensor); // y = sonar
nxtDisplayTextLine(2, "Sonar Reading:%d", y); // display sonar reading
while(SensorValue(lightSensor) < 30) //"while loop" while the lightsensor reads a value less than 50 the if code will be run
{
motor[motorA] = 0;
motor[motorB] = 0;
wait1Msec(200);
motor[motorA] = 70;
motor[motorB] = 70;
wait1Msec (200);
motor[motorA] = 70;
motor[motorB] = 0;
wait1Msec (900);
}
}
} |
Partager