Thank you for your reply; here is my full code.
If I run the getRange() together with the drawDisplay() function in the same loop. The drawDisplay() doesnt work any more and the display starts to flicker and the ESP32 crashes. getRange() and drawDisplay() only work if they are executed by their own but not together! So my best solution is to write my own function to get the information from the TOF sensor...
#include <SPI.h>
#include <TFT_eSPI.h>
#include "images.h"
#include <Wire.h>
#include "Adafruit_VL6180X.h"
TFT_eSPI tft = TFT_eSPI();
TFT_eSprite spr = TFT_eSprite(&tft);
TFT_eSprite spr2 = TFT_eSprite(&tft);
Adafruit_VL6180X vl = Adafruit_VL6180X();
// EXTRA INTEGERS
int screenSize = 240;
int gradientX = 0;
int gradientY = 10;
int sprHeight = 28;
int sprWidth = 184;
int spr2Width = 80;
// just for a loop animation on the display
int x = 0;
int y = 0;
bool growing = true;
int step = 2;
int w = 0;
// just for a loop animation on the display
bool winkelGrow = true;
int pfeilX = 0;
int pfeilStep = 2;
// just some extra lines to draw on display
int crclMid = 129.5;
int lineX1 = crclMid + 41; // dont change!!
int lineY1 = crclMid + 41; // dont change!!
int lineX2 = crclMid + 60;
int lineY2 = crclMid + 60;
// define variables for TOF sensor
const int sensorAddress = 0x29;
#define RESULT__RANGE_VAL 0x062
int range = 0;
// extra colors for display
#define F_N 0x2dc3 // Green
#define F_D 0x0340 // Dark Green
#define F_L 0x5feb // Light Green
#define WHITE 0xffff // White
void setup(void) {
Wire.begin();
Serial.begin(115200);
while (!Serial) {
delay(1);
}
tft.init();
// draw background and green circle
tft.fillScreen(F_N);
tft.fillCircle(screenSize / 2, screenSize / 2, 110, TFT_BLACK);
// draw text with bitmap and other images
tft.drawBitmap(0, 0, bgImg, 240, 240, WHITE);
tft.drawBitmap(129.5, 129.5, bullsEye, 82, 82, WHITE);
// create 2 sprite's
spr.createSprite(sprWidth, sprHeight);
spr2.createSprite(spr2Width, 32);
}
void loop() {
//getRange();
//testCommunication();
//newGetRange();
drawDisplay();
delay(40);
};
// not finished coding!
void newGetRange(){
Wire.beginTransmission(sensorAddress);
Wire.write()
Wire.endTransmission();
Wire.requestFrom(sensorAddress, 2);
if(Wire.available()<=2){
}
}
// doesnt give correct output i want
void testCommunication() {
Wire.beginTransmission(sensorAddress);
Wire.write(0x062); // Registeradresse, von der gelesen werden soll
Wire.endTransmission();
Wire.requestFrom(sensorAddress, 2); // Anfordern von 2 Bytes
if (Wire.available() == 2) {
uint8_t highByte = Wire.read();
uint8_t lowByte = Wire.read();
uint16_t data = (highByte << 8) | lowByte;
Serial.print("Gelesene Daten: ");
Serial.println(data);
} else {
Serial.println("Fehler beim Lesen der Daten");
}
//delay(1000); // Warte 1 Sekunde bis zur nächsten AbfrageF
}
//MAKES THE DISPLAY FLICKER AND CRASH ESP32!!!
void getRange() {
range = vl.readRange();
Serial.println(range);
}
// draws display
void drawDisplay() {
// clear the sprite
spr.fillSprite(TFT_BLACK);
spr2.fillSprite(TFT_BLACK);
if (growing) {
w += step;
if (w >= sprWidth - 4) {
growing = false;
}
} else {
w -= step;
if (w <= 0) {
growing = true;
}
}
if (winkelGrow) {
pfeilX += pfeilStep;
if (pfeilX >= spr2Width - 12) {
winkelGrow = false;
}
} else {
pfeilX -= pfeilStep;
if (pfeilX <= 0) {
winkelGrow = true;
}
}
spr.fillRoundRect(0, 0, sprWidth, sprHeight, sprHeight / 2, WHITE);
spr.fillRoundRect(3, 3, sprWidth - 6, sprHeight - 6, (sprHeight - 6) / 2, TFT_BLACK);
if (w >= 170) {
spr.fillRoundRect(x + 3, y + 3, w - 3, sprHeight - 6, (sprHeight - 4) / 2, TFT_RED);
} else {
spr.fillRoundRect(x + 3, y + 3, w - 3, sprHeight - 6, (sprHeight - 4) / 2, F_N);
}
// extra Linie fuer indicator
tft.drawLine(lineX1, lineY1, lineX2, lineY2, WHITE);
tft.drawLine(lineX1 + 1, lineY1, lineX2 + 1, lineY2, WHITE);
tft.drawLine(lineX1 - 1, lineY1, lineX2 - 1, lineY2, WHITE);
tft.drawLine(lineX1, lineY1 + 1, lineX2, lineY2 + 1, WHITE);
tft.drawLine(lineX1, lineY1 - 1, lineX2, lineY2 - 1, WHITE);
spr2.drawBitmap(pfeilX, 0, pfeile, 12, 36, WHITE);
spr2.fillRoundRect(0, 10, 25, 12, 0, 63488);
spr2.fillRoundRect(25, 10, 30, 12, 0, 63488);
spr2.fillRoundRect(25 + 30, 10, 25, 12, 0, F_N);
spr.pushSprite(28, 95);
spr2.pushSprite(43, 159);
}