#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#define TFT_CS 10
#define TFT_RST 9
#define TFT_DC 8
#define trig 7
#define echo 6
Adafruit_ILI9341 TFT = Adafruit_ILI9341 (TFT_CS, TFT_DC, TFT_RST);
#include <Servo.h>
Servo servo;
const int out = 12;
int pos = 0;
long duration;
int distance;
void setup() {
TFT.begin();
TFT.setRotation(0);
TFT.fillScreen(ILI9341_BLACK);
TFT.setTextSize(3);
servo.attach(out);
servo.write(0);
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
}
void loop() {
for (pos = 0; pos <= 180; pos++) {
servo.write(pos);
delay(10);
}
for (pos = 180; pos >= 0; pos--) {
servo.write(pos);
delay(10);
}
digitalWrite(trig, LOW);
delay(20);
digitalWrite(trig, HIGH);
delay(100);
digitalWrite(trig, LOW);
duration = pulseIn(echo,HIGH);
distance = (duration * 0.0343) / 2;
delay(1000);
TFT.fillScreen(ILI9341_BLACK);
TFT.setCursor(50, 100);
TFT.print("Razdalja : ");
TFT.print(distance);
TFT.print(" cm");
TFT.print("Stopinje : ");
TFT.print(pos);
TFT.print(" st.");
}