seguidor de linea — Arduino Uno simulation

An interactive Arduino Uno circuit simulation you can run free in your browser on Velxio, by agaleanoo.

Components

Sketch code

// Arduino Uno 2WD line follower.
// An L293D drives two DC motors; three DFRobot Gravity line trackers
// (SEN0017) read the tape: LOW over the black tape, HIGH on the white mat.
//
// L293D channel 1 = left motor, channel 2 = right motor:
//   EN1 (PWM) D5, IN1 D4, IN2 D7      EN2 (PWM) D6, IN3 D8, IN4 D12
// IN1 HIGH + IN2 LOW turns the motor forward.
const int EN_L = 5, IN1 = 4, IN2 = 7;
const int EN_R = 6, IN3 = 8, IN4 = 12;
const int LINE_L = 9, LINE_C = 10, LINE_R = 11;

// Graded steering at a modest speed. A two-wheel robot turns fast: hard
// pivots make it swing across the 22 mm tape until it loses it. Easing with
// two sensors on the tape and turning with one keeps it on the curves; when
// the tape is lost, a slow turn toward the side it was last seen sweeps the
// sensors back over it. This holds even when the sensors are read only every
// 100-250 ms (a busy computer), not just on a fast one.
const int CRUISE = 60;                    // 0-255
const int EASE = 15, TURN = 20;           // inner wheel: this much slower
const int SEARCH = 30;                    // lost: outer wheel only, slowly

void side(int en, int a, int b, int speed) {   // speed -255..255
  digitalWrite(a, speed >= 0 ? HIGH : LOW);
  digitalWrite(b, speed >= 0 ? LOW : HIGH);
  analogWrite(en, min(abs(speed), 255));
}

void drive(int left, int right) {
  side(EN_L, IN1, IN2, left);
  side(EN_R, IN3, IN4, right);
}

int lastTurn = -1;                        // -1 left, +1 right
unsigned long lastPrint = 0;

void setup() {
  Serial.begin(115200);
  pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT); pinMode(IN4, OUTPUT);
  pinMode(LINE_L, INPUT); pinMode(LINE_C, INPUT); pinMode(LINE_R, INPUT);
  drive(0, 0);
  Serial.println("Line follower ready");
  // A moment before moving off: the sensors settle on the tape (and on a
  // real robot, your hand is out of the way).
  delay(500);
}

void loop() {
  bool L = digitalRead(LINE_L) == LOW;
  bool C = digitalRead(LINE_C) == LOW;
  bool R = digitalRead(LINE_R) == LOW;

  if (C && !L && !R) {
    drive(CRUISE, CRUISE);                         // centred
  } else if (L && C) {
    drive(CRUISE - EASE, CRUISE); lastTurn = -1;   // drifting right: ease left
  } else if (R && C) {
    drive(CRUISE, CRUISE - EASE); lastTurn = +1;   // drifting left: ease right
  } else if (L) {
    drive(CRUISE - TURN, CRUISE); lastTurn = -1;   // tape on the left: turn left
  } else if (R) {
    drive(CRUISE, CRUISE - TURN); lastTurn = +1;   // tape on the right: turn right
  } else if (lastTurn < 0) {
    drive(0, SEARCH);                              // lost: turn back to the left
  } else {
    drive(SEARCH, 0);                              // lost: turn back to the right
  }

  if (millis() - lastPrint >= 500) {
    lastPrint = millis();
    Serial.print("line ");
    Serial.print(L); Serial.print(C); Serial.println(R);
  }
}

More projects by agaleanoo