I'm just trying to keep some style I guess. But yeah, changed that, thanks!
int sensorFrontInput = 7;
int sensorFrontEcho = 8;
int sensorSideInput = 3;
int sensorSideEcho = 4;
int relay1 = 10;
int relay2 = 11;
int relay3 = 12;
int relay4 = 13;
int frontLimit = 20;
int sideLimit1 = 30;
int sideLimit2 = 70;
void setup() {
pinMode(sensorFrontEcho, INPUT);
pinMode(sensorFrontInput, INPUT);
pinMode(sensorSideEcho, INPUT);
pinMode(sensorSideInput, INPUT);
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
pinMode(relay4, OUTPUT);
}
double ping(int outPin, int inPin) //Get CM to obstacle in front of the sensor
{
long duration;
pinMode(outPin, OUTPUT);
pinMode(inPin, INPUT);
digitalWrite(outPin, LOW);
delayMicroseconds(2);
digitalWrite(outPin, HIGH);
delayMicroseconds(10);
digitalWrite(outPin, LOW);
delayMicroseconds(10);
duration = pulseIn(inPin, HIGH);
return duration / 29.0 / 2.0;
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
}
void stop(){
digitalWrite(relay2, HIGH);
}
void reverse(){
digitalWrite(relay1, HIGH);
}
void turnLeft(){
digitalWrite(relay3, HIGH);
}
void turnRight(){
digitalWrite(relay3, HIGH);
digitalWrite(relay4, HIGH);
}
void straight(){
digitalWrite(relay3, LOW);
digitalWrite(relay4, LOW);
digitalWrite(relay1, LOW);
digitalWrite(relay2, LOW);
}
void loop()
{
int distanceFront = ping(sensorFrontInput, sensorFrontEcho);
int distanceSide = ping(sensorSideInput, sensorSideEcho);
if (distanceFront < frontLimit){
turnLeft();}
if (distanceSide < sideLimit1){
turnRight();}
if ((distanceFront > frontLimit) and (distanceSide < sideLimit2)){
straight();}
if (distanceSide > sideLimit2){
turnRight();}
delay(200);
}
Basically I do want the default to be the relays off so that full power straight on is my default for my motors. If there's something in front I want to turn left, go to main. If something too close to that right, turn left, go to main. If nothing in the front but something between 70 to 30 cm to the right, drive straight and go to main. If there's nothing closer than 70 to the right turn right and go to main.
My ability to go straight is by default, but mainly by relay 6 and 7 being Off. Ability to turn left (or right, my wires might be crossed on that but I can switch them) is by relay3 being On. The opposite direction is by relay 3 and relay 4 being On. I could also do reverse by relay 7 On and should stop by relay 6 On.