Untitled
unknown
plain_text
3 years ago
8.2 kB
9
Indexable
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP085.h>
#include "Adafruit_ILI9341.h"
#include <DHT.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <XPT2046_Touchscreen.h>
#define DHTTYPE DHT11 // DHT 11
#define DHTPIN 2 // what pin we're connected to(DHT10 and DHT20 don't need define it)
DHT dht(DHTPIN, DHTTYPE); // DHT11 DHT21 DHT22
Adafruit_BMP085 bmp;
// For the Adafruit shield, these are the default.
#define CS_PIN 7
#define TFT_DC 9
#define TFT_CS 10
#define TFT_RST 8
// Use hardware SPI (on Uno, #13, #12, #11) and the above for CS/DC
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
//XPT2046_Touchscreen ts(CS_PIN);
XPT2046_Touchscreen ts(CS_PIN);
// Screen Calibration Values
// calibration values
float xCalM = 0.09, yCalM = 0.07; // gradients
float xCalC = -33.62, yCalC = -14.70; // y axis crossing points
int8_t blockWidth = 20; // block size
int8_t blockHeight = 20;
int16_t blockX = 0, blockY = 0; // block position (pixels)
class ScreenPoint {
public:
int16_t x;
int16_t y;
// default constructor
ScreenPoint(){
}
ScreenPoint(int16_t xIn, int16_t yIn){
x = xIn;
y = yIn;
}
};
ScreenPoint getScreenCoords(int16_t x, int16_t y){
int16_t xCoord = round((x * xCalM) + xCalC);
int16_t yCoord = round((y * yCalM) + yCalC);
if(xCoord < 0) xCoord = 0;
if(xCoord >= tft.width()) xCoord = tft.width() - 1;
if(yCoord < 0) yCoord = 0;
if(yCoord >= tft.height()) yCoord = tft.height() - 1;
return(ScreenPoint(xCoord, yCoord));
}
// Data wire is plugged into pin 6 on the Arduino
#define ONE_WIRE_BUS 6
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
//define TFT quadrant position on screen
#define tl 0,0
#define tr 0,10
#define bl 0,20
#define br 0,20
//###############################
//### Debug Mode ###############
//##############################
//Change between Serial and display output
//Enter 1 for Debug, 0 for normal display output
#define DEBUG 0
#if DEBUG == 1
#define output Serial
#else
#define output tft
#endif
//###############################
//### End Debug Mode ###############
//##############################
int backLight = 4;
unsigned long lastFrame = millis();
void setup() {
digitalWrite(backLight, LOW);
pinMode(backLight, OUTPUT);
Serial.begin(9600);
// set backlight pin
// deselect all SPI devices
pinMode(10, OUTPUT);
pinMode(7, OUTPUT);
digitalWrite(10, HIGH);
digitalWrite(7, HIGH);
Wire.begin();
tft.begin();
tft.fillScreen(ILI9341_BLACK);
ts.begin();
displayRotation();
tft.setTextColor(ILI9341_WHITE);
digitalWrite(backLight, HIGH);
delay(100);
//display initialization message
initialize();
// //Start Temp/Humidity Sensor
// bool success1 = dht.begin();
// if (success1) {
// output.println("DHT Sensor init success");
// delay(1000);
// } else if (!dht.begin()) {
// output.println("Could not find a valid DHT sensor, check wiring!");
// }
// //Start external Temp Probe Sensor
// bool success2 = oneWire.begin();
// if (success2) {
// output.println("Temperature Probe Sensor init success");
// delay(1000);
// } else if (!oneWire.begin()) {
// output.println("Could not find a valid Temperature Probe sensor, check wiring!");
// }
//initialize the bmp180 temperature sensor
bool success = bmp.begin();
if (success) {
output.println("BMP180 init success");
delay(1000);
} else if (!bmp.begin()) {
output.println("Could not find a valid BMP180 sensor, check wiring!");
delay(1000);
}
//output to Serial that setup completed
Serial.println("Initialization Complete!");
// Clear the buffer
tft.fillScreen(ILI9341_BLACK);
tft.fillScreen(ILI9341_BLACK);
delay(2000);
tft.fillScreen(ILI9341_BLACK);
//this is mostly for Serial output
output.println();
}
void loop() {
ScreenPoint sp;
//set grid layout
tft.drawFastHLine(0,120,320,ILI9341_GREEN);
tft.drawFastVLine(160,0,240,ILI9341_GREEN);
if (ts.touched() ) {
TS_Point p = ts.getPoint();
sp = getScreenCoords(p.x, p.y);
Serial.print("Pressure = ");
Serial.print(p.z);
Serial.print(", x = ");
Serial.print(p.x);
Serial.print(", y = ");
Serial.print(p.y);
Serial.println();
}
//output current temp reading
getTemp();
//output current pressure reading
getPress();
//output current humidity reading
getHumidity();
// tempProbe();
// limit frame rate
while((millis() - lastFrame) < 20);
lastFrame = millis();
if (ts.touched()) {
moveBlock();
}
}
// get current temperature from temp probe
double tempProbe() {
double result;
if (!sensors.getTempCByIndex(0) ) {
displaySettings(190,150,2);
output.println("Probe: ");
displaySettings(190,200,3);
output.print("N/A");
} else {
displaySettings(190,150,2);
output.println("Probe: ");
displaySettings(190,200,3);
sensors.requestTemperatures();
output.print(sensors.getTempCByIndex(0) * .98);
output.println("*C");
}
return result;
}
//get current humidity
float getHumidity() {
float temp_hum_val[2] = {0};
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
if (!dht.readTempAndHumidity(temp_hum_val)) {
//displaySettings(bottom);
displaySettings(190,20,2);
output.print("Humidity: ");
displaySettings(190,70,3);
output.print(temp_hum_val[0] * 1.055);
output.print("%");
// delay(1000);
} else {
output.println("Failed to get humidity value.");
}
return temp_hum_val[0];
}
//get current temperature
float getTemp() {
float readTemp;
float t = dht.readTemperature();
//read temperature sensor
if (bmp.readTemperature()) {
//displaySettings(top);
displaySettings(50,20,2);
output.println("Temp: ");
displaySettings(15,70,3);
output.print(bmp.readTemperature() / 1.032);
output.println("*C");
} else if (!bmp.readTemperature()){
displaySettings(50,20,2);
output.println("Temp: ");
displaySettings(15,70,3);
output.print(t);
output.println("*C");
} else {
displaySettings(50,20,2);
output.print("Temp: ");
displaySettings(15,70,2);
output.println ("Unav");
}
return readTemp;
}
// //get current pressure
double getPress() {
double readPress;
//read pressure sensor
if (bmp.readPressure()) {
//displaySettings(middle);
displaySettings(25,150,2);
output.print("Pressure:");
displaySettings(45,200,3);
output.print(bmp.readPressure() / 1000);
output.println("KPA");
} else {
output.println ("Press Unavail");
}
return readPress;
}
//TFT display settings
void displaySettings(int x,int y,int z){
tft.setTextSize(z);
tft.setTextColor(ILI9341_BLUE,ILI9341_BLACK);
tft.setCursor(x,y);
}
//set TFT display rotation
void displayRotation(){
for(uint8_t rotation=0; rotation<4; rotation++) {
tft.setRotation(rotation);
ts.setRotation(4); }
}
//initialize message
void initialize(){
output.print("Initializing");
delay(800);
output.print(".");
delay(800);
output.print(".");
delay(800);
output.println(".");
delay(800);
output.println("Initialization Done!");
delay(1000);
}
void moveBlock(){
int16_t newBlockX, newBlockY;
ScreenPoint sp = ScreenPoint();
if (ts.touched()) {
TS_Point p = ts.getPoint();
sp = getScreenCoords(p.x, p.y);
newBlockX = sp.x - (blockWidth / 2);
newBlockY = sp.y - (blockHeight / 2);
if (newBlockX < 0) newBlockX = 0;
if (newBlockX >= (tft.width() - blockWidth)) newBlockX = tft.width() - 1 - blockWidth;
if (newBlockY < 0) newBlockY = 0;
if (newBlockY >= (tft.height() - blockHeight)) newBlockY = tft.height() - 1 - blockHeight;
}
if ((abs(newBlockX - blockX) > 2) || (abs(newBlockY - blockY) > 2)){
tft.fillRect(blockX, blockY, blockWidth, blockHeight,ILI9341_BLACK);
blockX = newBlockX;
blockY = newBlockY;
tft.fillRect(blockX, blockY, blockWidth, blockHeight,ILI9341_RED);
}
}
Editor is loading...