0% found this document useful (0 votes)
5 views4 pages

Code Car

The document contains Arduino code for controlling motors and a servo using serial commands. It allows for movement in different directions (forward, backward, left, right) and controls an LED, while also utilizing an ultrasonic sensor to measure distance. The servo sweeps from 15 to 165 degrees, reporting the angle and corresponding distance to the serial monitor.

Uploaded by

forlogins46
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Code Car

The document contains Arduino code for controlling motors and a servo using serial commands. It allows for movement in different directions (forward, backward, left, right) and controls an LED, while also utilizing an ultrasonic sensor to measure distance. The servo sweeps from 15 to 165 degrees, reporting the angle and corresponding distance to the serial monitor.

Uploaded by

forlogins46
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

char t;

void setup() {

pinMode(13,OUTPUT); //left motors forward

pinMode(12,OUTPUT); //left motors reverse

pinMode(11,OUTPUT); //right motors forward

pinMode(10,OUTPUT); //right motors reverse

pinMode(9,OUTPUT); //Led

[Link](9600);

void loop() {

if([Link]()){

t = [Link]();

[Link](t);

if(t == 'F'){ //move forward(all motors rotate in forward direction)

digitalWrite(13,HIGH);

digitalWrite(11,HIGH);

else if(t == 'B'){ //move reverse (all motors rotate in reverse direction)

digitalWrite(12,HIGH);

digitalWrite(10,HIGH);

else if(t == 'L'){ //turn right (left side motors rotate in forward direction, right side motors doesn't
rotate)

digitalWrite(11,HIGH);
}

else if(t == 'R'){ //turn left (right side motors rotate in forward direction, left side motors doesn't
rotate)

digitalWrite(13,HIGH);

else if(t == 'W'){ //turn led on or off)

digitalWrite(9,HIGH);

else if(t == 'w'){

digitalWrite(9,LOW);

else if(t == 'S'){ //STOP (all motors stop)

digitalWrite(13,LOW);

digitalWrite(12,LOW);

digitalWrite(11,LOW);

digitalWrite(10,LOW);

delay(100);

RADAR

// Includes the Servo library

#include <Servo.h>.

// Defines Tirg and Echo pins of the Ultrasonic Sensor

const int trigPin = 8;

const int echoPin = 9;


// Variables for the duration and the distance

long duration;

int distance;

Servo myServo; // Creates a servo object for controlling the servo motor

void setup() {

pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output

pinMode(echoPin, INPUT); // Sets the echoPin as an Input

[Link](9600);

[Link](12); // Defines on which pin is the servo motor attached

void loop() {

// rotates the servo motor from 15 to 165 degrees

for(int i=15;i<=165;i++){

[Link](i);

delay(30);

distance = calculateDistance();// Calls a function for calculating the distance measured by the
Ultrasonic sensor for each degree

[Link](i); // Sends the current degree into the Serial Port

[Link](","); // Sends addition character right next to the previous value needed later in the
Processing IDE for indexing

[Link](distance); // Sends the distance value into the Serial Port

[Link]("."); // Sends addition character right next to the previous value needed later in the
Processing IDE for indexing

// Repeats the previous lines from 165 to 15 degrees

for(int i=165;i>15;i--){

[Link](i);

delay(30);

distance = calculateDistance();

[Link](i);

[Link](",");
[Link](distance);

[Link](".");

// Function for calculating the distance measured by the Ultrasonic sensor

int calculateDistance(){

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH); // Reads the echoPin, returns the sound wave travel time in
microseconds

distance= duration*0.034/2;

return distance;

You might also like