 | Code: #pragma config(Sensor, S1, S1, sensorTouch) #pragma config(Sensor, S2, S2, sensorTouch) #pragma config(Sensor, S4, S4, sensorLightActive) #pragma config(Motor, motorA, motorA, tmotorNormal, PIDControl, encoder) #pragma config(Motor, motorB, motorB, tmotorNormal, PIDControl, encoder) #pragma config(Motor, motorC, motorC, tmotorNormal, PIDControl, encoder) //*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
const int SENSOR_ERR = 5; //Constant for the error range acceptable from the sensor
bool isAround(int sensorVal, int arrayVal){ // function to determine if the sensor value is around the given array value return abs(sensorVal - arrayVal) < SENSOR_ERR; }
void movements1(){ //code if the light sensor recognizes a value of colorArr[0] motor[motorA] = 0; //Motor A is stopped motor[motorB] = 0; //Motor B is stopped motor[motorC] = 0; //Motor C is stopped
}
void movements2(){ //if the light sensor recognizes a value of colorArr[1] if (SensorValue[S1]==1){ //if Sensor Touch S1 is pressed these movements bMotorFlippedMode[motorB] = 0; //Motor B forward direction motor[motorB] = 40; //Motor B power 40 while(nMotorEncoder[motorB] < 50); //Motor B 50 degrees target motor[motorB] = 0; //Motor B is stopped bMotorFlippedMode[motorA] = 0; //Motor A forward direction motor[motorA] = 20; //Motor A power 20 while(nMotorEncoder[motorA] < 90); //Motor A 90 degrees target motor[motorA] = 0; //Motor A is stopped wait1Msec(2000); //Wait 2 sec bMotorFlippedMode[motorA] = 1; //Motor A reverse direction motor[motorA] = 10; //Motor A power 10 while(nMotorEncoder[motorA] < 0); //Motor A 0 degrees target motor[motorA] = 0; //Motor A is stopped bMotorFlippedMode[motorB] = 1; //Motor B reverse direction motor[motorB] = 20; //Motor B power 20 bMotorFlippedMode[motorB] = 0; //Motor B forward direction while(nMotorEncoder[motorB] > 0); //Motor B 0 degrees target motor[motorB] = 0; //Motor B is stopped }
}
void movements3(){ //if the light sensor recognizes a value of colorArr[2] if (SensorValue[S2]==1){ //if Sensor Touch S2 is pressed these movements bMotorFlippedMode[motorB] = 1; //Motor B reverse direction motor[motorB] = 40; //Motor B power 40 while(nMotorEncoder[motorB] < 50); //Motor B 50 degrees target motor[motorB] = 0; //Motor B is stopped bMotorFlippedMode[motorA] = 0; //Motor A forward direction motor[motorA] = 20; //Motor A power 20 while(nMotorEncoder[motorA] < 90); //Motor A 90 degrees target motor[motorA] = 0; //Motor A is stopped wait1Msec(3000); //Wait 3 sec bMotorFlippedMode[motorC] = 0; //Motor C forward direction motor[motorC] = 20; //Motor C power 20 while(nMotorEncoder[motorC] < 95); //Motor A 95 degrees target motor[motorC] = 0; //Motor C is stopped wait1Msec(1000); //Wait 1 sec bMotorFlippedMode[motorC] = 1; //Motor C reverse direction motor[motorC] = 20; //Motor C power 20 while(nMotorEncoder[motorC] < 0); //Motor C 0 degrees target motor[motorC] = 0; //Motor C is stopped bMotorFlippedMode[motorA] = 1; //Motor A reverse direction motor[motorA] = 10; //Motor A power 10 while(nMotorEncoder[motorA] < 0); //Motor A 0 degrees target motor[motorA] = 0; //Motor A is stopped bMotorFlippedMode[motorB] = 0; //Motor B forward direction motor[motorB] = 20; //Motor B power 20 bMotorFlippedMode[motorB] = 1; //Motor B reverse direction while(nMotorEncoder[motorB] > 0); //Motor B 0 degrees target motor[motorB] = 0; //Motor B is stopped } }
void movements4(){ //if the light sensor recognizes a value of colorArr[3] bMotorFlippedMode[motorA] = 0; //Motor A forward direction motor[motorA] = 20; //Motor A power 20 while(nMotorEncoder[motorA] < 90); //Motor A 90 degrees target motor[motorA] = 0; //Motor A is stopped wait1Msec(2000); //Wait 2 sec bMotorFlippedMode[motorA] = 1; //Motor A reverse direction motor[motorA] = 10; //Motor A is stopped while(nMotorEncoder[motorA] < 0); //Motor A 0 degrees target motor[motorA] = 0; //Motor A is stopped
}
task main() //Here start the main program { int msgSize; byte msg[58];
nxtDisplayTextLine(0, " Calibration"); //Displaying text "Calibration" nxtDisplayTextLine(2, "Press the orange"); //Displaying text "Press the orange" nxtDisplayTextLine(3, "button on the NXT"); //Displaying text "button on the NXT" nxtDisplayTextLine(4, "to set the values"); //Displaying text "to set the values" while(nNxtButtonPressed == -1); //take control over the NXT buttons while(nNxtButtonPressed == 3); //press button 3 (the orange button) eraseDisplay(); //Erase the Display
int colorArr[4]; //Create and initialize variable colorArr with 4 values
colorArr[0] = 0; //Firs value on colorArr colorArr[1] = 0; //Second value on colorArr colorArr[2] = 0; //Third value on colorArr colorArr[3] = 0; //Fourth value on colorArr
nxtDisplayTextLine(0, " Calibration"); //Displaying text "Calibration" nxtDisplayTextLine(2, "Press the orange"); //Displaying text "Press the orange" nxtDisplayTextLine(3, "button on the NXT"); //Displaying text "button on the NXT" nxtDisplayTextLine(4, "to set the value"); //Displaying text "to set the values" nxtDisplayTextLine(5, "when there is"); //Displaying text "when there is" nxtDisplayTextLine(6, "nothing"); //Displaying text "nothing" while(nNxtButtonPressed == -1); //take control over the NXT buttons while(nNxtButtonPressed == 3); //press button 3 (the orange button) colorArr[0] = SensorValue[S4]; //when we press the orange button we set value of colorArr[0] from the Light Sensor S4
eraseDisplay(); //Erase the Display nxtDisplayTextLine(0, " Calibration"); //Displaying text "Calibration" nxtDisplayTextLine(2, "Press the orange"); //Displaying text "Press the orange" nxtDisplayTextLine(3, "button on the NXT"); //Displaying text "button on the NXT" nxtDisplayTextLine(4, "to set the value"); //Displaying text "to set the value" nxtDisplayTextLine(5, "of the first"); //Displaying text "of the first" nxtDisplayTextLine(6, "color"); //Displaying text "color" while(nNxtButtonPressed == -1); //take control over the NXT buttons while(nNxtButtonPressed == 3); //press button 3 (the orange button) colorArr[1] = SensorValue[S4]; //when we press the orange button we set value of colorArr[1] from the Light Sensor S4
eraseDisplay(); //Erase the Display nxtDisplayTextLine(0, " Calibration"); //Displaying text "Calibration" nxtDisplayTextLine(2, "Press the orange"); //Displaying text "Press the orange" nxtDisplayTextLine(3, "button on the NXT"); //Displaying text "button on the NXT" nxtDisplayTextLine(4, "to set the value"); //Displaying text "to set the value" nxtDisplayTextLine(5, "of the second"); //Displaying text "of the second" nxtDisplayTextLine(6, "color"); //Displaying text "color" while(nNxtButtonPressed == -1); //take control over the NXT buttons while(nNxtButtonPressed == 3); //press button 3 (the orange button) colorArr[2] = SensorValue[S4]; //when we press the orange button we set value of colorArr[2] from the Light Sensor S4
eraseDisplay(); //Erase the Display nxtDisplayTextLine(0, " Calibration"); //Displaying text "Calibration" nxtDisplayTextLine(2, "Press the orange"); //Displaying text "Press the orange" nxtDisplayTextLine(3, "button on the NXT"); //Displaying text "button on the NXT" nxtDisplayTextLine(4, "to set the value"); //Displaying text "to set the value" nxtDisplayTextLine(5, "of the third"); //Displaying text "of the third" nxtDisplayTextLine(6, "color"); //Displaying text "color" while(nNxtButtonPressed == -1); //take control over the NXT buttons while(nNxtButtonPressed == 3); //press button 3 (the orange button) colorArr[3] = SensorValue[S4]; //when we press the orange button we set value of colorArr[3] from the Light Sensor S4
eraseDisplay(); //Erase the Display string s; //make string s sprintf(s, "%d %d %d %d", colorArr[0], colorArr[1], colorArr[2], colorArr[3]); //remember the Value of colorArr[0], Value of colorArr[1], Value of colorArr[2], Value of colorArr[3] nxtDisplayTextLine(2, " Now"); //Displaying text "Now" nxtDisplayTextLine(3, " Robot Sorter"); //Displaying text "Robot Sorter" nxtDisplayTextLine(4, " wait for"); //Displaying text "wait for" nxtDisplayTextLine(5, " details..."); //Displaying text "details..." nxtDisplayTextLine(6, s); //Displaying text "Value of colorArr[0], Value of colorArr[1], Value of colorArr[2], Value of colorArr[3]" while(nNxtButtonPressed == -1); //take control over the NXT buttons while(nNxtButtonPressed == 3); //press button 3 (the orange button) eraseDisplay(); //Erase the Display
while(nNxtButtonPressed == -1){ //take control over the NXT buttons int sensor = SensorValue[S4]; //Create and initialize variable sensor = SensorValue[S4] sprintf(s, "Sensor: %d", sensor); //remember the Value of SensorValue[S4] nxtDisplayTextLine(2, s); //Displaying text "Value of SensorValue[S4]" int color = -1; //Create and initialize variable color = -1 for(int i = 0; i < 4; i++){ //a for loop that goes from 0 - 3 if(isAround(sensor, colorArr[i])) //check if the sensor value is around the value of colorArr[i] color = i; //initialize color az i }
sprintf(s, "Color: %d", color); //remember the values of string "color" nxtDisplayTextLine(3, s); //Displaying text value of the detail which is there
if (color >= 0){ //Decide which of the functions to execute switch(color){ switch (cCmdBTCheckStatus(0)) { case 0 : movements1(); break;
case 1 : movements2(); break;
case 2 : movements3(); break;
case 3 : movements4(); break;
case(ioRsltSuccess): // connected; nxtDisplayTextLine(2," Connected"); // check for msg in mailbox1; msgSize=cCmdMessageGetSize(mailbox1); if(msgSize>0) { cCmdMessageRead(msg, msgSize, mailbox1); [color=#FF0000] if(msg[0]==115 && msg[1]==116 && msg[2]==111 && msg[3]==112)[/color] //////////////////////// <- ASCII values of (stop) { StopAllTasks(); } } break; case(ioRsltCommChannelNotReady): // not connected; nxtDisplayTextLine(2,"Disconnected"); break; default:
default : break; } } } { while (true) { nxtDisplayTextLine(1, "%3d, %3d",cCmdBTCheckStatus(0)); // check for a BT connection
{
// Other condition; nxtDisplayTextLine(2, "%3d, %3d",cCmdBTCheckStatus(0)); break; } wait1Msec(10); } } }
|  |