ROB. Car

Skeem:

Kood:

// DISTANCE VARIABLES
const int trigPin = 3;
const int echoPin = 2;
long duration, distance;

// MOTORS VARIABLES
const int mot1f = 6;
const int mot1b = 5;
const int mot2f = 11;
const int mot2b = 10;
int mot_speed = 210; // motors speed

// LOGICS VARIABLES
const int dist_stop = 15;
int errorLED = 13;

// Function declarations
int ping();
void motors_forward();
void motors_back();
void motors_stop();
void motors_left();
void motors_right();

// INITIALIZATION
void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(errorLED, OUTPUT);
}

// BASIC PROGRAM CYCLE
void loop() {
  int result = ping(); // Check distance

  if (result <= dist_stop) { // Barrier detected within stop distance
    motors_stop();
    delay(200);

    // Check distance to the left
    motors_left();
    delay(500); // Turn left for a short duration
    motors_stop();
    delay(200);
    int left_distance = ping();

    // Check distance to the right
    motors_right();
    delay(1000); // Turn right from the initial position
    motors_stop();
    delay(200);
    int right_distance = ping();

    // Return to the initial position
    motors_left();
    delay(500);
    motors_stop();
    delay(200);

    // Turn in the direction where there is no barrier
    if (left_distance > right_distance) {
      motors_left();
      delay(500);
    } else {
      motors_right();
      delay(500);
    }
    motors_stop();
    delay(200);
  } else {
    // If no barrier, keep moving forward
    motors_forward();
  }
}

// Function definitions
int ping() { // CHECK DISTANCE FUNCTION
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = duration / 58;
  return distance;
}

void motors_forward() { // MOTORS FORWARD FUNCTION
  analogWrite(mot1f, mot_speed);
  analogWrite(mot2f, mot_speed);
  digitalWrite(mot1b, LOW);
  digitalWrite(mot2b, LOW);
}

void motors_back() { // MOTORS BACK FUNCTION
  digitalWrite(mot1f, LOW);
  digitalWrite(mot2f, LOW);
  analogWrite(mot1b, mot_speed);
  analogWrite(mot2b, mot_speed);
}

void motors_stop() { // MOTORS STOP FUNCTION
  digitalWrite(mot1f, LOW);
  digitalWrite(mot2f, LOW);
  digitalWrite(mot1b, LOW);
  digitalWrite(mot2b, LOW);
}

void motors_left() { // MOTORS LEFT FUNCTION
  analogWrite(mot1f, mot_speed);
  digitalWrite(mot2f, LOW);
  digitalWrite(mot1b, LOW);
  analogWrite(mot2b, mot_speed);
}

void motors_right() { // MOTORS RIGHT FUNCTION
  digitalWrite(mot1f, LOW);
  analogWrite(mot2f, mot_speed);
  analogWrite(mot1b, mot_speed);
  digitalWrite(mot2b, LOW);
}

Kasutatud komponendid:

  • 2 mootorid. Vasak ja paream
  • 1 mootori driver
  • 6 patarei
  • 1 kauguse andur
  • 1 arduino
  • 20 juhtmed
  • 1 nupp sisselülitamiseks

Uued funktsioonid:

Uusi funktsioone ei olnud

Raskused autudega:

Autol oli probleem, et üks mootor ei töötanud hästi. Üks oli kiirem kui teine, mis põhjustas sirgelt sõitmise probleemi, kuid andsime endast parima, et seda parandada. Ülejäänud masin oli hea, ainus asi, mida neil polnud aega korpust teha

Link: