Hallo Deltaflyer,
ich hoffe das Du jetzt mehr nachvollziehen kannst und einen Rat für mich hast.
Gruß Sven
#include <stdint.h>
#include "TouchScreen.h"
#include <Adafruit_GFX.h> // Core graphics library
#include <MCUFRIEND_kbv.h>
#include <Fonts\FreeSans18pt7b.h>
#include <Fonts\FreeSansBold24pt7b.h>
#include <Fonts\FreeSansBold12pt7b.h>
#include <Fonts\FreeSansBold18pt7b.h>
#include <Fonts\FreeSans9pt7b.h>
#include <EEPROM.h>
#include <Servo.h>
SoftwareSerial mySerial(12,3);
Servo servo_3;
MCUFRIEND_kbv tft;
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
#define MINPRESSURE 10
#define MAXPRESSURE 1000
#define DARKGREEN 0x1420
#define DARKRED 0x8000
int startScan=2;
int waitUntil=1;
int waitUntil_2=1;
int waitUntil_3=1;
int waitUntil_4=1;
int waitUntil_5=1;
int waitUntil_6=1;
int amountCount=2;
bool darkCheck=false;
uint16_t ID;
uint8_t YP = A3; // must be an analog pin, use "An" notation!
uint8_t XM = A2; // must be an analog pin, use "An" notation!
uint8_t YM = 9; // can be a digital pin
uint8_t XP = 8; // can be a digital pin
uint16_t pLEFT = 950;
uint16_t pRIGHT = 170;
uint16_t pTOP = 200;
uint16_t pBOTTOM = 890;
//uint16_t pLEFT = 880;
//uint16_t pRIGHT = 170;
//uint16_t pTOP = 180;
//uint16_t pBOTTOM = 950;
int v1;
uint16_t xpos, ypos;
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
uint16_t read16(File f) {
uint16_t result;
((uint8_t *)&result)[0] = f.read(); // LSB
((uint8_t *)&result)[1] = f.read(); // MSB
return result;
}
uint32_t read32(File f) {
uint32_t result;
((uint8_t *)&result)[0] = f.read(); // LSB
((uint8_t *)&result)[1] = f.read();
((uint8_t *)&result)[2] = f.read();
((uint8_t *)&result)[3] = f.read(); // MSB
return result;
}
void setup() {
digitalWrite(13, LOW);
Serial.begin(9600);
Serial1.begin(9600);
tft.begin(0x9486);
Serial.print(" size is "); Serial.print(tft.width()); Serial.print("x"); Serial.println(tft.height());
tft.setRotation(1);
bootScreen();
}
#define BUFFPIXEL 20
void bootScreen(){
tft.fillScreen(WHITE);
if (EEPROM.read(0) == 1) {
tft.invertDisplay(false);
darkCheck=false;
}
else if (EEPROM.read(0) == 2) {
tft.invertDisplay(true);
darkCheck=true;
}
tft.setFont(&FreeSansBold18pt7b);
tft.setCursor(0,50);
tft.setTextColor(BLACK);
tft.print("DIY Candy Vending Machine");
tft.setFont(&FreeSansBold12pt7b);
tft.setCursor(0,310);
tft.print("Made by Kacze");
tft.setCursor(330,310);
tft.print("1.0 BETA");
delay(100);
homeScreen();
}
void homeScreen(){
startScan=2;
waitUntil=1;
waitUntil_4=1;
waitUntil_5=1;
v1 = random(0, 2);
//tft.invertDisplay(true);
tft.fillScreen(WHITE);
tft.setFont(&FreeSansBold24pt7b);
tft.setTextColor(BLACK);
tft.setCursor(185,65);
tft.print("Hello!");
tft.drawLine(20, 85, 460, 85, BLACK);
tft.fillRoundRect(195, 100, 100, 100, 20, BLACK);
tft.fillRoundRect(202, 107, 86, 86, 15, WHITE);
tft.fillTriangle(220, 180, 220, 120, 270, 150, BLACK);
tft.fillRoundRect(5, 280, 73, 30, 5, BLACK);
tft.fillRoundRect(7, 281, 69, 26, 3, WHITE);
tft.setFont(&FreeSans9pt7b);
tft.setCursor(10,300);
tft.print("Options");
tft.setFont(&FreeSans9pt7b);
tft.setCursor(340,310);
tft.print("Made by Kacze");
playTouch();
}
void playTouch(){
do {
TSPoint p = ts.getPoint();
xpos = map(p.y, pLEFT, pRIGHT, 0, 320);
ypos = map(p.x, pTOP, pBOTTOM, 0, 480);
if ( p.z > MINPRESSURE && p.z < MAXPRESSURE && xpos<70 && xpos>0 && ypos>0 && ypos<30 ){
highlightOptions();
}
if ( p.z > MINPRESSURE && p.z < MAXPRESSURE && xpos<280 && xpos>180 && ypos>120 && ypos<220 ){
highlightPlay();
}
}
while (waitUntil<2);
}
void highlightOptions(){
waitUntil=3;
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
tft.fillRoundRect(5, 280, 73, 30, 5, GREEN);
tft.fillRoundRect(7, 281, 69, 26, 3, WHITE);
tft.setFont(&FreeSans9pt7b);
tft.setTextColor(GREEN);
tft.setCursor(10,300);
tft.print("Options");
delay(100);
waitUntil=1;
optionScreen();
}
void optionScreen(){
tft.fillScreen(WHITE);
//dark mode
tft.drawLine(80, 125, 380, 125, BLACK);
tft.drawLine(80, 55, 380, 55, BLACK);
tft.setFont(&FreeSansBold18pt7b);
tft.setTextColor(BLACK);
tft.setCursor(95,105);
tft.print("Dark Mode:");
tft.fillRoundRect(305, 71, 70, 40, 7, BLACK);
tft.fillRoundRect(309, 75, 62, 32, 4, WHITE);
tft.setFont(&FreeSans9pt7b);
if (darkCheck==false){
tft.setCursor(320,95);
tft.print("OFF");
}
else {
tft.setCursor(324,95);
tft.print("ON");
}
//back button
tft.fillRoundRect(5, 280, 73, 30, 5, BLACK);
tft.fillRoundRect(7, 281, 69, 26, 3, WHITE);
tft.setFont(&FreeSans9pt7b);
tft.setCursor(14,300);
tft.print("Back");
optionTouch();
}
void optionTouch(){
pinMode(XM, INPUT);
pinMode(YP, INPUT);
do {
TSPoint p = ts.getPoint();
xpos = map(p.y, pLEFT, pRIGHT, 0, 320);
ypos = map(p.x, pTOP, pBOTTOM, 0, 480);
if( p.z > MINPRESSURE && p.z < MAXPRESSURE && xpos<70 && xpos>0 && ypos>0 && ypos<30 ){
highlightBack();
}
else if ( p.z > MINPRESSURE && p.z < MAXPRESSURE && xpos<300 && xpos>250 && ypos>315 && ypos<345 ){
highlightDarkMode();
}
}
while (waitUntil_5<2);
}
void highlightBack(){
waitUntil_5=3;
waitUntil_6=3;
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
tft.fillRoundRect(5, 280, 73, 30, 5, GREEN);
tft.fillRoundRect(7, 281, 69, 26, 3, WHITE);
tft.setFont(&FreeSans9pt7b);
tft.setTextColor(GREEN);
tft.setCursor(14,300);
tft.print("Back");
delay(100);
homeScreen();
}
void highlightDarkMode(){
waitUntil_5=3;
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
if (darkCheck==true){
tft.fillRoundRect(305, 71, 70, 40, 7, GREEN);
tft.fillRoundRect(309, 75, 62, 32, 4, WHITE);
tft.setCursor(320,95);
tft.setTextColor(GREEN);
tft.print("OFF");
delay(100);
tft.fillRoundRect(305, 71, 70, 40, 7, BLACK);
tft.fillRoundRect(309, 75, 62, 32, 4, WHITE);
tft.setTextColor(BLACK);
tft.setCursor(320,95);
tft.print("OFF");
EEPROM.write(0, 1);
tft.invertDisplay(false);
darkCheck=false;
}
else {
tft.fillRoundRect(305, 71,70, 40, 7, GREEN);
tft.fillRoundRect(309, 75, 62, 32, 4, WHITE);
tft.setCursor(324,95);
tft.setTextColor(GREEN);
tft.print("ON");
delay(100);
tft.fillRoundRect(305, 71,70, 40, 7, BLACK);
tft.fillRoundRect(309, 75, 62, 32, 4, WHITE);
tft.setTextColor(BLACK);
tft.setCursor(324,95);
tft.print("ON");
tft.invertDisplay(true);
EEPROM.write(0, 2);
darkCheck=true;
}
waitUntil_5=1;
optionTouch();
}
void highlightAgain(){
waitUntil_2=3;
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
tft.fillRoundRect(150, 100, 100, 100, 20, GREEN);
tft.fillRoundRect(157, 107, 86, 86, 15, WHITE);
tft.fillCircle(200, 150, 30, GREEN);
tft.fillCircle(200, 150, 22, WHITE);
tft.fillRect(162, 112, 40, 40, WHITE);
tft.fillTriangle(185, 125, 201, 115, 201, 135, GREEN);
delay(100);
homeScreen();
}
void highlightPlay(){
waitUntil=3;
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
tft.setCursor(0,0);
tft.fillRoundRect(195, 100, 100, 100, 20, GREEN);
tft.fillRoundRect(202, 107, 86, 86, 15, WHITE);
tft.fillTriangle(220, 180, 220, 120, 270, 150, GREEN);
delay(100);
amountPicker();
}
void amountPicker(){
tft.fillScreen(WHITE);
tft.setFont(&FreeSansBold18pt7b);
tft.setTextColor(BLACK);
tft.setCursor(95,65);
tft.print("Choose Amount:");
tft.drawLine(80, 85, 380, 85, BLACK);
//- button
tft.fillRoundRect(55,105,80,80,20,BLACK);
tft.fillRoundRect(62,112,66,66,15,WHITE);
//+ button
tft.fillRoundRect(335,105,80,80,20,BLACK);
tft.fillRoundRect(342,112,66,66,15,WHITE);
//mid button
tft.fillRoundRect(140,105,190,80,20,BLACK);
tft.fillRoundRect(147,112,176,66,15,WHITE);
//X button
tft.fillRoundRect(153,190,80,40,20,BLACK);
tft.fillRoundRect(158,195,70,30,15,WHITE);
//v button
tft.fillRoundRect(238,190,80,40,20,BLACK);
tft.fillRoundRect(243,195,70,30,15,WHITE);
// v icon
tft.drawLine(274,220,284,200,DARKGREEN);
tft.drawLine(273,220,283,200,DARKGREEN);
tft.drawLine(275,220,285,200,DARKGREEN);
tft.drawLine(274,220,267,214,DARKGREEN);
tft.drawLine(275,220,268,214,DARKGREEN);
tft.drawLine(276,220,269,214,DARKGREEN);
//X icon
tft.drawLine(178+4,200,208-4,220,DARKRED);
tft.drawLine(179+4,200,209-4,220,DARKRED);
tft.drawLine(177+4,200,207-4,220,DARKRED);
tft.drawLine(208-4,200,178+4,220,DARKRED);
tft.drawLine(207-4,200,177+4,220,DARKRED);
tft.drawLine(209-4,200,179+4,220,DARKRED);
//- icon
tft.drawLine(72,145,118,145,BLACK);
tft.drawLine(72,144,118,144,BLACK);
tft.drawLine(72,146,118,146,BLACK);
//+ icon
tft.drawLine(352,145,398,145,BLACK);
tft.drawLine(352,144,398,144,BLACK);
tft.drawLine(352,146,398,146,BLACK);
tft.drawLine(375,122,375,168,BLACK);
tft.drawLine(374,122,374,168,BLACK);
tft.drawLine(376,122,376,168,BLACK);
tft.setFont(&FreeSansBold12pt7b);
tft.setCursor(185,153);
tft.setTextColor(BLACK);
tft.print("Not Alot");
waitUntil_3=1;
amountTouch();
}
void amountTouch(){
pinMode(XM, INPUT);
pinMode(YP, INPUT);
do {
TSPoint p = ts.getPoint();
xpos = map(p.y, pLEFT, pRIGHT, 0, 320);
ypos = map(p.x, pTOP, pBOTTOM, 0, 480);
if ( p.z > MINPRESSURE && p.z < MAXPRESSURE && xpos<360 && xpos>280 && ypos>215 && ypos<295 ){
Serial.print("X = "); Serial.print(p.x);
Serial.print("\tY = "); Serial.print(p.y);
Serial.print("\tPressure = "); Serial.println(p.z);
highlightPlus();
}
if ( p.z > MINPRESSURE && p.z < MAXPRESSURE && xpos<100 && xpos>40 && ypos>215 && ypos<295 ){
Serial.print("X = "); Serial.print(p.x);
Serial.print("\tY = "); Serial.print(p.y);
Serial.print("\tPressure = "); Serial.println(p.z);
highlightMinus();
}
if ( p.z > MINPRESSURE && p.z < MAXPRESSURE && xpos<283 && xpos>203 && ypos>120 && ypos<160 ){
Serial.print("X = "); Serial.print(p.x);
Serial.print("\tY = "); Serial.print(p.y);
Serial.print("\tPressure = "); Serial.println(p.z);
highlightVi();
}
if ( p.z > MINPRESSURE && p.z < MAXPRESSURE && xpos<198 && xpos>118 && ypos>120 && ypos<160 ){
Serial.print("X = "); Serial.print(p.x);
Serial.print("\tY = "); Serial.print(p.y);
Serial.print("\tPressure = "); Serial.println(p.z);
highlightX();
}
}
while (waitUntil_3<2);
}
void highlightPlus(){
Serial.print("got to highlightPlus");
waitUntil_3=3;
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
tft.fillRoundRect(335,105,80,80,20,GREEN);
tft.fillRoundRect(342,112,66,66,15,WHITE);
tft.drawLine(352,145,398,145,GREEN);
tft.drawLine(352,144,398,144,GREEN);
tft.drawLine(352,146,398,146,GREEN);
tft.drawLine(375,122,375,168,GREEN);
tft.drawLine(374,122,374,168,GREEN);
tft.drawLine(376,122,376,168,GREEN);
delay(100);
tft.fillRoundRect(335,105,80,80,20,BLACK);
tft.fillRoundRect(342,112,66,66,15,WHITE);
tft.drawLine(352,145,398,145,BLACK);
tft.drawLine(352,144,398,144,BLACK);
tft.drawLine(352,146,398,146,BLACK);
tft.drawLine(375,122,375,168,BLACK);
tft.drawLine(374,122,374,168,BLACK);
tft.drawLine(376,122,376,168,BLACK);
amountCount++;
if (amountCount>3){
amountCount=3;
}
else if (amountCount>=3){
tft.fillRoundRect(145,112,176,66,15,WHITE);
tft.setCursor(182,153);
tft.setTextColor(BLACK);
tft.print("Too Much");
}
else if (amountCount==2){
tft.fillRoundRect(145,112,176,66,15,WHITE);
tft.setCursor(185,153);
tft.setTextColor(BLACK);
tft.print("Not Alot");
}
waitUntil_3=1;
amountTouch();
}
void highlightMinus(){
Serial.print("got to highlightMinus");
waitUntil_3=3;
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
tft.fillRoundRect(55,105,80,80,20,RED);
tft.fillRoundRect(62,112,66,66,15,WHITE);
tft.drawLine(72,145,118,145,RED);
tft.drawLine(72,144,118,144,RED);
tft.drawLine(72,146,118,146,RED);
delay(100);
tft.fillRoundRect(55,105,80,80,20,BLACK);
tft.fillRoundRect(62,112,66,66,15,WHITE);
tft.drawLine(72,145,118,145,BLACK);
tft.drawLine(72,144,118,144,BLACK);
tft.drawLine(72,146,118,146,BLACK);
amountCount--;
if (amountCount<1){
amountCount=1;
}
else if (amountCount<=1){
tft.fillRoundRect(145,112,176,66,15,WHITE);
tft.setCursor(202,153);
tft.setTextColor(BLACK);
tft.print("A Bit");
}
else if (amountCount==2){
tft.fillRoundRect(145,112,176,66,15,WHITE);
tft.setCursor(185,153);
tft.setTextColor(BLACK);
tft.print("Not Alot");
}
waitUntil_3=1;
amountTouch();
}
void highlightX(){
Serial.print("got to highlightX");
waitUntil_3=3;
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
tft.fillRoundRect(153,190,80,40,20,RED);
tft.fillRoundRect(158,195,70,30,15,WHITE);
tft.drawLine(178+4,200,208-4,220,DARKRED);
tft.drawLine(179+4,200,209-4,220,DARKRED);
tft.drawLine(177+4,200,207-4,220,DARKRED);
tft.drawLine(208-4,200,178+4,220,DARKRED);
tft.drawLine(207-4,200,177+4,220,DARKRED);
tft.drawLine(209-4,200,179+4,220,DARKRED);
delay(100);
//back to defult state
amountCount=2;
//back to home screen
homeScreen();
}
void highlightVi(){
waitUntil_3=3;
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
tft.fillRoundRect(238,190,80,40,20,GREEN);
tft.fillRoundRect(243,195,70,30,15,WHITE);
tft.drawLine(274,220,284,200,GREEN);
tft.drawLine(273,220,283,200,GREEN);
tft.drawLine(275,220,285,200,GREEN);
tft.drawLine(274,220,267,214,GREEN);
tft.drawLine(275,220,268,214,GREEN);
tft.drawLine(276,220,269,214,GREEN);
delay(1000);
endScreen();
}
void endScreen(){
servo_3.attach(49);
if (amountCount==3){
for (int k=100;k>=30 ;k--){
servo_3.write(k);
delay (3);
}
//delay(200);
for (int i=30; i<=100 ;i++){
servo_3.write(i);
delay (3);
}
}
else if (amountCount==2){
for (int k=100;k>=40 ;k--){
servo_3.write(k);
delay (3);
}
delay(100);
for (int i=40; i<=100 ;i++){
servo_3.write(i);
delay (3);
}
}
else if (amountCount==1){
for (int k=100;k>=37;k--){
servo_3.write(k);
delay (2);
}
delay(50);
for (int i=37; i<=100 ;i++){
servo_3.write(i);
delay (2);
}
}
servo_3.detach();
amountCount=2;
int v2 = random(0, 2);
tft.fillScreen(WHITE);
tft.setFont(&FreeSansBold24pt7b);
tft.setTextColor(BLACK);
if (v2 ==0){
tft.setCursor(120,65);
tft.print("Enjoy!");
}
else if (v2 == 1){
tft.setCursor(85,65);
tft.setFont(&FreeSansBold18pt7b);
tft.print("There you go!");
}
tft.drawLine(20, 85, 380, 85, BLACK);
tft.fillRoundRect(150, 100, 100, 100, 20, BLACK);
tft.fillRoundRect(157, 107, 86, 86, 15, WHITE);
tft.fillTriangle(200, 117, 170, 145, 230, 145, BLACK);
tft.fillRoundRect(180, 138, 40, 40, 5, BLACK);
homeTouch();
}
void homeTouch(){
pinMode(XM, INPUT);
pinMode(YP, INPUT);
do {
TSPoint p = ts.getPoint();
xpos = map(p.x, pLEFT, pRIGHT, 0, 400);
ypos = map(p.y, pTOP, pBOTTOM, 0, 240);
if ( p.z > MINPRESSURE && p.z < MAXPRESSURE && xpos<250 && xpos>150 && ypos>100 &&
ypos<200 ){
highlightHome();
}
}
while (waitUntil_4<2);
}
void highlightHome(){
waitUntil_4=3;
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
tft.fillRoundRect(150, 100, 100, 100, 20,GREEN);
tft.fillRoundRect(157, 107, 86, 86, 15, WHITE);
tft.fillTriangle(200, 117, 170, 145, 230, 145, GREEN);
tft.fillRoundRect(180, 135, 40, 43, 10, GREEN);
delay(100);
homeScreen();
}
void loop(){
}