RENKLİ TFT EKRANLI MASA SAATİ

SICAKLIK VE NEM GÖSTEREN 2.4 inch spfd-5408 RENKLİ TFT EKRANLI MASA SAATİ
DHT11 2.4 inch spfd5408 renkli TFT EKRANLI MASA SAATİ YAZILIMI
(Gerekli pin bağlantıları programda bellidir) Arduino uno ve spfd5408 renkli ekran shield'i kullanıldı. Ayrıca dht11 ısı ve nem sensörü, ds1302 RTC modülü kullanıldı.
Yazılım Versiyon 1:
#include <Time.h>
#include <DS1302RTC.h> //sonraki versiyonda virtualrobotix kullanıldı. Daha kolay.
// Set pins:  CE, IO,CLK
DS1302RTC RTC(A5, 0, 1);
// Optional connection for RTC module
//#define DS1302_GND_PIN 33
//#define DS1302_VCC_PIN 35
//int dht11
#include <dht11.h>
dht11 DHT11;
#include <SPFD5408_Adafruit_GFX.h>    // Core graphics library
#include <SPFD5408_Adafruit_TFTLCD.h> // Hardware-specific library
#if defined(__SAM3X8E__)
#undef __FlashStringHelper::F(string_literal)
#define F(string_literal) string_literal
#endif
/*
  #define YP A1  // must be an analog pin, use "An" notation!
  #define XM A2  // must be an analog pin, use "An" notation!
  #define YM 5   // can be a digital pin
  #define XP 6   // can be a digital pin
*/
/*#define TS_MINX 125
  #define TS_MINY 85
  #define TS_MAXX 965
  #define TS_MAXY 905
*/
#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
// optional
#define LCD_RESET A4
#define  BLACK   0x0000
#define  BLUE    0x001F
#define  RED     0xF800
#define  GREEN   0x07E0
#define  CYAN    0x07FF
#define  MAGENTA 0xF81F
#define  YELLOW  0xFFE0
#define  WHITE   0xFFFF
Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
void setup()
{// digitalWrite(DS1302_GND_PIN, LOW);
  //pinMode(DS1302_GND_PIN, OUTPUT);
//  digitalWrite(DS1302_VCC_PIN, HIGH);
 // pinMode(DS1302_VCC_PIN, OUTPUT);
  delay(5000);
  DHT11.attach(13); // Burada dijital 13 numaralı pini kullandık. Kısıtlı pin bulunuyor.
  // tft.reset();
  tft.begin(0x9341); // SDFP5408
  tft.setRotation(1);
  tft.fillScreen(BLACK);
}
void loop() {
  int chk = DHT11.read();
  tft.setTextSize (3);
  tft.setCursor (10, 5);
  tft.setTextColor(RED);
  tft.print("SICAKLIK (C):");
   tft.setTextSize (5);
  tft.print((float)DHT11.temperature, 0);
  tft.setCursor (10, 40);
  tft.setTextColor(GREEN);
  tft.setTextSize (3);
  tft.print("NEM (%):");
  tft.print((float)DHT11.humidity, 0);
  tft.setCursor (10, 70);
  tft.setTextColor(YELLOW);
  tft.print("CiG (C):");
  tft.println(DHT11.dewPoint(), 1);
  tft.setTextColor(BLUE);
  tft.print("-----------------");
  tft.setTextColor(WHITE);
  tmElements_t tm;
  ! RTC.read(tm);
  tft.setTextSize (6);
  tft.setCursor(10, 120);
  print2digits(tm.Hour);
  tft.write(':');
  print2digits(tm.Minute);
  tft.write(':');
  print2digits(tm.Second);
  //tft.print(", ");
  tft.setCursor(10, 180);
  tft.setTextSize (5);
  tft.setTextColor(MAGENTA);
  tft.print(tm.Day);
  tft.write('/');
  tft.print(tm.Month);
  tft.write('/');
  tft.print(tmYearToCalendar(tm.Year));
  tft.setCursor(60, 225);
  tft.setTextSize (1);
  tft.setTextColor(CYAN);
  tft.print("AKILLI MASA SAATi / AYTEKiN TUZLALI");
  delay(2000);
  tft.fillScreen(BLACK);
}
void print2digits(int number) {
  if (number >= 0 && number < 10)
    tft.write('0');
  tft.print(number);
}

Versiyon 2: Üsttekine göre daha görsel ve gelişmiş bir program
//Burada dht22 ısı ve nem sensörü ile proje gerçekleştirildi.

 YAZILIM:
