## Arduino Sonar System While It is Working

Hi,

In my previous post i mentioned my system’s basics. Now i will show my project’s presentation video.

It is not in English but you can understand how it works.

Earlier i mentioned that it has two modes. One of rmanual another for automatic. You can choose this modes from screen.

If you want to quit just press the stop button.

You can see the distances and coordinates of the object at the manual mode.

Sincerely Yours.
gökhan öztürk

## Arduino Sonar System

My project is making sonar with Arduino Mega R3.

The equipments that i used:

Arduino Mega R3
Hc-sr04 x2
Futaba S3003 Servo Motor.
3.2″ TFT LCD Screen
Shield for TFT LCD Screen
12V-1A Dc Power Supply x2

As you know HC-SC04 distance sensors measured the distance with speed of sound.
Also we know that distance is equal to velocity times time (x=v.t).
At an certain degree my sensors measures whole time (going forward and coming back)
I divide that time by two and multiply bu speed of sound in terms of cm/us.
this is about 1/29. So my equation becomes; Distance = (time duration/2)/29.

At the beginning for automatic mode; press auto at the screen and for manual mode press at the screen.
You can exit from the modes by pressing stop.

Then let’s talk about rotationary system. I placed one of my sensor at 0 degrees and another at 180 degrees.
They stand opposite to each other. While my servo motor is on , it turns from 0 to 180 degree and comes back
to origin (zero degree). In mean time one my sensor measures distance from 0 to 180 degree interval and other one is measures
180 to 360 degrees interval.

If it gets and object it multiplies the distance with cosine of angle for x-axis, with sine for y-axis.
Than i mapped my corrdinates to my radar cirle. It runs as this at automatic mode.

At manual mode , our only difference is input for servo motor in degree. I gave the input from joystick.
Also you can observe the x,y coordinates and distances for both sensors at manual mode.

I am going to post a video of my system while it is working.

Sincerely Yours.

## Servo Motor Control via Joystick on Arduino Mega

Hi, my this program will be an improvement of “Usage of Joystick With Arduino Mega” post.

I controlled my servo motor via my joystick’s one axis. At the position of rest my analogue
input has the value of (Vcc / 2) about 512 that is approximately equals to 1023 / 2 .

Logic of my programme is like this. When i turn my joystick throuht right, my servo will turn as
clockwise and also if i turn my joystick throuht left, my servo will turn as counter-clockwise.
For able to get certain limits i choose 525 and 510. If my analog input’s digitalized value is
greater than 525 turns CCV if it is less than 510 it turns CW.

Servo motor is attached to the Arduino Mega’s A7 pin.

Also there is one more important point that I did not mentioned my earlier post is I delayed a little
the after one degree movement. ( delay(15); ) The point is if I do not use this delay, my analog
input will be at same value.

I hope it helps you.

```

#include <Servo.h>

Servo myservo;
int value1 = 0; // variable to read the value from the analog pin 0
int value2 = 0; // variable to read the value from the analog pin 1
int pos = 0; // variable to store the servo position

void setup() {
pinMode(joyPin1, INPUT);
pinMode(joyPin2, INPUT);
myservo.attach(A7); // attaches the servo on pin A7 to the servo object
Serial.begin(9600);
}

void loop() {
if (value1 > 525){
if ( pos < 181 )
pos++;
}
else if (value1 < 510){
if ( -1 < pos )
pos--;
}

delay(15);
myservo.write(pos);

// delay(10);
// delay(10);

Serial.print("first value : ");
Serial.println(value1);
Serial.print("second value : ");
Serial.println(value2);
Serial.println("---------------------");

}
```

## Usage of Joystick With Arduino Mega R3

At my this project i tried to use a joystick. This component has five pins.
These are Vcc, Gnd, voltage reference 1, voltage reference 2 and switch input.
This joystick based on two potentiometers that gives an analog inputs to the
arduino. This analog signals have values between 0 to 1023.

If you use my connection ;
you will have 0,0 at right up corner and 1023,1023 at left down corner.
We also have a switch. If you press the joystick through
down it will be active.

Best wishes

```

/* Usage of Joystick With ARduino Mega
*
* Equipments :
* 1 x Arduino Mega 2560 R3
* 1 x Jojstick
*
* My personal blog https://ozturkgokhan.com/
* This programme based from http://www.arduino.cc/
*/

int value1 = 0; // variable to read the value from the analog pin 0
int value2 = 0; // variable to read the value from the analog pin 1

void setup() {
pinMode(joyPin1, INPUT);
pinMode(joyPin2, INPUT);
Serial.begin(9600);
}

void loop() {