Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
altabo
/
Tankipoisid
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
8edb0c11
authored
Dec 04, 2019
by
altabo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update tank_0.1.ino
parent
b17226f7
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
48 additions
and
0 deletions
tank_0.1.ino
tank_0.1.ino
View file @
8edb0c11
...
...
@@ -12,6 +12,15 @@
int
motorSpeedA
=
0
;
int
motorSpeedB
=
0
;
int
i
;
// Includes the Servo library
#include <Servo.h>.
// Defines Tirg and Echo pins of the Ultrasonic Sensor
const
int
trigPin
=
10
;
const
int
echoPin
=
11
;
// 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
(
enA
,
OUTPUT
);
...
...
@@ -20,6 +29,10 @@ void setup() {
pinMode
(
in2
,
OUTPUT
);
pinMode
(
in3
,
OUTPUT
);
pinMode
(
in4
,
OUTPUT
);
pinMode
(
trigPin
,
OUTPUT
);
// Sets the trigPin as an Output
pinMode
(
echoPin
,
INPUT
);
// Sets the echoPin as an Input
Serial
.
begin
(
9600
);
myServo
.
attach
(
12
);
// Defines on which pin is the servo motor attached
}
void
loop
()
{
// Set Motor A forkward
...
...
@@ -49,4 +62,38 @@ void loop() {
delay
(
200
);
}
delay
(
2000
);
// rotates the servo motor from 15 to 165 degrees
for
(
int
i
=
15
;
i
<=
165
;
i
++
){
myServo
.
write
(
i
);
delay
(
30
);
distance
=
calculateDistance
();
// Calls a function for calculating the distance measured by the Ultrasonic sensor for each degree
Serial
.
print
(
i
);
// Sends the current degree into the Serial Port
Serial
.
print
(
","
);
// Sends addition character right next to the previous value needed later in the Processing IDE for indexing
Serial
.
print
(
distance
);
// Sends the distance value into the Serial Port
Serial
.
print
(
"."
);
// 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
--
){
myServo
.
write
(
i
);
delay
(
30
);
distance
=
calculateDistance
();
Serial
.
print
(
i
);
Serial
.
print
(
","
);
Serial
.
print
(
distance
);
Serial
.
print
(
"."
);
}
}
// 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
;
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment