1.8 TFT 128*160 и BMP180

gulin176
Offline
Зарегистрирован: 03.09.2016

Здравствуйте. Не нашел в интернете готового. Сделал своего франкенштейна из 2 библиотек. Экран вообще планирую подключить к PZEM, но пока не могу снять счётчик решил сделать термометр на цветном экране. проблема в том что по экрану почти нет полезной информации. Подскажите кто разбирается почему то не чистится место где выводится первая цифра. можно конечно чистить весь экран но надписи которые не в лупе пропадают тогда. Вроде бы всё очевидно, если выводить одну температуру то её чистит. Если вместе с давлением то чистит только цифу давления

[code]
#include <TFT.h>  // Arduino LCD library
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP085_U.h>
#include <Wire.h>
Adafruit_BMP085_Unified bmp = Adafruit_BMP085_Unified(10085);
// pin definition for the Uno
#define sclk 13
#define mosi 11
#define cs   10
#define dc   8
#define rst  9
// create an instance of the library
TFT TFTscreen = TFT(cs, dc, rst);
// char array to print to the screen
char sensorPrintout1[5];
char sensorPrintout2[5];
void setup() {
  // Put this line at the beginning of every sketch that uses the GLCD:
  TFTscreen.begin();
  bmp.begin();
  // clear the screen with a black background
  TFTscreen.background(0, 0, 0);
  // write the static text to the screen
  // set the font color to white
  TFTscreen.stroke(255, 255, 255);
  // set the font size
  TFTscreen.setTextSize(2);
  // write the text to the top left corner of the screen
  TFTscreen.text("TEMPERATURA\n ", 12, 0);
  TFTscreen.text("PRESSURE\n ", 28, 52);
  TFTscreen.setTextSize(4);
}

void loop() {
  sensors_event_t event;
  bmp.getEvent(&event);
  float temperature;
  bmp.getTemperature(&temperature);
  // Read the value of the sensor
  String sensorVal = String(temperature);
  // convert the reading to a char array
  sensorVal.toCharArray(sensorPrintout1, 6);
  // set the font color
  TFTscreen.stroke(255, 255, 255);
  // print the sensor value
  TFTscreen.text(sensorPrintout1, 28, 22);
  String sensorVal1 = String(event.pressure);
  sensorVal1.toCharArray(sensorPrintout2, 6);
  TFTscreen.stroke(255, 255, 255);
  TFTscreen.text(sensorPrintout2, 28, 72);
  // wait for a moment
  delay(2500);
  // erase the text you just wrote
  //TFTscreen.background(0, 0, 0);
  TFTscreen.stroke(0, 0, 0); //цвет очистки
  TFTscreen.text(sensorPrintout1, 28, 22); // положение где чистить
  TFTscreen.stroke(0, 0, 0);
  TFTscreen.text(sensorPrintout2, 28, 72);

}

[/code]

 

gulin176
Offline
Зарегистрирован: 03.09.2016

провёл эксперимент. строка 58 в переменной sensorPrintout1 не знает что выводить на экран(она ничего не выводит на экран). хотя в строке 17 она записана. не пойму в строке 60 переменная знает что выводить на экран. всё же одинакого

gulin176
Offline
Зарегистрирован: 03.09.2016

 

 

 

 

 

 

 

 

 

 

 

[code]
#include <TFT.h>  // Arduino LCD library
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP085_U.h>
#include <Wire.h>
#include <DS3231.h>
DS3231 clock;
RTCDateTime dt;
uint8_t hour;
uint8_t minute;
Adafruit_BMP085_Unified bmp = Adafruit_BMP085_Unified(10085);
// pin definition for the Uno
#define sclk 13
#define mosi 11
#define cs   10
#define dc   8
#define rst  9
// create an instance of the library
TFT TFTscreen = TFT(cs, dc, rst);
#define BLACK    0x0000
#define BLUE     0x001F
#define RED      0xF800
#define GREEN    0x07E0
#define CYAN     0x07FF
#define MAGENTA  0xF81F
#define YELLOW   0xFFE0 
#define WHITE    0xFFFF
// char array to print to the screen
char out1 [5];
char out2 [5];
char out3 [5];
char out4 [5];
float temperature;
void setup() {
  TFTscreen.begin();
  bmp.begin();
  // clear the screen with a black background
  TFTscreen.background(0, 0, 0);
  // write the static text to the screen
  // set the font color to white
  TFTscreen.stroke(255, 255, 255);
  // set the font size
  TFTscreen.setTextSize(2);
  // write the text to the top left corner of the screen
  TFTscreen.text("TEMPERATURA\n ", 12, 0);
  TFTscreen.text("PRESSURE\n ", 30, 50);
  TFTscreen.setTextSize(4);
}
void loop() {
  dt = clock.getDateTime();
  hour = dt.hour;
  minute = dt.minute;
  if (minute < 10) {
    TFTscreen.stroke(255, 255, 255);
    TFTscreen.text("0", 85, 100);
    String sensorVal3 = String(minute);
    sensorVal3.toCharArray(out4, 6);
    TFTscreen.stroke(255, 255, 255);
    TFTscreen.text(out4, 108, 100);
  }
  else {
    String sensorVal3 = String(minute);
    sensorVal3.toCharArray(out4, 6);
    TFTscreen.stroke(255, 255, 255);
    TFTscreen.text(out4, 85, 100);
  }
  String sensorVal2 = String(hour);
  sensorVal2.toCharArray(out3, 6);
  TFTscreen.stroke(255, 255, 255);
  TFTscreen.text(out3, 24, 100);
  sensors_event_t event;
  bmp.getEvent(&event);

  String sensorVal1 = String(event.pressure);
  sensorVal1.toCharArray(out2, 6);
  TFTscreen.stroke(255, 255, 255);
  TFTscreen.text(out2, 24, 70);

  bmp.getTemperature(&temperature);
  String sensorVal = String(temperature);
  sensorVal.toCharArray(out1, 6);
  TFTscreen.stroke(255, 255, 255);
  TFTscreen.text(out1, 24, 16);

  delay(2500);
  TFTscreen.stroke(0, 0, 0);
  TFTscreen.text(out1, 24, 16);
  TFTscreen.text(out2, 24, 70);
  TFTscreen.text(out3, 24, 100);
  if (minute < 10) {
  TFTscreen.text(out4, 108, 100);
  }
  else
  {
   TFTscreen.text(out4, 85, 100); 
  }
}

[/code]

примерно так.не понимаю теперь не чистится только время часов. поднял блок "температуры" над блоком "давления" и стало нормально. добавил часы 3231 теперь часы не затирает

gulin176
Offline
Зарегистрирован: 03.09.2016

попользовался библиотекой #include <TFT.h> понял что это просто ужас. решил что библиотека #include <Adafruit_ST7735.h> это просто просветление и ничего подтирать каждый цикл не надо. на основании материала взятого с ютуб сделал градусник, гигрометр(si7021), показатель комфортной температуры, часы(3231). код

[code]
#define sclk 13
#define mosi 11
#define lcd_cs 10
#define dc 8
#define rst 9
#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>
#include <SPI.h>
#include <HTU21D.h>
#include <DS3231.h>
DS3231 clock;
RTCDateTime dt;
uint8_t hour;
uint8_t minute;
Adafruit_ST7735 tft = Adafruit_ST7735(lcd_cs, dc, rst);
//Black theme
#define COLOR1 ST7735_WHITE
#define COLOR2 ST7735_BLACK
//White theme
//#define COLOR1 ST7735_BLACK
//#define COLOR2 ST7735_WHITE
int text_color_humidex;
float humidity, temperature, humidex, time ;
String message;
HTU21D myHTU21D;
void setup(void) {
  myHTU21D.begin();
  tft.initR(INITR_BLACKTAB);   // initialize a ST7735S chip, black tab
  tft.fillScreen(COLOR2);
}
/* Recode russian fonts from UTF-8 to Windows-1251 */
String utf8rus(String source)
{
  int i, k;
  String target;
  unsigned char n;
  char m[2] = { '0', '\0' };
  k = source.length(); i = 0;
  while (i < k) {
    n = source[i]; i++;
    if (n >= 0xC0) {
      switch (n) {
        case 0xD0: {
            n = source[i]; i++;
            if (n == 0x81) {
              n = 0xA8;
              break;
            }
            if (n >= 0x90 && n <= 0xBF) n = n + 0x30;
            break;
          }
        case 0xD1: {
            n = source[i]; i++;
            if (n == 0x91) {
              n = 0xB8;
              break;
            }
            if (n >= 0x80 && n <= 0x8F) n = n + 0x70;
            break;
          }
      }
    }
    m[0] = n; target = target + String(m);
  }
  return target;
}
//function to calculete Humidex
float calculate_humidex(float temperature, float humidity) {
  float e;

  e = (6.112 * pow(10, (7.5 * temperature / (237.7 + temperature))) * humidity / 100); //vapor pressure

  float humidex = temperature + 0.55555555 * (e - 10.0); //humidex
  return humidex;
}
// outputs temperature to LCD
void temperature_to_lcd (float temperature, unsigned char text_position )
{
  int text_color;

  tft.setCursor(4, text_position);
  tft.setTextColor(COLOR1, COLOR2);
  tft.setTextSize(1);
  tft.print(utf8rus("температура:"));
  tft.setTextSize(3);
  if (temperature > 0) {
    text_color = ST7735_RED;
  }
  else {
    text_color = ST7735_BLUE;
  }
  tft.setCursor(1, text_position + 9);
  fix_number_position(temperature);
  tft.setTextColor(text_color, COLOR2);
  tft.print(temperature, 1);

  tft.setCursor(108, text_position + 9);
  tft.print("C");
  tft.drawChar(90, text_position + 9, 247, text_color, COLOR2, 2); //degree symbol
}
// aligs number to constant position
void fix_number_position(float number)

