// defines pins numbers const int trigPinLeft = 10; const int echoPinLeft = 17; const int trigPinMiddle = 6; const int echoPinMiddle = 7; const int trigPinRight = 5; const int echoPinRight = 4; const int motLeft = 12; const int bLeft = 9; const int motRight = 13; const int bright = 8; const int speedLeft = 3; const int speedRight = 11; String text = " "; //light senseor int lightSensorPin = A2; int analogValue = 0; // defines variables long durationLeft; int distanceLeft; long durationMiddle; int distanceMiddle; long durationRight; int distanceRight; int dir = 0; bool randomgenerate; void setup() { //led //digital 2 pinMode(motLeft, OUTPUT); //Initiates Motor Channel A pin pinMode(9, OUTPUT); //Initiates Brake Channel A pin //pin 3 is speed for channel A pinMode(motRight, OUTPUT); //Initiates Motor Channel B pin pinMode(8, OUTPUT); //Initiates Brake Channel B pin //pin 11 is speed for channel B //Analog 0 and 1 are current sensing on shield //Digital 3, 8, 9, 11, 12, 13 are special on shield //can not use these for anything else //Left pinMode(trigPinLeft, OUTPUT); // Sets the trigPin as an Output pinMode(echoPinLeft, INPUT); // Sets the echoPin as an Input //Middle pinMode(trigPinMiddle, OUTPUT); // Sets the trigPin as an Output pinMode(echoPinMiddle, INPUT); // Sets the echoPin as an Input //Right pinMode(trigPinRight, OUTPUT); // Sets the trigPin as an Output pinMode(echoPinRight, INPUT); // Sets the echoPin as an Input Serial.begin(9600); // Starts the serial communication } void loop() { //Left pin, clears pin, delays, sets pin to high state for 10 us, clears pin, returns val digitalWrite(trigPinLeft, LOW); delayMicroseconds(2); digitalWrite(trigPinLeft, HIGH); delayMicroseconds(10); digitalWrite(trigPinLeft, LOW); durationLeft = pulseIn(echoPinLeft, HIGH); //Middle pin """ digitalWrite(trigPinMiddle, LOW); delayMicroseconds(2); digitalWrite(trigPinMiddle, HIGH); delayMicroseconds(10); digitalWrite(trigPinMiddle, LOW); durationMiddle = pulseIn(echoPinMiddle, HIGH); //Right pin """ digitalWrite(trigPinRight, LOW); delayMicroseconds(2); digitalWrite(trigPinRight, HIGH); delayMicroseconds(10); digitalWrite(trigPinRight, LOW); durationRight = pulseIn(echoPinRight, HIGH); // Calculating the distance distanceLeft= durationLeft*0.034/2; distanceMiddle= durationMiddle*0.034/2; distanceRight= durationRight*0.034/2; // Prints the distance on the Serial Monitor Serial.print("Distance L M R: "); Serial.println(); Serial.println(distanceLeft); Serial.println(distanceMiddle); Serial.println(distanceRight); analogValue = analogRead(lightSensorPin); Serial.print("Light sensor: "); Serial.println(analogValue); Serial.println(randomgenerate); Serial.println(dir); Serial.println(); Serial.println(); //Logic if ( distanceMiddle>20){ randomgenerate=0; } if (distanceMiddle<=20) { if (randomgenerate == 0) { dir = random(6); randomgenerate = 1; } if (dir> 2){ digitalWrite(motLeft, 0); digitalWrite(motRight, 0); analogWrite(speedLeft, 255); analogWrite(speedRight, 255); Serial.println("LLLLLLLLLLL"); } else { digitalWrite(motLeft, 1); digitalWrite(motRight, 1); analogWrite(speedLeft, 255); analogWrite(speedRight, 255); Serial.println("RRRRRRRRRR"); } } else if (distanceLeft<20) { digitalWrite(motLeft, 1); digitalWrite(motRight, 0); analogWrite(speedRight, 100); analogWrite(speedLeft, 255); Serial.println("Turning Right"); } else if (distanceRight<20) { digitalWrite(motLeft, 1); //Establishes forward direction of Channel A digitalWrite(motRight, 0); //Establishes forward direction of Channel B analogWrite(speedLeft, 100); //Spins the motor on Channel B at full speed analogWrite(speedRight, 255); Serial.println("Turning Left"); } else { digitalWrite(motLeft, 1); //Establishes forward direction of Channel A digitalWrite(motRight, 0); //Establishes forward direction of Channel B analogWrite(speedLeft, 255); //Spins the motor on Channel A at full speed analogWrite(speedRight, 255); //Spins the motor on Channel B at full speed Serial.println("Straight"); } }