#include <Servo.h>
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#define TFT_DC 9
#define TFT_CS 10
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
Servo myservo; // create servo object to control a servo
int potpin = A0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
void setup() {
tft.begin();
// Initialize servo
myservo.attach(7); // attaches the servo on pin 7 to the servo object
}
void loop() {
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
// Clear previous servo angle text
tft.fillRect(20, 50, 200, 20, ILI9341_BLACK);
// Display current servo angle
tft.setCursor(20, 50);
tft.setTextColor(ILI9341_YELLOW);
tft.setTextSize(2);
tft.print("Servo Angle: ");
tft.print(val);
delay(15); // waits for the servo to get there
}