task main () { tMotor testMotor = motorC; // playSound(soundBeepBeep); // displayBigTextLine(3, "Retract"); // FLACtretractLA(testMotor, 100); // while(!isDone(testMotor)) sleep(50); // sleep(200); // nMotorEncoder[testMotor] = 0; // sleep(1000); playSound(soundBeepBeep); eraseDisplay(); displayBigTextLine(1, "Extend"); displayTextLine(3, "ticks: 1"); displayTextLine(4, "speed: 50"); FLACextendLA(testMotor, 50, 1); while(!isDone(testMotor)) sleep(50); if (isStalled(testMotor)) displayTextLine(7, "STALLED"); sleep(1000); // playSound(soundBeepBeep); // eraseDisplay(); // displayBigTextLine(1, "Extend"); // displayTextLine(3, "ticks: 100"); // displayTextLine(4, "speed: 80"); // FLACextendLA(testMotor, 80, 100); // while(!isDone(testMotor)) sleep(50); // if (isStalled(testMotor)) // displayTextLine(7, "STALLED"); // sleep(1000); // eraseDisplay(); // playSound(soundBeepBeep); // displayBigTextLine(1, "Retract"); // displayTextLine(3, "ticks: 120"); // displayTextLine(4, "speed: 100"); // FLACtretractLA(testMotor, 100, 120); // while(!isDone(testMotor)) sleep(50); // if (isStalled(testMotor)) // displayTextLine(7, "STALLED"); // sleep(1000); playSound(soundBeepBeep); displayBigTextLine(1, "Retract"); displayTextLine(3, "ticks: 1"); displayTextLine(4, "speed: 100"); FLACtretractLA(testMotor, 100, 1); while(!isDone(testMotor)) sleep(50); if (isStalled(testMotor)) displayTextLine(7, "STALLED"); sleep(1000); playSound(soundBeepBeep); eraseDisplay(); displayBigTextLine(1, "Done"); sleep(2000); }
//IR SENSOR TASK task IR_Sense(){ ubyte request[3]; // I2C request message ubyte reply[6]; // accelerometer returns 7 values int replyLen; // length of reply array. tSensors port; // port the sensor is attached to. // which port is the accelerator hooked up to? port = S4; // Ensure the sensor is configured correctly SensorType[port] = sensorI2CCustom; eraseDisplay(); while (true) { // Read the data from the sensor // setup the request message.... request[0] = 2; // Message size is 2 bytes.. request[1] = I2C_ADDR; // I2C Address of sensor(0x02) request[2] = REG_OFFSET; // location of registers to read 0x42 replyLen = 6; // how many registers to read // send request message to sensor. sendI2CMsg(port, &request[0], replyLen); // wait for a reply on the sensor port. // also check for sensor error... if (!waitForI2C(port)) { displayBigTextLine(4, "Error 1."); wait1Msec(2000); stopAllTasks(); } // read the reply from the sensor. readI2CReply(port, &reply[0], replyLen); // make sure the bus is clear... // check for error. if (!waitForI2C(port)){ displayBigTextLine(4, "Error 2."); wait1Msec(2000); stopAllTasks(); } //Waits, then updates BrightLight value wait1Msec(5); BrightLight = reply[0]; } }
task main() { while(true) { displayBigTextLine(0, "%d" , getColorAmbient(Color1)); } }
task main(){ nMotorEncoder[Input]=0; while(true){ int pos=nMotorEncoder[Input]/8; displayBigTextLine(1,"%4d",pos); servo[toTest]=pos; servoTarget[toTest]=pos; servoChangeRate[toTest]=5; } }
task main() { while(true) { displayBigTextLine(0, "%f", calcDistMoved()); while(calcDistMoved() < 55.0) { setMotorSpeed(LeftMotor, 15); setMotorSpeed(RightMotor, 15); } setMotorSpeed(LeftMotor, 0); setMotorSpeed(RightMotor, 0); displayBigTextLine(0, "%f", calcDistMoved()); delay(100000); } }
task main() { manualCallibColor(); resetGyro(Gyro); seekBounds(); faceHeading(forwardsHeading); while(true) { if(isInsideLine()) { setMotorSpeed(LeftMotor, 30); setMotorSpeed(RightMotor, 30); } else { seekBounds(); faceHeading(forwardsHeading); eraseDisplay(); displayBigTextLine(0, "Head: %d", forwardsHeading); } } }
task main() { int count=0; int direction=1; int end=0; int doubleline=0; int pause=0; //turn right go forward until hit double line go back half a square then turn left (You are at bottom left sq now RightTurn(); while(pause==0) { motor(motorB)=SPEED; motor(motorC)=SPEED; wait1Msec(1); if(SensorValue(S3)<45&&doubleline!=1) { if(time1[T1]<200) { pause=1; } doubleline=1; } if(SensorValue(S3)>45&&doubleline==1) { doubleline=0; clearTimer(T1); } } motor[motorB]=-20; motor[motorC]=-20; wait1Msec(700); LeftTurn(); while(end!=7) { //traverse right and count lines if(direction==1) { end++; while(count<8) { if(SensorValue(S3)<45) { blacksq++; //store position to array grid[pos1][pos2]=1; } //increment counters count++; total++; displayBigTextLine(2,"Black:%d",blacksq); displayBigTextLine(5,"Count:%d ",count); displayBigTextLine(8,"Total:%d",total); //move forward one square Forward(); pos2++; } //Switches to next line if(end!=6) { direction=NextLineLeft(direction); //Resets count count=ResetCount(count); pos2=0; pos1++; } } if(direction==2) { end++; while(count<8) { if(SensorValue(S3)<45) { blacksq++; //store position to array grid[pos1][pos2]=1; } //increment counters count++; total++; displayBigTextLine(2,"Black:%d",blacksq); displayBigTextLine(5,"Count:%d ",count); displayBigTextLine(8,"Total:%d",total); //move forward one square Forward(); pos2++; } //Switches to next line if(end!=7) { //Switches to next line direction=NextLineRight(direction); //Resets count count=ResetCount(count); pos2=0; pos1++; } }//end if() //go back 7 suares and take away distance from bottom col from start from 7 and go to start }//end while() }//end main()
task main() { int count=0; int direction=1; int end=0; int doubleline=0; int pause=0; //open the file to write fileHandle=fileOpenWrite(filename); //turn right go forward until hit double line go back half a square then turn left You are at bottom left sq now RightTurn(); while(pause==0) { motor(motorB)=SPEED; motor(motorC)=SPEED; wait1Msec(1); if(SensorValue(S3)<45&&doubleline!=1) { if(time1[T1]<200) { pause=1; } doubleline=1; } if(SensorValue(S3)>45&&doubleline==1) { doubleline=0; clearTimer(T1); } } motor[motorB]=-20; motor[motorC]=-20; wait1Msec(700); LeftTurn(); //start traversing grid while(end!=7) { //traverse right and count lines if(direction==1) { end++; while(count<8) { if(SensorValue(S3)<45) { blacksq++; //store position to array grid[pos1][pos2]='1'; } //increment counters count++; total++; displayBigTextLine(2,"Black:%d",blacksq); displayBigTextLine(5,"Count:%d ",count); displayBigTextLine(8,"Total:%d",total); //move forward one square Forward(); pos2++; } //Switches to next line if(end!=7) { direction=NextLineLeft(direction); //Resets count count=ResetCount(count); pos1++; } } if(direction==2) { end++; while(count<8) { if(SensorValue(S3)<45) { blacksq++; //store position to array grid[pos1][pos2]='1'; } //increment counters count++; total++; displayBigTextLine(2,"Black:%d",blacksq); displayBigTextLine(5,"Count:%d ",count); displayBigTextLine(8,"Total:%d",total); //move forward one square Forward(); pos2--; } //Switches to next line if(end!=7) { //Switches to next line direction=NextLineRight(direction); //Resets count count=ResetCount(count); pos1++; } }//end if() }//end while() checkarray(); //Goes back to the start function GoStart(); //THIS IS THE SECOND PART OF THE PROGRAM TO MAP THE LOCATION OF THE OBJECT while(1==1) { if(getTouchValue(S2)==1) { //reset all values pos1=0; pos2=0; count=0; direction=1; end=0; while(end!=7) { //traverse right and count lines if(direction==1) { end++; while(count<8) { //check to see if there is an object 140mm away if(getUSDistance(S1)<14) { grid[pos1][pos2]='J'; motor[motorB]=0; motor[motorC]=0; wait1Msec(1000000); } //increment counters count++; Forward(); pos2++; } //Switches to next line if(end!=7) { direction=NextLineLeft(direction); //Resets count count=ResetCount(count); pos1++; } } if(direction==2) { //check to see if there is an object 140mm away end++; while(count<8) { if(getUSDistance(S1)<14) { grid[pos1][pos2]='J'; motor[motorB]=0; motor[motorC]=0; wait1Msec(1000000); } //increment counters count++; //move forward one square Forward(); pos2--; } //Switches to next line if(end!=7) { //Switches to next line direction=NextLineRight(direction); //Resets count count=ResetCount(count); pos1++; } }//end if() }//end while() } }//end if() }//end main()
void manualCallibColor() { unsigned short lightVal = 0; unsigned short greyVal = 0; unsigned short darkVal = 0; displayBigTextLine(0, "Color callib."); displayTextLine(3, "Place sensor on light surface."); displayTextLine(4, "Press Enter to accept."); while(true) { // Display the reflected value live to help manual positioning. lightVal = getColorReflected(Color1); displayBigTextLine(6, "Reflected: %d", lightVal); // Press enter to take 10 light sampels then tage the average and break out of the loop. if(getButtonPress(buttonEnter) == 1) { lightVal = avgReflectedLight(10); break; } } delay(1000); eraseDisplay(); displayBigTextLine(0, "Color callib."); displayTextLine(3, "Place sensor on grey surface."); displayTextLine(4, "Press Enter to accept."); while(true) { // Display the reflected value live to help manual positioning. greyVal = getColorReflected(Color1); displayBigTextLine(6, "Reflected: %d", greyVal); // Press enter to take 10 light sampels then tage the average and break out of the loop. if(getButtonPress(buttonEnter) == 1) { greyVal = avgReflectedLight(10); break; } } delay(1000); displayBigTextLine(0, "Color callib."); displayTextLine(3, "Place sensor on dark surface."); displayTextLine(4, "Press Enter to accept."); while(true) { // Display the reflected value live to help manual positioning. darkVal = getColorReflected(Color1); displayBigTextLine(6, "Reflected: %d", darkVal); // Press enter to take 10 light sampels then tage the average and break out of the loop. if(getButtonPress(buttonEnter) == 1) { darkVal = avgReflectedLight(10); break; } } delay(5000); threshold = (lightVal + greyVal) / 2; stopLine = darkVal; }
task main() { //Declare variables int targetImage[8][8] = {{1, 1, 1, 0, 0, 1, 1, 1}, {1, 0, 0, 0, 0, 0, 0, 1}, {1, 0, 0, 0, 0, 0, 0, 1}, {0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0}, {1, 0, 0, 0, 0, 0, 0, 1}, {1, 0, 0, 0, 0, 0, 0, 1}, {1, 1, 1, 0, 0, 1, 1, 1}}; int hitImage[8][8] = {{0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 1, 1, 1, 1, 0, 0}, {0, 1, 1, 1, 1, 1, 1, 0}, {0, 1, 1, 1, 1, 1, 1, 0}, {0, 1, 1, 1, 1, 1, 1, 0}, {0, 1, 1, 1, 1, 1, 1, 0}, {0, 0, 1, 1, 1, 1, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0}}; int splashImage[8][8] = {{0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 1, 1, 1, 1, 0, 0}, {0, 1, 0, 0, 0, 0, 1, 0}, {0, 1, 0, 0, 0, 0, 1, 0}, {0, 1, 0, 0, 0, 0, 1, 0}, {0, 1, 0, 0, 0, 0, 1, 0}, {0, 0, 1, 1, 1, 1, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0}}; int mainGrid[8][8] = {{0, 1, 1, 1, 1, 1, 0, 0}, //[0][0-8] {0, 0, 0, 0, 0, 0, 0, 0}, //[1][0-7] {0, 1, 0, 0, 0, 0, 0, 0}, //[2][0-7] {0, 1, 0, 0, 0, 1, 1, 1}, //[3][0-7] {1, 0, 0, 0, 0, 0, 0, 0}, //[4][0-7] {1, 0, 0, 0, 0, 0, 0, 0}, //[5][0-7] {1, 0, 1, 1, 1, 0, 0, 0}, //[6][0-7] {1, 0, 0, 0, 0, 0, 0, 0}}; //[7][0-7] int colourValue; int x=10; int y=10; int xMove=0; int yMove=0; int xShip=0; int yShip=7-yMove; int hits=0; //Setup colour sensor SensorType[S3] = sensorEV3_Color;//set TYPE SensorMode[S3] = modeEV3Color_Color;//set MODE while(hits<17) /* ************************************************************* Red - 5 - SHOOT Blue - 2 - LEFT Yellow - 4 - RIGHT Green - 3 - DOWN ************************************************************* */ { //Erase target image clearTarget(targetImage,x,y); sleep(500); //Check colour colourValue=getColorName(S3); sleep(50); if (colourValue==2) { if(xMove>0) { xMove--; } else if(xMove==0) { xMove=7; } } if (colourValue==4) { if(xMove<7) { xMove++; } else if(xMove==7) { xMove=0; } } if (colourValue==3) { if(yMove>0) { yMove--; } else if(yMove==0) { yMove=7; } } x=xMove*8+10; y=yMove*8+10; if (colourValue==5) { //Calculate Main Grid Location xShip=xMove; yShip=7-yMove; //Play torpedo drop sound torpedoSound(); if (mainGrid[yShip][xShip]==1) { //Play hit sound kabloom(); //Draw hit image kabloomDraw(hitImage,x,y); //Increment hits hits++; //Remove ship mainGrid[yShip][xShip]=0; } else { //Play splash sound sploosh(); //Draw splash image splooshDraw(splashImage,x,y); } } //Draw target image drawTarget(targetImage,x,y); //Sleep for a small amount of time sleep(500); } displayBigTextLine(2, "Winner!"); sleep(5000); }