#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <Adafruit_FT6206.h>
//Pins
#define stepPin 3
#define dirPin 2
#define TFT_CS 10
#define TFT_DC 9
#define TFT_MOSI 11
#define TFT_SCK 13
#define TFT_RST 8
#define TFT_MISO 12
// Colors
#define BLACK 0x0000
#define WHITE 0xFFFF
#define RED 0xF800
#define GREEN 0x07E0
// initialize ILI9341 TFT library
//Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCK, TFT_RST, TFT_MISO);
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
Adafruit_FT6206 ts = Adafruit_FT6206();
void drawRect(String first, String second,int x1,int x2, int fontSize=3) {
tft.fillRect(1, 1, 239, 158, WHITE);
tft.fillRect(1, 163, 239, 158, WHITE);
tft.setTextSize(fontSize);
tft.setTextColor(BLACK);
tft.setCursor(x1,68);
tft.println(first);
tft.setCursor(x2,229);
tft.println(second);
}
void drawKeypad(){
tft.fillRect(1,79,78,58,WHITE);
tft.fillRect(81,79,78,58,WHITE);
tft.fillRect(161,79,78,58,WHITE);
tft.fillRect(1,141,78,58,WHITE);
tft.fillRect(81,141,78,58,WHITE);
tft.fillRect(161,141,78,58,WHITE);
tft.fillRect(1,202,78,58,WHITE);
tft.fillRect(81,202,78,58,WHITE);
tft.fillRect(161,202,78,58,WHITE);
tft.fillRect(1,263,78,58,WHITE);
tft.fillRect(81,263,78,58,WHITE);
tft.fillRect(161,263,78,58,WHITE);
tft.setTextColor(BLACK);
tft.setCursor(31,98);
tft.println("1");
tft.setCursor(111,98);
tft.println("2");
tft.setCursor(191,98);
tft.println("3");
tft.setCursor(31,160);
tft.println("4");
tft.setCursor(111,160);
tft.println("5");
tft.setCursor(191,160);
tft.println("6");
tft.setCursor(31,221);
tft.println("7");
tft.setCursor(111,221);
tft.println("8");
tft.setCursor(191,221);
tft.println("9");
tft.setCursor(31,282);
tft.println(".");
tft.setCursor(111,282);
tft.println("0");
tft.setCursor(191,282);
tft.println(">");
}
double keypadInput(){
Serial.println("Keypad Input Started!");
String input="";
bool flag=true;
while(flag){
tft.setCursor(4,32);
tft.setTextColor(WHITE,BLACK);
tft.println(input);
Serial.println("Waiting");
while(!ts.touched());
TS_Point p = ts.getPoint();
p.x = map(p.x, 0, 240, 240, 0);
p.y = map(p.y, 0, 320, 320, 0);
if(p.y>=79&&p.y<141){
if(p.x>=0&&p.x<81){input=input+"1";
Serial.println(1);}
else if(p.x>=81&&p.x<161){input=input+"2";
Serial.println(2);}
else if(p.x>=161&&p.x<=240){input=input+"3";
Serial.println(3);}
}
else if(p.y>=141&&p.y<202){
if(p.x>=0&&p.x<81){input=input+"4";
Serial.println(4);}
else if(p.x>=81&&p.x<161){input=input+"5";
Serial.println(5);}
else if(p.x>=161&&p.x<=240){input=input+"6";
Serial.println(6);}
}
else if(p.y>=202&&p.y<263){
if(p.x>=0&&p.x<81){input=input+"7";
Serial.println(7);}
else if(p.x>=81&&p.x<161){input=input+"8";
Serial.println(8);}
else if(p.x>=161&&p.x<=240){input=input+"9";
Serial.println(9);}
}
else if(p.y>=263&&p.y<=320){
if(p.x>=0&&p.x<81){input=input+".";
Serial.println(".");}
else if(p.x>=81&&p.x<161){input=input+"0";
Serial.println(0);}
else if(p.x>=161&&p.x<=240){flag=false;
Serial.println(flag);}
}
delay(300);
}
return input.toDouble();
}
void erase(){
tft.fillScreen(BLACK);
}
void inputDegree(double °ree){
erase();
tft.setTextColor(WHITE);
tft.setCursor(4,4);
tft.println("DEGREE: ");
drawKeypad();
degree=keypadInput();
}
int inputRpmUsingScreen(int &rpm){
erase();
tft.setTextSize(3);
tft.setTextColor(WHITE);
tft.setCursor(4,4);
tft.println("RPM: ");
drawKeypad();
rpm=keypadInput();
return 9375/rpm;
}
int inputRPM(int &rpm){
erase();
int newDelay=0;
tft.setCursor(60,160);
tft.setTextColor(WHITE);
tft.setTextSize(3);
tft.println("SET RPM");
delay(500);
erase();
drawRect("SCREEN","POTENTIOMETER",88,40,2);
while(!ts.touched())
delay(300);
TS_Point p = ts.getPoint();
p.y = map(p.y, 0, 320, 320, 0);
if(p.y<=160)
newDelay = inputRpmUsingScreen(rpm);
else{
erase();
tft.setTextSize(3);
tft.setTextColor(WHITE);
tft.setCursor(4,4);
tft.println("RPM: ");
tft.fillRect(0,160,240,160,WHITE);
tft.setTextColor(BLACK);
tft.setCursor(120,240);
tft.println(">");
while(!ts.touched()){
int newRpm = map(analogRead(A0), 0, 1023, 1, 125);
newDelay=9375/rpm;
tft.setTextColor(WHITE,BLACK);
if(newRpm!=rpm){
tft.setCursor(4,32);
tft.fillRect(4,32,240,32,BLACK);
rpm=newRpm;
tft.print(rpm);
}
}
}
return newDelay;
}
void rotateByDegree(double degree, int newDelay)
{
for(int i = 0; i < int (degree/0.1125); i++)
{
digitalWrite(stepPin, HIGH);
delayMicroseconds(newDelay);
digitalWrite(stepPin, LOW);
delayMicroseconds(newDelay);
}
}
void rotateContinous(int newDelay)
{
erase();
tft.setCursor(4, 4);
tft.setTextSize(3);
tft.setTextColor(WHITE);
tft.println("RPM: " + String(9375/newDelay)) ;
tft.fillRect(0,160,240,320, WHITE);
tft.setCursor(90, 220);
tft.setTextColor(BLACK);
tft.println("STOP");
while(!ts.touched())
{
digitalWrite(stepPin, HIGH);
delayMicroseconds(newDelay);
digitalWrite(stepPin, LOW);
delayMicroseconds(newDelay);
}
}
void setDir() {
erase();
drawRect("CW", "ACW",108,96);
while(!ts.touched());
TS_Point p = ts.getPoint();
Serial.println(String(p.x) + " " + String(p.y));
p.y = map(p.y, 0, 320, 320, 0);
if(p.y <= 160) {
Serial.println("Upper");
digitalWrite(dirPin, HIGH);
}
else if(p.y > 160){
Serial.println("Bottom");
digitalWrite(dirPin, LOW);
}
erase();
}
void setup() {
pinMode(stepPin, OUTPUT), pinMode(dirPin, OUTPUT);
digitalWrite(dirPin, HIGH), rotateByDegree(3.6, 60);
Serial.begin(9600);
Serial.println("CAM Theory Project");
tft.begin();
// Read Diagnostics:
uint8_t x = tft.readcommand8(ILI9341_RDMODE);
Serial.print("Display Power Mode: 0x"); Serial.println(x, HEX);
x = tft.readcommand8(ILI9341_RDMADCTL);
Serial.print("MADCTL Mode: 0x"); Serial.println(x, HEX);
x = tft.readcommand8(ILI9341_RDPIXFMT);
Serial.print("Pixel Format: 0x"); Serial.println(x, HEX);
x = tft.readcommand8(ILI9341_RDIMGFMT);
Serial.print("Image Format: 0x"); Serial.println(x, HEX);
x = tft.readcommand8(ILI9341_RDSELFDIAG);
Serial.print("Self Diagnostic: 0x"); Serial.println(x, HEX);
if (!ts.begin(0x27)) {
Serial.println("Couldn't start touchscreen controller");
while (1);
}
}
void loop(void) {
erase();
double degree = 0;
int rpm=0;
drawRect("STEP", "CONTINUOUS",88,34);
int newDelay = 0;
while(!ts.touched())
delay(300);
TS_Point p = ts.getPoint();
p.y = map(p.y, 0, 320, 320, 0);
if(p.y<=160){
inputDegree(degree);
newDelay = inputRPM(rpm);
Serial.print("Degree: ");
Serial.println(degree);
Serial.print("RPM: ");
Serial.println(rpm);
setDir();
rotateByDegree(degree, newDelay);
}
else{
newDelay = inputRPM(rpm);
Serial.print("RPM: ");
Serial.println(rpm);
setDir();
rotateContinous(newDelay);
}
}
Loading
ili9341-cap-touch
ili9341-cap-touch