/*this works with both motors turning in unison, with the positive of each motor on the chip end of the motor connectors. * pin 12 goes to IN1, pin 8 goes to IN3. pin 12 & 8 are FORWARD when high and 9 & 13 LOW. * see hookup image in lesson, I have the RIGHT motor going to MOTOR B */ const int threshold = 50; //This is the threshold I chose based on previous experimenting in IR lesson, to deduce when we're close to an obstacle void setup() { // put your setup code here, to run once: pinMode (13, OUTPUT); // setup our control pins as outputs. pinMode (12, OUTPUT); pinMode (11, OUTPUT); pinMode (10, OUTPUT); pinMode (9, OUTPUT); pinMode (8, OUTPUT); } void loop() { // put your main code here, to run repeatedly: //**start off by simply running forward at controlled speed in a straight line digitalWrite (12, HIGH); //set both right and left motors for forward rotation digitalWrite (13, LOW); digitalWrite (8, HIGH); digitalWrite (9, LOW); //drive forward on PWM, attempt a straight line for half a second analogWrite (11, 125); analogWrite (10, 125); if (analogRead (A0) < threshold) { //we've detected an obstacle, let's make a series of instructions backing up and turning then resuming forward movement analogWrite (11, 0); //first, stop all movement analogWrite (10, 0); delay (500); //let's just pause a second before we rotate digitalWrite (8, LOW); //set the right motor for reverse rotation digitalWrite (9, HIGH); //rotate clockwise on the spot, so as not to back into anything digitalWrite (11, HIGH); digitalWrite (10, HIGH); delay (300); //we'll time a rotation of about 1/2 second digitalWrite (11, LOW); //stop both motors digitalWrite (10, LOW); delay (500); //let's just pause a second after rotation //we've finished the turn now, we'll just let the robot loop back to its forward movement } }