이번에 아두이노 메가를 사용하여 온도센서와 기압센서 2개를 사용하고 있습니다.
이 센서값을 TFT LCD에 출력을 하고 싶은데 출력이 되지 않습니다.
명령어 문제인지 아니면 다른 문제인지 궁금합니다.
숫자가 압력부분에서는 000으로 값을 읽지 못하고, 온도부분에서는 오류가 발생하여 이상한 값이 나옵니다.
#include <UTFT.h>
#include <UTouch.h>
#include <HP20x_dev.h>
#include "Arduino.h"
#include "Wire.h"
#include <KalmanFilter.h>
#include <OneWire.h>
unsigned char ret = 0;
int DS18S20_Pin = 8;
OneWire ds(DS18S20_Pin);
/* Instance */
KalmanFilter t_filter; //temperature filter
KalmanFilter p_filter; //pressure filter
KalmanFilter a_filter; //altitude filter
//==== Creating Objects
UTFT myGLCD(ILI9327,38,39,40,41); //Parameters should be adjusted to your Display/Schield model
UTouch myTouch( 6, 5, 4, 3, 2);
//==== Defining Variables
extern uint8_t SmallFont[];
extern uint8_t BigFont[];
extern uint8_t SevenSegNumFont[];
extern unsigned int bird01[0x41A];
int x, y, i;
char currentPage, selectedUnit;
void setup() {
// Initial setup
myGLCD.InitLCD();
myGLCD.clrScr();
myTouch.InitTouch();
myTouch.setPrecision(PREC_MEDIUM);
drawHomeScreen(); // Draws the Home Screen
currentPage = '0'; // Indicates that we are at Home Screen
selectedUnit = '0'; // Indicates the selected unit for the first example, cms or inches
}
void loop() {
float Temp = getTemp();
// Home Screen
if (currentPage == '0') {
if (myTouch.dataAvailable()) {
myTouch.read();
x=myTouch.getX(); // X coordinate where the screen has been pressed
y=myTouch.getY(); // Y coordinates where the screen has been pressed
// If we press the Distance Sensor Button
if ((x>=35) && (x<=285) && (y>=90) && (y<=130)) {
drawFrame(35, 90, 285, 130); // Custom Function -Highlighs the buttons when it's pressed
currentPage = '1'; // Indicates that we are the first example
myGLCD.clrScr(); // Clears the screen
PressureSensor(); // It is called only once, because in the next iteration of the loop, this above if statement will be false so this funtion won't be called. This function will draw the graphics of the first example.
}
// If we press the RGB Control Button
if ((x>=35) && (x<=285) && (y>=140) && (y<=180)) {
drawFrame(35, 140, 285, 180);
currentPage = '2';
myGLCD.clrScr();
TempSensor();
}
// If we press the Birduino Game Button
if ((x>=35) && (x<=285) && (y>=190) && (y<=230)) {
drawFrame(35, 190, 285, 230);
currentPage = '3';
myGLCD.clrScr();
Result();
}
}
}
// Distance Sensor Example
if (currentPage == '1') {
getPres(); // Gets distance from the sensor and this function is repeatedly called while we are at the first example in order to print the lasest results from the distance sensor
if (myTouch.dataAvailable()) {
myTouch.read();
x=myTouch.getX();
y=myTouch.getY();
// If we press the Back Button
if ((x>=10) && (x<=60) &&(y>=10) && (y<=36)) {
drawFrame(10, 10, 60, 36);
currentPage = '0'; // Indicates we are at home screen
myGLCD.clrScr();
drawHomeScreen(); // Draws the home screen
}
}
}
// RGB LED Control
if (currentPage == '2') {
setTemp();
if (myTouch.dataAvailable()) {
myTouch.read();
x=myTouch.getX();
y=myTouch.getY();
//Back button
if ((x>=10) && (x<=60) &&(y>=10) && (y<=36)) {
drawFrame(10, 10, 60, 36);
currentPage = '0';
myGLCD.clrScr();
drawHomeScreen();
// Turns the LED off
}
}
}
//==== This section of the code, for the game example, is explained in my next tutorial
// Birduino Game
if (currentPage == '3') {
if (myTouch.dataAvailable()) {
myTouch.read();
x=myTouch.getX();
y=myTouch.getY();
if ((x>=10) && (x<=60) &&(y>=10) && (y<=36)) {
drawFrame(10, 10, 60, 36);
currentPage = '0';
myGLCD.clrScr();
drawHomeScreen();
}
}
}
}
/////Temp
float getTemp(){
//returns the temperature from one DS18S20 in DEG Celsius
byte data[12];
byte addr[8];
if ( !ds.search(addr)) {
//no more sensors on chain, reset search
ds.reset_search();
return -1000;
}
if ( OneWire::crc8( addr, 7) != addr[7]) {
return -1000;
}
if ( addr[0] != 0x10 && addr[0] != 0x28) {
return -1000;
}
ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end
byte present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
for (int i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
}
ds.reset_search();
byte MSB = data[1];
byte LSB = data[0];
float tempRead = ((MSB << 8) | LSB); //using two's compliment
float Temp_sum = tempRead / 16;
return Temp_sum;
}
//////Temp
// ====== Custom Funtions ======
// drawHomeScreen - Custom Function
void drawHomeScreen() {
// Title
myGLCD.setBackColor(0,0,0); // Sets the background color of the area where the text will be printed to black
myGLCD.setColor(255, 255, 255); // Sets color to white
myGLCD.setFont(SmallFont); // Sets font to big
myGLCD.print("CBNU Chemical Engineering <Ar 4 Go>", CENTER, 10); // Prints the string on the screen
myGLCD.setColor(255, 0, 0); // Sets color to red
myGLCD.drawLine(0,32,319,32); // Draws the red line
myGLCD.setColor(255, 255, 255); // Sets color to white
myGLCD.setFont(SmallFont);
myGLCD.print("Adulterated", CENTER, 45);
myGLCD.print("Gasoline Detector", CENTER, 60);
// Button - Pressure
myGLCD.setColor(16, 167, 103); // Sets green color
myGLCD.fillRoundRect (35, 90, 285, 130); // Draws filled rounded rectangle
myGLCD.setColor(255, 255, 255); // Sets color to white
myGLCD.drawRoundRect (35, 90, 285, 130); // Draws rounded rectangle without a fill, so the overall appearance of the button looks like it has a frame
myGLCD.setFont(BigFont); // Sets the font to big
myGLCD.setBackColor(16, 167, 103); // Sets the background color of the area where the text will be printed to green, same as the button
myGLCD.print("Pressure", CENTER, 102); // Prints the string
// Button - Temperature
myGLCD.setColor(16, 167, 103);
myGLCD.fillRoundRect (35, 140, 285, 180);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (35, 140, 285, 180);
myGLCD.setFont(BigFont);
myGLCD.setBackColor(16, 167, 103);
myGLCD.print("Temperature", CENTER, 152);
// Button - RESULT
myGLCD.setColor(16, 167, 103);
myGLCD.fillRoundRect (35, 190, 285, 230);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (35, 190, 285, 230);
myGLCD.setFont(BigFont);
myGLCD.setBackColor(16, 167, 103);
myGLCD.print("RESULT", CENTER, 202);
}
// Highlights the button when pressed
void drawFrame(int x1, int y1, int x2, int y2) {
myGLCD.setColor(255, 0, 0);
myGLCD.drawRoundRect (x1, y1, x2, y2);
while (myTouch.dataAvailable())
myTouch.read();
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (x1, y1, x2, y2);
}
//====================================================
void PressureSensor() {
myGLCD.setColor(100, 155, 203);
myGLCD.fillRoundRect (10, 10, 60, 36);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (10, 10, 60, 36);
myGLCD.setFont(BigFont);
myGLCD.setBackColor(100, 155, 203);
myGLCD.print("<-", 18, 15);
myGLCD.setBackColor(0, 0, 0);
myGLCD.setFont(SmallFont);
myGLCD.print("Back to Main Menu", 70, 18);
myGLCD.setFont(BigFont);
myGLCD.print("Vapor Pressure", CENTER, 50);
myGLCD.print("OvO", CENTER, 76);
myGLCD.setColor(255, 0, 0);
myGLCD.drawLine(0,100,319,100);
myGLCD.setFont(BigFont);
myGLCD.print("Pressure:", CENTER, 120);
myGLCD.setBackColor(0, 0, 0);
myGLCD.setFont(SmallFont);
myGLCD.print("Team : Ar 4 Go", CENTER, 220);
}
//====================================================
//===== getDistance - Custom Function
void getPres() {
// Clears the trigPin
HP20x.begin();
long Pressure = HP20x.ReadPressure();
float pres[100];
pres[i] = Pressure/100.0;
myGLCD.setFont(SevenSegNumFont);
myGLCD.setColor(0, 255, 0);
myGLCD.setBackColor(0, 0, 0);
myGLCD.printNumI(pres[i], CENTER, 145, 3, '0');
myGLCD.setFont(BigFont);
myGLCD.print("hPa", 250, 178);
i++;
delay(1000);
}
//====================================================
void TempSensor() {
myGLCD.setColor(100, 155, 203);
myGLCD.fillRoundRect (10, 10, 60, 36);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (10, 10, 60, 36);
myGLCD.setFont(BigFont);
myGLCD.setBackColor(100, 155, 203);
myGLCD.print("<-", 18, 15);
myGLCD.setBackColor(0, 0, 0);
myGLCD.setFont(SmallFont);
myGLCD.print("Back to Main Menu", 70, 18);
myGLCD.setFont(BigFont);
myGLCD.print("Temperature", CENTER, 50);
myGLCD.print("O.O", CENTER, 76);
myGLCD.setColor(255, 0, 0);
myGLCD.drawLine(0,100,319,100);
myGLCD.setFont(BigFont);
myGLCD.print("Temperature:", CENTER, 120);
myGLCD.setBackColor(0, 0, 0);
myGLCD.setFont(SmallFont);
myGLCD.print("Team : Ar 4 Go", CENTER, 220);
}
//====================================================
//============= setLedColor() - Custom Funtion
void setTemp() {
float Temp[100];
myGLCD.setFont(SevenSegNumFont);
myGLCD.setColor(0, 255, 0);
myGLCD.setBackColor(0, 0, 0);
myGLCD.printNumI(Temp[i],CENTER,145);
i++;
delay(1000);
}
//====================================================
void drawGround() {
myGLCD.setColor(221,216,148);
myGLCD.fillRect(0, 215, 319, 239);
myGLCD.setColor(47,175,68);
myGLCD.fillRect(0, 205, 319, 214);
myGLCD.setColor(0, 0, 0);
myGLCD.setBackColor(221, 216, 148);
myGLCD.setFont(BigFont);
myGLCD.print("Score:",5,220);
myGLCD.setFont(SmallFont);
myGLCD.print("HowToMechatronics.com", 140, 220);
}
//====================================================
void Result(){
double sum_pres,sum_pres_2;
sum_pres = 0;
sum_pres_2=sum_pres*5;
HP20x.begin();
long Pressure = HP20x.ReadPressure();
float pres[100];
pres[i] = Pressure/100.0;
float Temp=getTemp();
float temp[100];
myGLCD.setColor(100, 155, 203);
myGLCD.fillRoundRect (10, 10, 60, 36);
myGLCD.setColor(255, 255, 255);
myGLCD.drawRoundRect (10, 10, 60, 36);
myGLCD.setFont(BigFont);
myGLCD.setBackColor(100, 155, 203);
myGLCD.print("<-", 18, 15);
myGLCD.setBackColor(0, 0, 0);
myGLCD.setFont(SmallFont);
myGLCD.print("Back to Main Menu", 70, 18);
myGLCD.setColor(255, 255, 255);
myGLCD.setBackColor(0, 0, 0);
myGLCD.setFont(BigFont);
for (i = 100; i >= 80; i--)
if (temp[i] < 20)
{
for (i = 100; i >= 80; i--)
{
if (pres[i] >= 480 && pres[i] < 530)
{
pres[i] = 1;
sum_pres = sum_pres + pres[i];
}
else
{
pres[i] = 0;
sum_pres = sum_pres + pres[i];
}
}
myGLCD.print("Percent",50,200);
myGLCD.printNumI(sum_pres_2,180,200);
myGLCD.print("%",230,200);
if (sum_pres >= 15)
{
myGLCD.setColor(255, 0, 0);
myGLCD.print("Gasoline",CENTER,150);
myGLCD.print("WOW",CENTER,100);}
else
{
myGLCD.setColor(255, 0, 0);
myGLCD.print("Fake Gasoline",CENTER,150);
myGLCD.print("OTL", CENTER, 100);}
}
else if (temp[i] >= 20 && temp[i] < 22)
{
for (i = 100; i >= 80; i--)
{
if (pres[i] >= 480 && pres[i] < 530)
{
pres[i] = 1;
sum_pres = sum_pres + pres[i];
}
else
{
pres[i] = 0;
sum_pres = sum_pres + pres[i];
}
}
myGLCD.print("Percent",50,200);
myGLCD.printNumI(sum_pres_2,180,200);
myGLCD.print("%",230,200);
if (sum_pres >= 15)
{
myGLCD.setColor(255, 0, 0);
myGLCD.print("Gasoline",CENTER,150);
myGLCD.print("WOW",CENTER,100);}
else
{
myGLCD.setColor(255, 0, 0);
myGLCD.print("Fake Gasoline",CENTER,150);
myGLCD.print("OTL", CENTER, 100);}
}
else if (temp[i] >= 22 && temp[i] < 24)
{
for (i = 100; i >= 80; i--)
{
if (pres[i] >= 480 && pres[i] < 530)
{
pres[i] = 1;
sum_pres = sum_pres + pres[i];
}
else
{
pres[i] = 0;
sum_pres = sum_pres + pres[i];
}
}
myGLCD.print("Percent",50,200);
myGLCD.printNumI(sum_pres_2,180,200);
myGLCD.print("%",230,200);
if (sum_pres >= 15)
{
myGLCD.setColor(255, 0, 0);
myGLCD.print("Gasoline",CENTER,150);
myGLCD.print("WOW",CENTER,100);}
else
{
myGLCD.setColor(255, 0, 0);
myGLCD.print("Fake Gasoline",CENTER,150);
myGLCD.print("OTL", CENTER, 100);}
}
else if (temp[i] >= 24 && temp[i] < 26)
{
for (i = 100; i >= 80; i--)
{
if (pres[i] >= 480 && pres[i] < 530)
{
pres[i] = 1;
sum_pres = sum_pres + pres[i];
}
else
{
pres[i] = 0;
sum_pres = sum_pres + pres[i];
}
}
myGLCD.print("Percent",50,200);
myGLCD.printNumI(sum_pres_2,180,200);
myGLCD.print("%",230,200);
if (sum_pres >= 15)
{
myGLCD.setColor(255, 0, 0);
myGLCD.print("Gasoline",CENTER,150);
myGLCD.print("WOW",CENTER,100);}
else
{
myGLCD.setColor(255, 0, 0);
myGLCD.print("Fake Gasoline",CENTER,150);
myGLCD.print("OTL", CENTER, 100);}
}
else if (temp[i] >= 26 && temp[i] < 28)
{
for (i = 100; i >= 80; i--)
{
if (pres[i] >= 480 && pres[i] < 530)
{
pres[i] = 1;
sum_pres = sum_pres + pres[i];
}
else
{
pres[i] = 0;
sum_pres = sum_pres + pres[i];
}
}
myGLCD.print("Percent",50,200);
myGLCD.printNumI(sum_pres_2,180,200);
myGLCD.print("%",230,200);
if (sum_pres >= 15)
{
myGLCD.setColor(255, 0, 0);
myGLCD.print("Gasoline",CENTER,150);
myGLCD.print("WOW",CENTER,100);}
else
{
myGLCD.setColor(255, 0, 0);
myGLCD.print("Fake Gasoline",CENTER,150);
myGLCD.print("OTL", CENTER, 100);}
}
else if (temp[i] >= 28 && temp[i] < 30)
{
for (i = 100; i >= 80; i--)
{
if (pres[i] >= 480 && pres[i] < 530)
{
pres[i] = 1;
sum_pres = sum_pres + pres[i];
}
else
{
pres[i] = 0;
sum_pres = sum_pres + pres[i];
}
}
myGLCD.print("Percent",50,200);
myGLCD.printNumI(sum_pres_2,180,200);
myGLCD.print("%",230,200);
if (sum_pres >= 15)
{
myGLCD.setColor(255, 0, 0);
myGLCD.print("Gasoline",CENTER,150);
myGLCD.print("WOW",CENTER,100);}
else
{
myGLCD.setColor(255, 0, 0);
myGLCD.print("Fake Gasoline",CENTER,150);
myGLCD.print("OTL", CENTER, 100);}
}
else if (temp[i] >= 30 && temp[i] < 35)
{
for (i = 100; i >= 80; i--)
{
if (pres[i] >= 480 && pres[i] < 530)
{
pres[i] = 1;
sum_pres = sum_pres + pres[i];
}
else
{
pres[i] = 0;
sum_pres = sum_pres + pres[i];
}
}
myGLCD.print("Percent",50,200);
myGLCD.printNumI(sum_pres_2,180,200);
myGLCD.print("%",230,200);
if (sum_pres >= 15)
{
myGLCD.setColor(255, 0, 0);
myGLCD.print("Gasoline",CENTER,150);
myGLCD.print("WOW",CENTER,100);}
else
{
myGLCD.setColor(255, 0, 0);
myGLCD.print("Fake Gasoline",CENTER,150);
myGLCD.print("OTL", CENTER, 100);}
}
else
myGLCD.print("fail",CENTER,150);
}
|