Devices & Components
Arduino Uno Rev3
Breadboard-
Jumper-Wires
Servo Motor (SG90)
L298N Motor Driver Module
2S LiPo Battery (7.4V) or External Power Supply
HC-SR04 ultrasonic sensor
4WD Car Chassis Kit
Software & Tools
Webotricks
Project description
Code
Code
1#include <NewPing.h> 2#define TRIG_PIN 7 3#define ECHO_PIN 8 4#define MAX_DISTANCE 200 5NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE); 6 7#define IN1 5 8#define IN2 6 9#define IN3 9 10#define IN4 10 11 12void setup() { 13 pinMode(IN1, OUTPUT); 14 pinMode(IN2, OUTPUT); 15 pinMode(IN3, OUTPUT); 16 pinMode(IN4, OUTPUT); 17} 18 19void loop() { 20 int distance = sonar.ping_cm(); 21 if (distance > 0 && distance < 20) { 22 avoidObstacle(); 23 } else { 24 followLine(); 25 } 26} 27 28void avoidObstacle() { 29 // Logic for obstacle avoidance 30} 31 32void followLine() { 33 // Logic for line following 34} 35}
Comments
Only logged in users can leave comments