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!!