// line following function (black line on white ground) void stayOnTrack (int PWR) // PWR is the desired power for the motors { setMotor(MOTOR_A, -PWR); setMotor(MOTOR_B, PWR); LEDnum(0); while(getSensor(BUMPER) == 0) // make sure it hasn't crashed { //Both sensors detect white -> Go straight while (getSensor(REFLECT_1) >= 200 && getSensor(REFLECT_2) >= 200 && getSensor(BUMPER) == 0) { setMotor(MOTOR_A, -PWR); setMotor(MOTOR_B, PWR); LEDnum(7); } //REFLECT_1 detects black, REFLECT_2 detects white -> Pivot right if (getSensor(REFLECT_1) < 200 && getSensor(REFLECT_2) >= 200) { setMotor(MOTOR_A, PWR); setMotor(MOTOR_B, PWR); LEDnum(1); } //REFLECT_1 detects white, REFLECT_2 detects black -> Turn left if (getSensor(REFLECT_1) >= 200 && getSensor(REFLECT_2) < 200) { setMotor(MOTOR_A, -PWR); setMotor(MOTOR_B, PWR / 1.5); LEDnum(0); } //Both detect black -> Pivot Right if(getSensor(REFLECT_1) < 200 && getSensor(REFLECT_2) < 200) { setMotor(MOTOR_A, PWR); setMotor(MOTOR_B, PWR); } wait1Msec(10); } }
void main (void) { initialize(); wait1Msec(200); //initialize values int pwr = 15; int sensorState = -1; //determine these values by running the calibration program first int threshold1 = 150; int threshold2 = 150; int delay = 10; //initialize motors and LED indicators setMotor (MOTOR_A, 0); setMotor (MOTOR_B, 0); LEDnum(0); LEDoff(RED_LED); LEDoff(GREEN_LED); //waits for a button press while(getSensor(BUTTON) == 0); wait1Msec(100); while(getSensor(BUTTON) == 1); resetTimer(); while(getSensor(BUTTON)== 0 && time100() < 64) { //line follows for 6.4 seconds if (sensorState != getSensorState(threshold1, threshold2)) { //only change direction if conditions are different than before. sensorState = getSensorState(threshold1,threshold2); if (sensorState == 0) { //go straight drive(15, delay); } else if (sensorState == 1){ rightTurn(15,delay); } else if (sensorState == 2){ leftTurn(15, delay); } else if (sensorState == 3){ rightTurn(15, delay); } } } //hard code exit 2 rightTurn(pwr,2400); drive(pwr,9400); leftTurn(pwr,1500); drive(pwr,20000); }
void main() { initialize(); while (getSensor(BUTTON) != 1){} wait1Msec(200); while (getSensor(BUTTON) != 0){} LEDon(RED_LED); LEDoff(GREEN_LED); for (int i = 0; i < 8; i++) { LEDnum(i); wait1Msec(500); } }
void main() { initialize(); wait1Sec(2); LEDon(RED_LED); LEDoff(GREEN_LED); LEDnum(3); volatile int x = 0; while (getSensor(BUTTON) != 1){} wait1Msec(200); while (getSensor(BUTTON) != 0){} LEDon(GREEN_LED); LEDoff(RED_LED); LEDnum(3); while (getSensor(BUMPER) != 1) { if (getSensor(REFLECT_2) <0) LEDnum(0); else if (getSensor(REFLECT_2)<75) { LEDnum(1); setMotor(MOTOR_A, 10); } else if (getSensor(REFLECT_2)<150) LEDnum(2); else if (getSensor(REFLECT_2)<175) LEDnum(3); else if (getSensor(REFLECT_2)<200) LEDnum(4); else if (getSensor(REFLECT_2)<250) LEDnum(5); else if (getSensor(REFLECT_2)<300) LEDnum(6); else if (getSensor(REFLECT_2)>300) LEDnum(7); } wait1Msec(200); while (getSensor(BUMPER) != 0); wait1Msec(200); while (getSensor(BUMPER) != 1) { if (getSensor(REFLECT_1) <0) LEDnum(0); else if (getSensor(REFLECT_1)<75) LEDnum(1); else if (getSensor(REFLECT_1)<150) LEDnum(2); else if (getSensor(REFLECT_1)<175) LEDnum(3); else if (getSensor(REFLECT_1)<200) LEDnum(4); else if (getSensor(REFLECT_1)<250) LEDnum(5); else if (getSensor(REFLECT_1)<300) LEDnum(6); else if (getSensor(REFLECT_1)>300) LEDnum(7); } wait1Msec(200); while (getSensor(BUMPER) != 0); }