{


  if ((number >= -40) && (number < -9.9))
  {
    ;
  }

  if ((number >= -9.9) && (number < 0.0))
  {
    tft.print(" ");
  }

  if ((number >= 0.0 ) && (number < 9.9))
  {
    tft.print("  ");
  }

  if ((number >= 9.9 ) && (number < 99.9))
  {
    tft.print(" ");
  }

  if ((number >= 99.9 ) && (number < 151))
  {
    tft.print("");
  }
}
void loop() {
  dt = clock.getDateTime();
  hour = dt.hour;
  minute = dt.minute;

  humidity = myHTU21D.readHumidity();
  temperature = myHTU21D.readTemperature();

  //humidex is calculated

  humidex = calculate_humidex (temperature, humidity);


  // Table
  tft.drawRect(0, 0, 128, 160, COLOR1);
  tft.drawLine(0, 35, 128, 35, COLOR1);
  tft.drawLine(0, 70, 128, 70, COLOR1);
  tft.drawLine(0, 113, 128, 113, COLOR1);


  // data is outputed

  temperature_to_lcd (temperature, 2);
  humidity_to_lcd (humidity, 37);
  humidex_to_lcd (humidex, 72);
  time_to_lcd (time, 115);

}
//outputs time to LCD
void time_to_lcd (float time, unsigned char text_position )
{
  tft.setTextColor(COLOR1, COLOR2);
  tft.setCursor(4, text_position);
  tft.setTextSize(1);
  tft.println("time:");
  tft.setTextSize(4);
  tft.setCursor(10, text_position + 9);
  if (hour < 10) {
    tft.print(0, 1);
    tft.print(hour, 1);
  }
  else
  {
    tft.print(hour, 1);
  }
  tft.setCursor(70, text_position + 9);
  if (minute < 10) {
    tft.print(0, 1);
    tft.print(minute, 1);
  }
  else
  {
    tft.print(minute, 1);
  }
}
//outputs humidity to LCD
void humidity_to_lcd (float humidity, unsigned char text_position )
{
  tft.setTextColor(COLOR1, COLOR2);
  tft.setCursor(4, text_position);
  tft.setTextSize(1);
  tft.println("humidity:");
  tft.setTextSize(3);
  tft.setCursor(1, text_position + 9);

  fix_number_position(humidity);

  tft.print(humidity, 1);
  tft.print(" %");

}

//outputs Humidex to LCD

void humidex_to_lcd (float humidex, unsigned char text_position )

{
  tft.setCursor(4, text_position);

  tft.setTextSize(1);
  tft.println("humidex:");
  tft.setTextSize(3);

  tft.setCursor(1, text_position + 9);

  if ((humidex >= 21 ) && (temperature < 44)) {

    fix_number_position(humidex);
    get_humidex_color_warning_message(humidex);
    tft.setTextColor(text_color_humidex, COLOR2);
    tft.print(humidex, 1);

    tft.setCursor(108, text_position + 9);
    tft.print("C");
    tft.drawChar(90, text_position + 9, 247, text_color_humidex, COLOR2, 2); //degree symbol

    tft.setCursor(3, text_position + 32);
    tft.setTextSize(1);
    tft.print(message);
  }

  else {
    tft.print(" --.-");
    tft.setCursor(108, text_position + 9);
    tft.print("C");
    tft.drawChar(90, text_position + 9, 247, COLOR1, COLOR2, 2); //degree symbol
    tft.setCursor(1, text_position + 32);
    tft.setTextSize(1);
    tft.println("                   ");
  };

}
// Setting text color and message based on Humidex value
void get_humidex_color_warning_message(float humidex)
{
  if ((humidex >= 21 ) && (humidex < 27))
  {
    text_color_humidex = tft.Color565(0, 235, 0);
    message = "no discomfort      ";
  } // dark green

  if ((humidex >= 27 ) && (humidex < 35))
  {
    text_color_humidex = tft.Color565(76, 255, 0); // light green
    message = "some discomfort     ";
  }

  if ((humidex >= 35 ) && (humidex < 40))
  {
    text_color_humidex = tft.Color565(255, 255, 0);
    message = "great discomfort    ";
  } // yellow


  if ((humidex >= 40 ) && (humidex < 46))
  {
    text_color_humidex = tft.Color565(255, 140, 0);
    message = "health risk         ";
  } //light orange

  if ((humidex >= 46 ) && (humidex < 54))
  {
    text_color_humidex = tft.Color565(221, 128, 0);
    message = "great health risk   ";
  } //dark orange

  if ((humidex >= 54 ))
  {
    text_color_humidex = tft.Color565(255, 0, 0);
    message = "heat stroke danger  ";
  } // red
}
[/code]

