/* Implementation of an arduino robot line follower. */ /* Read/write pins. */ int EN1 = 6; int EN2 = 5; int IN1 = 7; int IN2 = 4; boolean hasStarted = false; /* Light sensors. */ int sensorLeft, sensorMiddle, sensorRight; /* Run motor 1. */ void Motor1(int pwm, boolean reverse) { analogWrite(EN1, pwm); if (reverse) { digitalWrite(IN1, HIGH); } else { digitalWrite(IN1, LOW); } } /* Run motor 2. */ void Motor2(int pwm, boolean reverse) { analogWrite(EN2, pwm); if (reverse) { digitalWrite(IN2, HIGH); } else { digitalWrite(IN2, LOW); } } void setup() { int i; for (i = 4; i <= 7; i++) { pinMode(i, OUTPUT); } Serial.begin(9600); } void loop() { /* Begin on black line. */ if (sensorMiddle < 820) { hasStarted = true; } if (hasStarted) { /* Read sensor input. */ sensorLeft = analogRead(A0); sensorMiddle = analogRead(A1); sensorRight = analogRead(A2); /* Stop at intersection. */ if (sensorLeft < 820 && sensorRight < 820) { Motor1(0, false); Motor2(0, false); } /* Go forward when centered on black line. */ else if (sensorLeft > 820 && sensorRight > 820) { Motor1(150, false); Motor2(150, false); } /* Correct to left. */ else if (sensorLeft < 820 && sensorRight > 820) { Motor1(255, true); Motor2(255, false); } /* Correct to right. */ else if (sensorLeft > 820 && sensorRight < 820) { Motor1(255, false); Motor2(255, true); } } }