Beruflich Dokumente
Kultur Dokumente
digitalWrite(trigPin2, LOW);
}//endVoid
//////////////////////////////////////////////////////////Edit these as needed
int toCloseWall = 1000;
int toFarWall = 1500;
int toCloseFront = 1000;
//////////////////////////////////////////////////////////Edit these as needed
void loop(){
//-------------Main Program
Main:
rangeFront = readRangeFront();
rangeWall = readRangeWall();
if (rangeFront <= 400){
rangeFront = 3000;
}//endif
if(rangeWall <= 400){
rangeWall = 3000;
}//endif
//
The Debugger Code. Uncomment this if you're
having issues with the distance sensors.
//
Make sure to comment out the main code below
this debugger.
//
Serial.print(rangeFront);
//
Serial.print(" Front");
//
Serial.println();
//
delay(500);
//
Serial.print(rangeWall);
//
Serial.print(" Wall");
//
Serial.println();
//
delay(500);
//More debugging code is down there if you want to mess with it.
if (rangeFront < toCloseFront){
drive_backward();
// Serial.print(" Drive Back");
turn_right();
// Serial.print(" Right Turn");
// Serial.println();
delay(800);
drive_forward();
turn_left();
delay(1000);
goto Main;
}//endif
if(rangeWall > toCloseWall && rangeWall < toFarWall){
drive_forward();
no_turn();
// Serial.print(" Drive Forward");
// Serial.println();
goto Main;
}//endeif
if (rangeWall < toCloseWall){
turn_left();
// Serial.print(" Turn Left");
drive_forward();
// Serial.print(" Drive Forward");
// Serial.println();
goto Main;
}//endif
if (rangeWall > toFarWall){
turn_right();
// Serial.print(" Turn Right");
drive_forward();
// Serial.print(" Drive Forward");
// Serial.println();
goto Main;
}//endif
}//endVoid
//-------------Subroutines for the motor control
void no_turn(){
digitalWrite(motor_front[0],LOW);
digitalWrite(motor_front[1],LOW);
}
void motor_stop(){
digitalWrite(motor_back[0], LOW);
digitalWrite(motor_back[1], LOW);
digitalWrite(motor_front[0], LOW);
digitalWrite(motor_front[1],LOW);
}
void drive_forward(){
digitalWrite(motor_back[0], HIGH);
digitalWrite(motor_back[1], LOW);
}
void drive_backward(){
digitalWrite(motor_back[0], LOW);
digitalWrite(motor_back[1], HIGH);
}
void turn_left(){
digitalWrite(motor_front[0], HIGH);
digitalWrite(motor_front[1], LOW);
}
void turn_right(){
digitalWrite(motor_front[0], LOW);
digitalWrite(motor_front[1], HIGH);
}
//-------------SubRoutines for the SRF05 and SR04
int readRangeFront(){
//SRF05 Output
delay(10);
unsigned rangeFront = sonar[0].ping(); // Send ping to first array's sensor
sonar[0].timer_stop();
return rangeFront;
}
int readRangeWall(){
//SRF05 Output
delay(10);
unsigned rangeWall = sonar[1].ping(); // Send ping to second array's sensor
sonar[1].timer_stop();
return rangeWall;
}