[SOLVED] Problem with Waveshare 3.5 touchscreen and Supersonic sensor HC-SR04

Hi!
I am working on rendering automatic some functions of my Gaggia Classic espresso machine and it was coming along really nice until now.
I am currently trying to display the distance between the top of the water tank and the water level, so that i can calculate the remaining water volume and display it on the touchscreen along with other parameters.
I decided to use a supersonic module HC-SR04 situated on top of the water tank but i can/t make it work along with my TFT screen.

This is the code I use.

// Include Arduino libraries
#include <Arduino.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Wire.h>
#include <Waveshare_ILI9486.h>
#include <XPT2046_Touchscreen.h>

// Assign human-readable names to some common 16-bit color values:
#define BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF

// Pins
// Touchscreen
#define CS_PIN  4  // MOSI=11, MISO=12, SCK=13
//XPT2046_Touchscreen ts(CS_PIN);
#define TIRQ_PIN  3  //XPT2046_Touchscreen ts(CS_PIN);  // Param 2 - NULL - No interrupts
//XPT2046_Touchscreen ts(CS_PIN, 255);  // Param 2 - 255 - No interrupts

// Ultrasonic sensor
#define trigPin 52 // Sets the trigPin as an OUTPUT
#define echoPin 53

// CONSTANTS
//Touchscreen TFT
uint16_t screenColor = BLACK;
uint16_t textForeColor = WHITE;
uint16_t textBackColor = BLACK;


//Ultrasonic sensor
float duration, distance;
int pingIterations = 30;
float waterLeft = 0;
float waterAlarmLevel = 0.3;
bool WaterAlarmStatus = false;
int MAX_DISTANCE = 400; // Maximum distance we want to ping for (in centimeters).

// Constructors
Waveshare_ILI9486 Waveshield; // TFT screen
Adafruit_GFX &tft = Waveshield;  //  TFT screen
XPT2046_Touchscreen ts(CS_PIN, TIRQ_PIN);  // TFT touchscreen

namespace
{
Waveshare_ILI9486 tft;
}



void setup() {
  //Initialize sensors
  SPI.begin();         // TFT, SPI controller
  tft.begin();          // TFT
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
  pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
  Serial.begin(9600);
}

void PrintOnTFT() {
  //Displays distance on tft
  tft.setRotation(1);
  tft.setTextSize(2);
  tft.setCursor(100, 100);
  tft.print("Distance: ");
  tft.print(distance);
  tft.println(" cm");
}



void Supersonic() {
  // Clears the trigPin condition
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the trigPin HIGH (ACTIVE) for 10 microseconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);
  // Calculating the distance
  distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)
  // Displays the distance on the Serial Monitor
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");
  delay(500);
}


void loop() {
  //Supersonic();
  PrintOnTFT();
}

Every time I delete the lines 'SPI.begin();', 'Waveshield.begin();' and the PrintOnTFT(); function the sensor works flawlessly.

Any ideas?

Thank you very much for your time!!

Pretty colors, but unreadable. Can you post code as described in the forum guidelines so us geezers can read it?

What Arduino board are you using? On a Mega the SPI pins are MOSI = 51, MISO = 50, SCK = 52, default CS = 53. What pins are you using for SPI? What pins are you using for the ultrasonic sensor.

Thank you very much!!! You were absolutely right!!!
I am using a Mega2560.
As you suspected, I was using pins 52 e 53 for the ultrasonic sensor and , after I read your link to the SPI library, I found out that these pins are reserved for the SCK and CS!
I changed them to 36 and 37 and now everything works like a charm!!!
Thank you very much!!

PS: I posted again the code again with </> as you told me! Sorry, i read the forum rules but I couldn't do it from my cellphone!

If, in the future, you want to post from a cell phone just put

[code]

before the start of the code and

 [/code]

at the end.

So
[code] your code here [/code] turns into

 your code here