// // Wiring for motor control mappings to arduino pin numbers: // // Motor 1: // EN1 - pin 3 // I1 - pin 6 // I2 - pin 7 // Motor 2: // EN3 - pin 4 // I3 - pin 9 // I4 - pin 10 // // Front sensors // // left - pin 14 // right - pin 15 // // The motor controls are wired to the L298 - refer to its data // sheet for the meanings o the pins // // the front sensors here are used as digital inputs (they could // be analog) and the built in AVR pullup resistors are enabled // so they can be connected to simple microswitches - they will read // as 0 when pressed and 1 when released void stop() { // all motors stop digitalWrite(6, 0); digitalWrite(7, 0); digitalWrite(3, 0); digitalWrite(9, 0); digitalWrite(10, 0); digitalWrite(4, 0); } void back() { // all motors reverse digitalWrite(6, 1); digitalWrite(7, 0); digitalWrite(3, 1); digitalWrite(9, 1); digitalWrite(10, 0); digitalWrite(4, 1); } void forward() { // all motors forwards digitalWrite(6, 0); digitalWrite(7, 1); digitalWrite(3, 1); digitalWrite(9, 0); digitalWrite(10, 1); digitalWrite(4, 1); } void left() { // left motor back right motor forward digitalWrite(6, 0); digitalWrite(7, 1); digitalWrite(3, 1); digitalWrite(9, 1); digitalWrite(10, 0); digitalWrite(4, 1); } void right() { // left motor forwards, right motor back digitalWrite(6, 1); digitalWrite(7, 0); digitalWrite(3, 1); digitalWrite(9, 0); digitalWrite(10, 1); digitalWrite(4, 1); } void setup() { // set up motor control as outputs pinMode(6, OUTPUT); // I1 PD6 pinMode(7, OUTPUT); // I2 PD7 pinMode(3, OUTPUT); // EN1 PD3 pinMode(9, OUTPUT); // I3 PB1 pinMode(10, OUTPUT);// I4 PB2 pinMode(4, OUTPUT);// EN2 PD4 // stop the motors stop(); // set up the sensors ans inputs and enable the pullups pinMode(14, INPUT); // PC0 - sensor 1 (microswitch) digitalWrite(14, 1); pinMode(15, INPUT); // PC1 - sensor 2 digitalWrite(15, 1); // enable pullups delay(100); // go forwards forward(); } void loop() { // if the left switch has hit a wall .... if (!digitalRead(14)) { stop(); // stop, wait a while delay(100); if (!digitalRead(15)) { // if both switches are pressed back(); // back up a bit delay(500); } right(); // turn right delay(500); stop(); delay(100); forward(); // go forwards return; } // otherwise if the right switch has hit a wall turn left and go if (!digitalRead(15)) { stop(); delay(100); left(); delay(500); stop(); delay(100); forward(); return; } }