Hello Arduino Forum,
I have a problem (shown in the picture) with m project. The Speed-function works fine but putting it on the screen is a big problem right now.
Maybe someone would be so kind and help me
#include <Elegoo_GFX.h>
#include <Elegoo_TFTLCD.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <TouchScreen.h>
#if defined(__SAM3X8E__)
#undef __FlashStringHelper::F(string_literal)
#define F(string_literal) string_literal
#endif
//Bildschirmpins
#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
#define LCD_RESET A4
//Farbendefinition
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
//Touchscreen
#define YP A3
#define XM A2
#define YM 9
#define XP 8
#define TS_MINX 150
#define TS_MINY 120
#define TS_MAXX 920
#define TS_MAXY 940
#define MINPRESSURE 240
#define MAXPRESSURE 1000
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
//Reed-Sensor
#define reed A14
Elegoo_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
//Berechnungen
//Rad Radius ca. 13,5 Zoll
//Umfang ca. 2*r*PI = 85 Zoll
//Maximal Geschwindigkeit von 56,327 km/h
//Rotationen per Minute 7,25
//Speichervariablen
const float radius = 13.5;
const float circumference = 2 * 3.14 * radius;
const int maxReedCounter = 100; //minimale Zeit (in ms) von einer Umdrehung
volatile int reedCounter = maxReedCounter;
volatile long timer = 0; //Zeit zwischen einer ganzen Rotation
volatile float mph = 0.00;
volatile int kph;
volatile int rpm;
unsigned long draw_begin()
{
unsigned long start = micros();
tft.setRotation(3);
tft.setCursor(0, 0);
tft.setTextColor(WHITE);
tft.setTextSize(3);
tft.println("Willkommen");
tft.setCursor(0, 40);
tft.setTextSize(1);
tft.setTextColor(RED);
tft.println("Speedometer");
tft.setTextColor(WHITE);
tft.setCursor(0, 70);
tft.println("made by");
tft.setCursor(0, 100);
tft.println("NAME");
return micros() - start;
}
unsigned long draw_main()
{
tft.setRotation(3);
unsigned long start = micros();
int kphCopy, rpmCopy;
cli();
kphCopy = kph;
rpmCopy = rpm;
sei();
tft.setCursor(8, 20);
tft.setTextColor(RED);
tft.setTextSize(2.5);
tft.println("Speed km/h");
tft.setCursor(8, 50);
tft.setTextColor(WHITE);
tft.setTextSize(4);
tft.print(kph);
tft.setCursor(8, 120);
tft.setTextColor(RED);
tft.setTextSize(2.5);
tft.println("Rotation per min");
tft.setCursor(8, 120);
tft.setTextColor(WHITE);
tft.setTextSize(4);
tft.setCursor(8, 150);
tft.print(rpm);
//Create Red Button
tft.fillRect(60,180, 200, 40, RED);
tft.drawRect(60,180,200,40,WHITE);
tft.setCursor(80,188);
tft.setTextColor(WHITE);
tft.setTextSize(3);
tft.print("Weiter!");
return micros() - start;
}
unsigned long draw_second()
{
tft.setRotation(3);
tft.setTextColor(RED);
tft.setTextSize(2.5);
tft.println("Test1");
}
void setup(void)
{
//Vom LCD-Bildschirm Hersteller vorgegebene Syntax
Serial.begin(9600);
Serial.println(F("TFT LCD test"));
#ifdef USE_Elegoo_SHIELD_PINOUT
Serial.println(F("Using Elegoo 2.8\" TFT Arduino Shield Pinout"));
#else
Serial.println(F("Using Elegoo 2.8\" TFT Breakout Board Pinout"));
#endif
Serial.print("TFT size is "); Serial.print(tft.width()); Serial.print("x"); Serial.println(tft.height());
tft.fillScreen(BLACK);
uint16_t identifier = tft.readID();
identifier=0x9341;
if(identifier == 0x9325) {
Serial.println(F("Found ILI9325 LCD driver"));
} else if(identifier == 0x9328) {
Serial.println(F("Found ILI9328 LCD driver"));
} else if(identifier == 0x7575) {
Serial.println(F("Found HX8347G LCD driver"));
} else if(identifier == 0x9341) {
Serial.println(F("Found ILI9341 LCD driver"));
} else if(identifier == 0x8357) {
Serial.println(F("Found HX8357D LCD driver"));
} else {
Serial.print(F("Unknown LCD driver chip: "));
Serial.println(identifier, HEX);
Serial.println(F("If using the Elegoo 2.8\" TFT Arduino shield, the line:"));
Serial.println(F(" #define USE_Elegoo_SHIELD_PINOUT"));
Serial.println(F("should appear in the library header (Elegoo_TFT.h)."));
Serial.println(F("If using the breakout board, it should NOT be #defined!"));
Serial.println(F("Also if using the breakout, double-check that all wiring"));
Serial.println(F("matches the tutorial."));
return;
}
tft.begin(identifier);
Serial.println(F("Done!"));
Serial.println(F("Benchmark Time (microseconds)"));
Serial.print(F("Screen fill "));
Serial.println(draw_begin());
delay(2000);
Serial.print(F("Text "));
Serial.println(draw_main());
//delay(3000);
pinMode(reed, INPUT);
//Interrupts
cli();
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
OCR1A = 1999;
TCCR1B |= (1 << WGM12);
TCCR1B |= (1 << CS11);
TIMSK1 |= (1 << OCIE1A);
sei();
}
ISR(TIMER1_COMPA_vect) {
if (digitalRead(reed))
{
if (reedCounter == 0)
{
mph = 56.8 * circumference / float(timer);
rpm = 60000.0 / (float(timer));
timer = 0;
reedCounter = maxReedCounter;
kph = mph * 1.609344;
}
}
else
{
if (reedCounter > 0)
{
reedCounter -= 1;
}
}
if (timer > 1000)
{
mph = 0;
kph = 0;
rpm = 0;
}
else
{
timer += 1;//increment timer
}
}
void loop()
{
TSPoint p=ts.getPoint();
if(p.z > ts.pressureThreshhold)
{
p.x=map(p.x, TS_MAXX, TS_MINX, 0,320);
p.y=map(p.y, TS_MAXY, TS_MINY, 0,240);
if(p.x>60 && p.x<260 && p.y>180 && p.y<220)
{
//This is important, because the libraries are sharing pins
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
tft.fillScreen(BLACK);
draw_second();
}
delay(10);
}
}