כדי לוודא שכיוון סיבוב המנועים מתאים לקוד זה, חשוב לעקוב אחר ההוראות הבאות:
כאשר מחברים את המנועים לשלדת הפלסטיק אנו מניחים אותם כך שהחלקים המתכתיים פונים אל הצד האחורי של המכונית, והחוטים פונים זה לזה ולכיוון מרכז השלדה.
אנו מחברים את חוטי המנועים לגשר H בצורה כזו:
החוט התחתון של המנוע הימני מחובר ל-OUT4, והחוט העליון מחובר ל-OUT3
החוט התחתון של המנוע השמאלי מחובר ל-OUT1, והחוט העליון מחובר ל-OUT2
אפשר להעתיק את הקוד מלמטה (שבאנגלית) ללא הטקסט למעלה שבעברית:
//It's important to follow these instructions for the motors rotation to comply with this code
// When connecting the motors to the plastic chassis we lay them down so the metallic parts are facing backwards
// and the wires are facing each other and towards the center of the chassis.
// We connect the wires of the motors to the H bridge in this way:
// Right motor is connected to the right side of the H bridge (marked on the bottom or top as- OUT3 & OUT4)
// Left motor connected to the left side of the H bridge (marked on the bottom or top as- OUT1 & OUT2)
// The bottom wire of the right motor is connected to OUT4, and the top wire is connected to OUT3
// The bottom wire of the left motor is connected to OUT1, and the top wire is connected to OUT2
// Including necessary library for working with the Infrared (IR) remote
#include <IRremote.h>
// Defining the pin for the IR receiver
#define IR_RECEIVE_PIN 3 // The IR sensor (receiver) is connected to pin 3
// Defining motors control pins (connected to H-Bridge motor driver)
#define RIGHT_FORWARD 4 // Controls right motor forward direction
#define RIGHT_BACKWARD 5 // Controls right motor backward direction
#define LEFT_FORWARD 7 // Controls left motor forward direction
#define LEFT_BACKWARD 6 // Controls left motor backward direction
// Defining pins for the ultrasonic sensor
#define TRIG_PIN A5 // Trigger pin for sending ultrasonic pulses
#define ECHO_PIN A4 // Echo pin for receiving reflected pulses
// Defining buzzer pin for alerts
#define BUZZER_PIN 11 // Buzzer connected to pin 11
// Defining IR remote button codes for directional control
#define UP_ARROW 24 // Code for up arrow button (move forward)
#define DOWN_ARROW 82 // Code for down arrow button (move backward)
#define LEFT_ARROW 8 // Code for left arrow button (turn left)
#define RIGHT_ARROW 90 // Code for right arrow button (turn right)
#define OK_BUTTON 28 // Code for OK Button
int Distance; // Variable to store the measured distance from the ultrasonic sensor
// Variables for timing control
long previousMillis = 0; // Stores the last time an IR signal was received
long Interval = 100; // Time interval (in milliseconds) for stopping if no IR signal is received
void setup() { // This function runs once at startup
Serial.begin(9600); // Start serial communication at 9600 baud for debugging
IrReceiver.begin(IR_RECEIVE_PIN); // Initialize the IR receiver
// Set motor control pins as outputs
pinMode(LEFT_FORWARD, OUTPUT);
pinMode(LEFT_BACKWARD, OUTPUT);
pinMode(RIGHT_FORWARD, OUTPUT);
pinMode(RIGHT_BACKWARD, OUTPUT);
// Set ultrasonic sensor pins
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
// Set buzzer pin as output
pinMode(BUZZER_PIN, OUTPUT);
}
void loop() { // This function runs repeatedly
checkdistance(); // Measure distance
// Stop the car if no IR signal is received within the defined interval
if (millis() - previousMillis > Interval) {
Stop(); // Call Stop function to halt all motors
Serial.println("=============== NOT RF"); // Print debug message
}
// Check if IR receiver detects a signal
if (IrReceiver.decode()) {
Serial.println("Receiver IR Code"); // Debug message
IrReceiver.resume(); // Resume receiving IR signals
Serial.println(IrReceiver.decodedIRData.command); // Print received IR command
previousMillis = millis(); // Reset timing for IR signal detection
// Check which button was pressed and move the car accordingly
if (IrReceiver.decodedIRData.command == DOWN_ARROW) {
Backward(); // Move backward
} else if (IrReceiver.decodedIRData.command == UP_ARROW) {
Forward(); // Move forward
}
if (IrReceiver.decodedIRData.command == LEFT_ARROW) {
Leftward(); // Turn left
} else if (IrReceiver.decodedIRData.command == RIGHT_ARROW) {
Rightward(); // Turn right
}
if (IrReceiver.decodedIRData.command == OK_BUTTON) {
BEEP(); // Beep
}
}
}
void checkdistance() { // Function to measure distance using ultrasonic sensor
digitalWrite(TRIG_PIN, LOW); // Ensure trigger is LOW before sending pulse
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH); // Send 10-microsecond pulse
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW); // Turn off trigger
// Measure pulse duration from echo pin and convert it to distance (in cm)
Distance = pulseIn(ECHO_PIN, HIGH, 100000) / 58.00;
Serial.println(Distance); // Print distance for debugging
delay(10); // Short delay
if (Distance < 12 && Distance > 0) { // If an obstacle is detected within 12 cm, beep
BEEP();
Serial.println("BEEeeP"); }
}
void BEEP() { // Function to activate the buzzer briefly
digitalWrite(BUZZER_PIN, HIGH); // Turn buzzer on
delay(50); // Wait for 50ms
digitalWrite(BUZZER_PIN, LOW); // Turn buzzer off
delay(50); // Wait for 50ms
}
// Function to move the car forward
void Forward() {
digitalWrite(RIGHT_FORWARD, HIGH); // Right motor move forward
digitalWrite(RIGHT_BACKWARD, LOW);
digitalWrite(LEFT_FORWARD, HIGH); // Left motor move forward
digitalWrite(LEFT_BACKWARD, LOW);
}
// Function to move the car backward
void Backward() {
digitalWrite(RIGHT_FORWARD, LOW);
digitalWrite(RIGHT_BACKWARD, HIGH); // Right motor move backward
digitalWrite(LEFT_FORWARD, LOW);
digitalWrite(LEFT_BACKWARD, HIGH); // Left motor move backward
}
// Function to turn the car right
void Rightward() {
digitalWrite(RIGHT_FORWARD, LOW); // Stop right motors
digitalWrite(RIGHT_BACKWARD, LOW);
digitalWrite(LEFT_FORWARD, HIGH); // Left motor move forward
digitalWrite(LEFT_BACKWARD, LOW);
}
// Function to turn the car left
void Leftward() {
digitalWrite(RIGHT_FORWARD, HIGH); // Right motor move forward
digitalWrite(RIGHT_BACKWARD, LOW);
digitalWrite(LEFT_FORWARD, LOW); // Stop left motor
digitalWrite(LEFT_BACKWARD, LOW);
}
void Stop() { // Function to stop both motors
digitalWrite(RIGHT_FORWARD, LOW);
digitalWrite(RIGHT_BACKWARD, LOW);
digitalWrite(LEFT_FORWARD, LOW);
digitalWrite(LEFT_BACKWARD, LOW);
}