проблема в том что при попытке перевести экран на русский язык выводится абракадабра. хотя все условия выполнил так как раньше никто ничего не посоветовал не вижу смысла задавать вопросы

gulin176
Offline
Зарегистрирован: 03.09.2016

gulin176
Offline
Зарегистрирован: 03.09.2016
[code]
#define sclk 13
#define mosi 11
#define lcd_cs 10
#define dc 8
#define rst 9
#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>
#include <SPI.h>
#include <HTU21D.h>
#include <DS3231.h>
DS3231 clock;
RTCDateTime dt;
uint8_t hour;
uint8_t minute;
Adafruit_ST7735 tft = Adafruit_ST7735(lcd_cs, dc, rst);
//Black theme
#define COLOR1 ST7735_WHITE
#define COLOR2 ST7735_BLACK
//White theme
//#define COLOR1 ST7735_BLACK
//#define COLOR2 ST7735_WHITE
int text_color_humidex;
float humidity, temperature, humidex, time ;
String message;
HTU21D myHTU21D;
void setup(void) {
  myHTU21D.begin();
  tft.initR(INITR_BLACKTAB);   // initialize a ST7735S chip, black tab
  tft.cp437(true);
  tft.fillScreen(COLOR2);
}
/* Recode russian fonts from UTF-8 to Windows-1251 */
String utf8rus(String source)
{
  int i, k;
  String target;
  unsigned char n;
  char m[2] = { '0', '\0' };
  k = source.length(); i = 0;
  while (i < k) {
    n = source[i]; i++;
    if (n >= 0xC0) {
      switch (n) {
        case 0xD0: {
            n = source[i]; i++;
            if (n == 0x81) {
              n = 0xA8;
              break;
            }
            if (n >= 0x90 && n <= 0xBF) n = n + 0x30;
            break;
          }
        case 0xD1: {
            n = source[i]; i++;
            if (n == 0x91) {
              n = 0xB8;
              break;
            }
            if (n >= 0x80 && n <= 0x8F) n = n + 0x70;
            break;
          }
      }
    }
    m[0] = n; target = target + String(m);
  }
  return target;
}
//function to calculete Humidex
float calculate_humidex(float temperature, float humidity) {
  float e;

  e = (6.112 * pow(10, (7.5 * temperature / (237.7 + temperature))) * humidity / 100); //vapor pressure

  float humidex = temperature + 0.55555555 * (e - 10.0); //humidex
  return humidex;
}
// outputs temperature to LCD
void temperature_to_lcd (float temperature, unsigned char text_position )
{
  int text_color;

  tft.setCursor(4, text_position);
  tft.setTextColor(COLOR1, COLOR2);
  tft.setTextSize(1);
  tft.print(utf8rus("температура:"));
  tft.setTextSize(3);
  if (temperature > 0) {
    text_color = ST7735_RED;
  }
  else {
    text_color = ST7735_BLUE;
  }
  tft.setCursor(1, text_position + 9);
  fix_number_position(temperature);
  tft.setTextColor(text_color, COLOR2);
  tft.print(temperature, 1);

  tft.setCursor(108, text_position + 9);
  tft.print("C");
  tft.drawChar(96, text_position + 7, 265, text_color, COLOR2, 2); //degree symbol
}
// aligs number to constant position
void fix_number_position(float number)