#include "DHT.h"
#define DHTPIN A5
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
#include <virtuabotixRTC.h>
virtuabotixRTC myRTC(11, 12, 13);
// Set pins:  CE, IO, CLK
#include <SPFD5408_Adafruit_GFX.h>    // Core graphics Adafruit library
#include <SPFD5408_Adafruit_TFTLCD.h> // Hardware-specific library
#include <SPFD5408_TouchScreen.h>     // Touchscreen library
// *** Define Touchscreen Pin
#define YP A1
#define XM A2
#define YM 5
#define XP 6
// *** Define Value of Touchscreen input
#define TS_MINX 125
#define TS_MINY 85
#define TS_MAXX 965
#define TS_MAXY 905
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
// *** Define Pin of LCD used
#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
#define LCD_RESET A4
// *** Define Name of Color
#define BLACK   0x0000
#define WHITE   0xFFFF
#define RED     0xF800
#define GREEN   0x07E0
#define BLUE    0x001F
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define GREY    0x2108
Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
#define MINPRESSURE 10
#define MAXPRESSURE 1000
// Meter colour schemes
#define RED2RED 0
#define GREEN2GREEN 1
#define BLUE2BLUE 2
#define BLUE2RED 3
#define GREEN2RED 4
#define RED2GREEN 5
uint32_t runTime = -99999;       // time for next update
int reading = 0; // Value to be displayed
int d = 0; // Variable used for the sinewave test waveform
boolean alert = 0;
int8_t ramp = 1;
int tesmod = 0;
void setup() {
  // myRTC.setDS1302Time(00, 04, 13, 7, 23, 7, 2017);
  // zaman; ilk defa ayarlanacaksa üst satır açılır(hafızaya alınır), sonra hemen kapatılır.
  dht.begin();
  tft.reset(); //Reset LCD to begin
  tft.begin(0x9341); //SDFP5408 Chipset ID on Library
  tft.setRotation(1); // Set Rotation at 0 degress (default)
  tft.fillScreen(BLACK); //Set Background Color with BLACK
  tft.setCursor (223, 75);
  tft.setTextSize (2);
  tft.setTextColor (GREEN, BLACK);
  tft.print ("SICAKLIK");
  tft.setCursor (240, 5);
  tft.setTextSize (3);
  tft.setTextColor (RED, BLACK);
  tft.print ("NEM");
  //Design Interface (lines)
  tft.fillRect(0, 197, 217, 4, RED);
  tft.fillRect(217, 130, 320, 4, YELLOW);
  tft.fillRect(217, 0, 4, 240, CYAN);
  tft.fillRect(217, 65, 320, 4, GREEN);
}
void loop() {
  myRTC.updateTime();
  tft.setCursor (0, 205);
  tft.setTextSize (4);
  tft.setTextColor (WHITE, BLACK);
  tft.print (myRTC.hours);
  tft.write(':');
  tft.print(myRTC.minutes);
  tft.write(':');
  tft.print(myRTC.seconds);
  tft.write(' ');
  tft.setCursor (225, 140);
  tft.setTextSize (3);
  tft.setTextColor (MAGENTA, BLACK);
  tft.print("TARiH");
  tft.setCursor (225, 165); //heat index fahrenheit
  tft.setTextSize (3);
  tft.setTextColor (WHITE, BLACK);
  tft.print (myRTC.dayofmonth);
  tft.write('/');
  tft.write(' ');
  tft.setCursor (225, 190);
  tft.print(myRTC.month);
  tft.write('/');
  tft.write(' ');
  tft.setCursor (225, 215);
  tft.print(myRTC.year);
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  float f = dht.readTemperature(true);
  tft.setCursor (225, 100);
  tft.setTextSize (3);
  tft.setTextColor (YELLOW, BLACK);
  tft.print (t, 1); tft.print ('C');
  if (millis() - runTime >= 500) { // Execute every 500ms
    runTime = millis();
    if (tesmod == 0) {
      reading = 40;
    }
    if (tesmod == 1) {
      reading = t ;
    }
    int xpos = 0, ypos = 5, gap = 4, radius = 52;
    // Draw a large meter
    xpos = 320 / 2 - 160, ypos = 0, gap = 100, radius = 105;
    ringMeter(reading, 1, 40, xpos, ypos, radius, "Celsius", GREEN2RED); // Draw analogue meter
    if (h > 0) { //Humidity %
      tft.setCursor (225, 35); //157,208
      tft.setTextSize (3);
      tft.setTextColor (YELLOW, BLACK);
      tft.print ('%'); tft.print (h, 1);
      tesmod = 1;
    }
  }
}
// #########################################################################
//  Draw the meter on the screen, returns x coord of righthand side
// #########################################################################
int ringMeter(int value, int vmin, int vmax, int x, int y, int r, char *units, byte scheme)
{
  // Minimum value of r is about 52 before value text intrudes on ring
  // drawing the text first is an option
  x += r; y += r;   // Calculate coords of centre of ring
  int w = r / 4;    // Width of outer ring is 1/4 of radius
  int angle = 150;  // Half the sweep angle of meter (300 degrees)
  int v = map(value, vmin, vmax, -angle, angle); // Map the value to an angle v
  byte seg = 3; // Segments are 3 degrees wide = 100 segments for 300 degrees
  byte inc = 6; // Draw segments every 3 degrees, increase to 6 for segmented ring
  // Variable to save "value" text colour from scheme and set default
  int colour = BLUE;
  // Draw colour blocks every inc degrees
  for (int i = -angle + inc / 2; i < angle - inc / 2; i += inc) {
    // Calculate pair of coordinates for segment start
    float sx = cos((i - 90) * 0.0174532925);
    float sy = sin((i - 90) * 0.0174532925);
    uint16_t x0 = sx * (r - w) + x;
    uint16_t y0 = sy * (r - w) + y;
    uint16_t x1 = sx * r + x;
    uint16_t y1 = sy * r + y;
    // Calculate pair of coordinates for segment end
    float sx2 = cos((i + seg - 90) * 0.0174532925);
    float sy2 = sin((i + seg - 90) * 0.0174532925);
    int x2 = sx2 * (r - w) + x;
    int y2 = sy2 * (r - w) + y;
    int x3 = sx2 * r + x;
    int y3 = sy2 * r + y;
    if (i < v) { // Fill in coloured segments with 2 triangles
      switch (scheme) {
        case 0: colour = RED; break; // Fixed colour
        case 1: colour = GREEN; break; // Fixed colour
        case 2: colour = BLUE; break; // Fixed colour
        case 3: colour = rainbow(map(i, -angle, angle, 0, 127)); break; // Full spectrum blue to red
        case 4: colour = rainbow(map(i, -angle, angle, 70, 127)); break; // Green to red (high temperature etc)
        case 5: colour = rainbow(map(i, -angle, angle, 127, 63)); break; // Red to green (low battery etc)
        default: colour = BLUE; break; // Fixed colour
      }
      tft.fillTriangle(x0, y0, x1, y1, x2, y2, colour);
      tft.fillTriangle(x1, y1, x2, y2, x3, y3, colour);
      //text_colour = colour; // Save the last colour drawn
    }
    else // Fill in blank segments
    {
      tft.fillTriangle(x0, y0, x1, y1, x2, y2, GREY);
      tft.fillTriangle(x1, y1, x2, y2, x3, y3, GREY);
    }
  }
  // Convert value to a string
  char buf[10];
  byte len = 0;  if (value > 999) len = 5;
  dtostrf(value, 1, len, buf);
  // buf[len] = ' '; buf[len] = 2; // Add blanking space and terminator, helps to centre text too!
  // Set the text colour to default
  if (value > 9) {
    tft.setTextColor(colour, BLACK);
    tft.setCursor(x - 30, y - 40); tft.setTextSize(4);
    tft.print(buf);
  }
  if (value < 10) {
    tft.setTextColor(colour, BLACK);
    tft.setCursor(x - 25, y - 10); tft.setTextSize(4);
    tft.print(buf);
  }
  tft.setTextColor(WHITE, BLACK);
  tft.setCursor(x - 39, y + 75); tft.setTextSize(2);
  tft.print(units); // Units display
  // Calculate and return right hand side x coordinate
  return x + r;
}
// #########################################################################
// Return a 16 bit rainbow colour
// #########################################################################
unsigned int rainbow(byte value)
{
  // Value is expected to be in range 0-127
  // The value is converted to a spectrum colour from 0 = blue through to 127 = red
  byte red = 0; // Red is the top 5 bits of a 16 bit colour value
  byte green = 0;// Green is the middle 6 bits
  byte blue = 0; // Blue is the bottom 5 bits
  byte quadrant = value / 32;
  if (quadrant == 0) {
    blue = 31;
    green = 2 * (value % 32);
    red = 0;
  }
  if (quadrant == 1) {
    blue = 31 - (value % 32);
    green = 63;
    red = 0;
  }
  if (quadrant == 2) {
    blue = 0;
    green = 63;
    red = value % 32;
  }
  if (quadrant == 3) {
    blue = 0;
    green = 63 - 2 * (value % 32);
    red = 31;
  }
  return (red << 11) + (green << 5) + blue;
}
// #########################################################################
// Return a value in range -1 to +1 for a given phase angle in degrees
// #########################################################################
float sineWave(int phase) {
  return sin(phase * 0.0174532925);
}

Hiç yorum yok:

Yorum Gönder