int searchSpot() { //menelusuri garis hitam sampai menemukan "color" int hue; int colortemp; int threshold = 65; resetGyro(gyroSensor); moveMotorTarget(leftMotor,360,100); moveMotorTarget(rightMotor,360,100); while(getMotorMoving(leftMotor)||getMotorMoving(rightMotor)) sleep(1); while(1) { // sensor sees light: if(getColorReflected(colorSensor) < threshold) { // counter-steer right: motor[leftMotor] = 55; motor[rightMotor] = 15; } // sensor sees dark: else { // counter-steer left: motor[leftMotor] = 15; motor[rightMotor] = 55; } hue=getColorHue(colorSensor); if(hue==99 || hue==253) { colortemp=hue==99?green:red; break; } } return colortemp; }
void turnLeft(){ moveMotorTarget(leftMotor,-220,200); moveMotorTarget(rightMotor,220,200); while ( getMotorMoving(leftMotor) || getMotorMoving(rightMotor) ) { sleep(1); } }
void moveForward(int degree){ moveMotorTarget(leftMotor,degree,50); moveMotorTarget(rightMotor,degree,50); while ( getMotorMoving(leftMotor) || getMotorMoving(rightMotor) ) { sleep(1); } }
void stepAhead(int degree) { //maju degree derajat putaran roda moveMotorTarget(leftMotor,degree,50); moveMotorTarget(rightMotor,degree,50); while(getMotorMoving(leftMotor)||getMotorMoving(rightMotor)) { sleep(1); } }
void turnBack(){ resetGyro(gyroSensor); moveMotorTarget(leftMotor,447,100); moveMotorTarget(rightMotor,-447,100); while ( getMotorMoving(leftMotor) || getMotorMoving(rightMotor) ) { sleep(1); } }
void turn(int direction,int degree) { //berbelok ke arah "direction", sebesar "degree" int rightpower=direction==right?-71:71; moveMotorTarget(leftMotor,degree,-rightpower); moveMotorTarget(rightMotor,-degree,rightpower); while(getMotorMoving(leftMotor) || getMotorMoving(rightMotor)) { sleep(1); } }
void turn(int dir,int deg,int lmot,int rmot) { //berbelok ke arah "dir", sebesar "deg" derajat resetGyro(gyroSensor); setMotorSpeed(leftMotor,lmot); setMotorSpeed(rightMotor,rmot); while(getMotorMoving(leftMotor) || getMotorMoving(rightMotor)) { sleep(1); if((getGyroDegrees(gyroSensor)<deg && dir==left) ||(getGyroDegrees(gyroSensor)>deg && dir==right)) break; } }
int cekLine(int dir,int deg,int lmot,int rmot) { //mengembalikan 1 jika menemukan garis int threshold = 65; resetGyro(gyroSensor); setMotorSpeed(leftMotor,lmot); setMotorSpeed(rightMotor,rmot); while(getMotorMoving(leftMotor) || getMotorMoving(rightMotor)) { sleep(1); if(getColorReflected(colorSensor) < threshold) return 1; else if((getGyroDegrees(gyroSensor)<deg && dir==left) ||(getGyroDegrees(gyroSensor)>deg && dir==right)) break; } return 0; }
int checkLine(int dir) { int a,degmax=0; int incdegree=dir==right?10:-10; int rightpower=dir==right?-50:50; int rightdegree=dir==right?-10:10; while((degmax<2300&&dir==right) || (degmax>-2300&&dir==left)) { a=check_color(); switch(a) { case 0: moveMotorTarget(leftMotor,-rightdegree,-rightpower); moveMotorTarget(rightMotor,rightdegree,rightpower); while(getMotorMoving(leftMotor) && getMotorMoving(rightMotor)) sleep(1); degmax+=incdegree; break; case 1: motor[leftMotor]=0; motor[rightMotor]=0; sleep(100); return 1; } } }
int searchSpot() { //menelusuri garis hitam sampai menemukan "color" int a; while(1) { a=check_color(); switch(a){ case 0: //Putih_Arena -> Belok Kanan motor[leftMotor] = 15; motor[rightMotor] = 55; break; case 1: //Hitam_Garis -> Belok Kiri motor[leftMotor] = 55; motor[rightMotor] = 15; break; case 3: //Ijo_Simpang -> diem //moveMotorTarget(leftMotor,180,50); //moveMotorTarget(rightMotor,-180,-50); motor[leftMotor] =0; motor[rightMotor] =0; sleep(10); while(getMotorMoving(leftMotor) && getMotorMoving(rightMotor)) sleep(1); break; case 4: //Merah_ujung -> Balik kanan motor[leftMotor] = 100; motor[rightMotor] = -100; sleep(500); break; default: //motor[leftMotor] = 0; //motor[rightMotor] = 0; } } return a; }