{


  if ((number >= -40) && (number < -9.9))
  {
    ;
  }

  if ((number >= -9.9) && (number < 0.0))
  {
    tft.print(" ");
  }

  if ((number >= 0.0 ) && (number < 9.9))
  {
    tft.print("  ");
  }

  if ((number >= 9.9 ) && (number < 99.9))
  {
    tft.print(" ");
  }

  if ((number >= 99.9 ) && (number < 151))
  {
    tft.print("");
  }
}
void loop() {
  dt = clock.getDateTime();
  hour = dt.hour;
  minute = dt.minute;

  humidity = myHTU21D.readHumidity();
  temperature = myHTU21D.readTemperature();

  //humidex is calculated

  humidex = calculate_humidex (temperature, humidity);


  // Table
  tft.drawRect(0, 0, 128, 160, COLOR1);
  tft.drawLine(0, 35, 128, 35, COLOR1);
  tft.drawLine(0, 70, 128, 70, COLOR1);
  tft.drawLine(0, 113, 128, 113, COLOR1);


  // data is outputed

  temperature_to_lcd (temperature, 2);
  humidity_to_lcd (humidity, 37);
  humidex_to_lcd (humidex, 72);
  time_to_lcd (time, 115);

}
//outputs time to LCD
void time_to_lcd (float time, unsigned char text_position )
{
  tft.setTextColor(COLOR1, COLOR2);
  tft.setCursor(4, text_position);
  tft.setTextSize(1);
  tft.println(utf8rus("время:"));
  tft.setTextSize(4);
  tft.setCursor(10, text_position + 9);
  if (hour < 10) {
    tft.print(0, 1);
    tft.print(hour, 1);
  }
  else
  {
    tft.print(hour, 1);
  }
  tft.setCursor(70, text_position + 9);
  if (minute < 10) {
    tft.print(0, 1);
    tft.print(minute, 1);
  }
  else
  {
    tft.print(minute, 1);
  }
}
//outputs humidity to LCD
void humidity_to_lcd (float humidity, unsigned char text_position )
{
  tft.setTextColor(COLOR1, COLOR2);
  tft.setCursor(4, text_position);
  tft.setTextSize(1);
  tft.println(utf8rus("влажность:"));
  tft.setTextSize(3);
  tft.setCursor(1, text_position + 9);

  fix_number_position(humidity);

  tft.print(humidity, 1);
  tft.print(" %");

}

//outputs Humidex to LCD

void humidex_to_lcd (float humidex, unsigned char text_position )

{
  tft.setCursor(4, text_position);

  tft.setTextSize(1);
  tft.println(utf8rus("температура комфорта"));
  tft.setTextSize(3);

  tft.setCursor(1, text_position + 9);

  if ((humidex >= 21 ) && (temperature < 44)) {

    fix_number_position(humidex);
    get_humidex_color_warning_message(humidex);
    tft.setTextColor(text_color_humidex, COLOR2);
    tft.print(humidex, 1);

    tft.setCursor(108, text_position + 9);
    tft.print("C");
    tft.drawChar(96, text_position + 7, 265, text_color_humidex, COLOR2, 2); //degree symbol

    tft.setCursor(3, text_position + 32);
    tft.setTextSize(1);
    tft.print(message);
  }

  else {
    tft.print(" --.-");
    tft.setCursor(108, text_position + 9);
    tft.print("C");
    tft.drawChar(96, text_position + 7, 265, COLOR1, COLOR2, 2); //degree symbol
    tft.setCursor(1, text_position + 32);
    tft.setTextSize(1);
    tft.println("                   ");
  };

}
// Setting text color and message based on Humidex value
void get_humidex_color_warning_message(float humidex)
{
  if ((humidex >= 21 ) && (humidex < 27))
  {
    text_color_humidex = tft.Color565(0, 235, 0);
    message = (utf8rus("хорошо              "));
  } // dark green

  if ((humidex >= 27 ) && (humidex < 35))
  {
    text_color_humidex = tft.Color565(76, 255, 0); // light green
    message = (utf8rus("небольшой дискомфорт"));
  }

  if ((humidex >= 35 ) && (humidex < 40))
  {
    text_color_humidex = tft.Color565(255, 255, 0);
    message = (utf8rus("жарко               "));
  } // yellow


  if ((humidex >= 40 ) && (humidex < 46))
  {
    text_color_humidex = tft.Color565(255, 140, 0);
    message = (utf8rus("риск для здоровья   "));
  } //light orange

  if ((humidex >= 46 ) && (humidex < 54))
  {
    text_color_humidex = tft.Color565(221, 128, 0);
    message = (utf8rus("значительный риск   "));
  } //dark orange

  if ((humidex >= 54 ))
  {
    text_color_humidex = tft.Color565(255, 0, 0);
    message = (utf8rus("тепловой удар       "));
  } // red
}
[/code]

разобрался с русским языком. теперь есть температура, влажность, индекс комфорта, часы 

правильная Adafruit GFX тут https://yadi.sk/d/FgUr5NPztVcRd

gulin176
Offline
Зарегистрирован: 03.09.2016