כדי לוודא שכיוון סיבוב המנועים מתאים לקוד זה, חשוב לעקוב אחר ההוראות הבאות:
כאשר מחברים את המנועים לשלדת הפלסטיק, מניחים אותם כך שהחלקים המתכתיים והחוטים פונים זה לזה ולכיוון מרכז השלדה.
לאחר שמחברים את המנועים לשלדת הפלסטיק, מרכיבים את גשר H באחד מקצוות השלדה והוא יקבע את חזית המכונית שלנו (החלק המתכתי הגדול - גוף הקירור, יהיה קרוב יותר לקצה)
מחברים את חוטי המנועים לגשר H בצורה כזו:
שני מנועים ימניים מחוברים לצד ימין של גשר H (מסומן בתחתית הרכיב או למעלה כ- OUT3 & OUT4)
שני מנועים שמאליים מחוברים לצד השמאלי של גשר H (מסומן בתחתית הרכיב או למעלה כ- OUT1 & OUT2)
בשני הצדדים החוט התחתון מהמנועים האחוריים, והחוטים העליונים של המנועים הקדמיים מחוברים ליציאות האחוריות (OUT3 בצד ימין, & OUT2 בצד שמאל)
בשני הצדדים החוטים העליונים של המנועים האחוריים, והחוטים התחתונים של המנועים הקדמיים מחוברים ליציאות הקדמיות של גשר H (מסומנים כ-OUT4 בצד ימין, ו-OUT1 בצד שמאל)
לפני שמדבקים בתוכנה את הקוד מכאן, חשוב למחוק ולנקות את שורות הקוד והערות שנימצאות כשפותחים את הARDUINO IDE.
אפשר להעתיק את הקוד מלמטה (שבאנגלית):
// including libraries in this code
#include <TM1637Display.h> // Library for controlling a 7-segment display
#include <NewPing.h> // Library for working with ultrasonic sensors
// Defining pins for controling the motors
// These pins control the direction of the motors via the H-Bridge motor driver
#define RIGHT_FORWARD 4 // Right-side motors move forward
#define RIGHT_BACKWARD 5 // Right-side motors move backward
#define LEFT_FORWARD 7 // Left-side motors move forward
#define LEFT_BACKWARD 6 // Left-side motors move backward
// Defining ultrasonic sensor pins
#define TRIG_PIN 8 // Trigger pin for sending the ultrasound pulse
#define ECHO_PIN 9 // Echo pin for receiving the reflected signal
// Defining buzzer pin
#define BUZZER_PIN 11 // Buzzer used for alerts
// Defining display pins
#define CLK A5 // Clock pin for the TM1637 display
#define DIO A4 // Data input/output pin for the TM1637 display
TM1637Display display(CLK, DIO); // Initializing display with defined pins
int Distance; // Variable to store measured distance
void setup() { // put your setup code here, to run once:
Serial.begin(9600); // Start serial communication for debugging
// Set motor control pins as output
pinMode(LEFT_FORWARD, OUTPUT);
pinMode(LEFT_BACKWARD, OUTPUT);
pinMode(RIGHT_FORWARD, OUTPUT);
pinMode(RIGHT_BACKWARD, OUTPUT);
// Set ultrasonic sensor pins
pinMode(TRIG_PIN, OUTPUT); // Trigger pin for sending the ultrasound pulse
pinMode(ECHO_PIN, INPUT); // Echo pin for receiving the reflected signal
// Set buzzer pin as output
pinMode(BUZZER_PIN, OUTPUT);
// Configure display
display.setBrightness(0x0f); // Set maximum brightness
display.clear(); // Clear the display
testCar(); // Run an initial motor test
BEEP(); // Sound buzzer twice to indicate setup is done
BEEP();
}
void loop() { // put your main code here, to run repeatedly:
checkdistance(); // Measure distance using the ultrasonic sensor
if (Distance < 40 && Distance > 0) { // If an obstacle is detected within 40 cm
Stop(); // Stop the car
Backward(); // Move backward briefly
delay(150); // delay the code by 150 milliseconds (0.15 seconds) from continuing to the next line
Serial.println("Right!"); // Print "Right!" to the serial monitor
Right(); // Turn right
delay(100); // delay the code by 100 milliseconds (0.10 seconds) from continuing to the next line
} else {
Serial.println("Forward!"); // Print "Forward!" to the serial monitor
Forward(); // Move forward
}
delay(20);
}
// Function to test motor directions
void testCar() {
Forward(); // Move forward for 1 second
delay(1000);
Backward(); // Move backward for 1 second
delay(1000);
Right(); // Turn right for 1 second
delay(1000);
Left(); // Turn left for 1 second
delay(1000);
Stop(); // Stop movement
}
// Function to measure distance using the ultrasonic sensor
void checkdistance() {
digitalWrite(TRIG_PIN, LOW); // Ensure trigger pin is low
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH); // Send a 10-microsecond pulse to trigger the sensor
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
Distance = pulseIn(ECHO_PIN, HIGH, 100000) / 58.00; // Convert echo time to distance in cm
Serial.print("Distance: "); // Print "Distance: " to the serial monitor
Serial.print(Distance); // Print the measured distance to the serial monitor
Serial.println("cm"); // Print "cm" to indicate unit in the serial monitor
display.showNumberDec((Distance / 5) * 5, false); // Display rounded distance
delay(10);
if (Distance < 15 && Distance > 0) { // If an object is very close
BEEP(); // Sound buzzer
}
}
// Function to sound the buzzer
void BEEP() {
digitalWrite(BUZZER_PIN, HIGH);
delay(50);
digitalWrite(BUZZER_PIN, LOW);
delay(50);
}
// Function to move the car forward
void Forward() {
digitalWrite(RIGHT_FORWARD, HIGH); // Right motors move forward
digitalWrite(RIGHT_BACKWARD, LOW);
digitalWrite(LEFT_FORWARD, HIGH); // Left motors move forward
digitalWrite(LEFT_BACKWARD, LOW);
}
// Function to move the car backward
void Backward() {
digitalWrite(RIGHT_FORWARD, LOW);
digitalWrite(RIGHT_BACKWARD, HIGH); // Right motors move backward
digitalWrite(LEFT_FORWARD, LOW);
digitalWrite(LEFT_BACKWARD, HIGH); // Left motors move backward
}
// Function to turn the car right
void Right() {
digitalWrite(RIGHT_FORWARD, LOW); // Stop right motors
digitalWrite(RIGHT_BACKWARD, LOW);
digitalWrite(LEFT_FORWARD, HIGH); // Left motors move forward
digitalWrite(LEFT_BACKWARD, LOW);
}
// Function to turn the car left
void Left() {
digitalWrite(RIGHT_FORWARD, HIGH); // Right motors move forward
digitalWrite(RIGHT_BACKWARD, LOW);
digitalWrite(LEFT_FORWARD, LOW); // Stop left motors
digitalWrite(LEFT_BACKWARD, LOW);
}
// Function to stop the car
void Stop() {
digitalWrite(RIGHT_FORWARD, LOW);
digitalWrite(RIGHT_BACKWARD, LOW);
digitalWrite(LEFT_FORWARD, LOW);
digitalWrite(LEFT_BACKWARD, LOW);
}