Проект «Движущаяся машина»

Masina loomisel kasutasime:

2 geomootorit
20 juhet
nuppu
6 patareid
Mootori juht
Arduino uno
Liikumisandur

Raskused autoga.

Loomise ajal oli meil probleeme mootoritega (väike probleem on siiski alles). Mootorid lihtsalt ei tahtnud töötada! Vahetasime need ära, jootesime juhtmed sisse, aga siis saime hakkama.

Teine probleem autoga on see, et see sõidab ühes kohas ringi. Kahjuks ei saanud me seda probleemi kunagi lahendada.

Selle auto skeem:

Kuid me ei kasutanud bluetooth moodulit, kuna seda lihtsalt polnud

Selle auto 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);
}

Uue funktsioonid:

Kood ei sisaldanud uusi funktsioone, kuna see on üsna banaalne

Link videole: