1 | #include <TFT.h>
|
2 | #include <SPI.h>
|
3 | #include <Servo.h>
|
4 | #define echo 4
|
5 | #define trig 10
|
6 | #define CS A3
|
7 | #define DC A2
|
8 | #define RES 7
|
9 | #define MOSI 6
|
10 | #define SCLK 2
|
11 |
|
12 | Adafruit_ST7735 screen = Adafruit_ST7735(CS, DC, MOSI, SCLK, RES);
|
13 | Servo myservo; // create servo object to control a servo
|
14 | // a maximum of eight servo objects can be created
|
15 | int dis;
|
16 | char disC[5];
|
17 | String disS;
|
18 | int pos = 0; // variable to store the servo position
|
19 | int freeRam () {
|
20 | extern int __heap_start, *__brkval;
|
21 | int v;
|
22 | return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
|
23 | }
|
24 | void abstand(){
|
25 | digitalWrite(trig, LOW);
|
26 | delayMicroseconds(2);
|
27 | digitalWrite(trig, HIGH);
|
28 | delayMicroseconds(10);
|
29 | digitalWrite(trig, LOW);
|
30 | //dis = pulseIn(echo, HIGH);
|
31 | disS = String(dis/58);
|
32 | disS.toCharArray(disC, 5);
|
33 | }
|
34 | void lcdtext(char *distance, char *text){
|
35 | screen.stroke(255, 255, 255);
|
36 | screen.setTextSize(2);
|
37 | screen.text(text, 0, 0);
|
38 | screen.setTextSize(5);
|
39 | screen.text(distance, 0, 25);
|
40 | screen.stroke(0, 0, 0);
|
41 | screen.text(distance, 0, 25);
|
42 | }
|
43 | void setup() {
|
44 | screen.initR(INITR_REDTAB);
|
45 | screen.setRotation(1);
|
46 | screen.background(0, 0, 0);
|
47 | screen.setTextSize(5);
|
48 | pinMode(echo, INPUT);
|
49 | pinMode(trig, OUTPUT);
|
50 | myservo.attach(5); // attaches the servo on pin 9 to the servo object
|
51 | }
|
52 |
|
53 |
|
54 | void loop() {
|
55 |
|
56 | for(pos = 90; pos <180; pos += 1) // goes from 0 degrees to 180 degrees
|
57 | { // in steps of 1 degree
|
58 | myservo.write(pos); // tell servo to go to position in variable 'pos'
|
59 | abstand();
|
60 | //lcdtext(disC, "Abstand(cm):");
|
61 | delay(75); // waits 15ms for the servo to reach the position
|
62 | }
|
63 | for(pos = 180; pos>=90; pos-=1) // goes from 180 degrees to 0 degrees
|
64 | {
|
65 | myservo.write(pos); // tell servo to go to position in variable 'pos'
|
66 | abstand();
|
67 | //lcdtext(disC, "Abstand(cm)");
|
68 | delay(75); // waits 15ms for the servo to reach the position
|
69 | }
|
70 |
|
71 | }
|