Arduino 连接扬声器喇叭,温湿度传感器,声音传感器并局部刷新ILI9486 LCD,LED
最近玩Arduino时,有一块ILI9486错都LCD屏,它没有局部刷新功能,所以就把背景设置为黑色,然后用变量记住光标的位置,用设置文字颜色为黑色的方法在原位置重新显示相同的字符,这样旧的数据就消失了,然后再改文字的颜色为绿色重新显示新的值。效果不错,完全满足局部刷新
的效果,分享给大家。希望可以帮助到你。
#include <MCUFRIEND_kbv.h>
#include <Adafruit_GFX.h> // Core graphics library
// wiring with UNO or Mega2560:
//--------------POWER Pins--------------------------------
// 5V connects to DC 5V
// GND connects to Ground
// 3V3 do not need to connect(NC)
//--------------LCD Display Pins--------------------------
// LCD_RD connects to Analog pin A0
// LCD_WR connects to Analog pin A1
// LCD_RS connects to Analog pin A2
// LCD_CS connects to Analog pin A3
// LCD_RST connects to Analog pin A4
// LCD_D0 connects to digital pin 8
// LCD_D1 connects to digital pin 9
// LCD_D2 connects to digital pin 2
// LCD_D3 connects to digital pin 3
// LCD_D4 connects to digital pin 4
// LCD_D5 connects to digital pin 5
// LCD_D6 connects to digital pin 6
// LCD_D7 connects to digital pin 7
//--------------SD-card fuction Pins ----------------------
//This Connection Only for UNO, Do not support Mega2560
//because they use different Hardware-SPI Pins
//SD_SS connects to digital pin 10
//SD_DI connects to digital pin 11
//SD_DO connects to digital pin 12
//SD_SCK connects to digital pin 13
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#define DHTTYPE DHT11 // DHT 11
#define DHTPIN 11
DHT_Unified dht(DHTPIN, DHTTYPE);
uint32_t delayMS;
int voice = A7;
const int BEEPPIN = 22;
#define LCD_RESET A4 // Can alternately just connect to Arduino's reset pin
#define LCD_CS A3 // Chip Select goes to Analog 3
#define LCD_CD A2 // Command/Data goes to Analog 2
#define LCD_WR A1 // LCD Write goes to Analog 1
#define LCD_RD A0 // LCD Read goes to Analog 0
// 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
float TemperatureOld = 00;
float HumidityOld = 00;
int VoiceOld = 0;
int locationX5 = -100;
int locationY5 = -100;
MCUFRIEND_kbv tft;
void setup() {
pinMode(BEEPPIN, OUTPUT);
digitalWrite(BEEPPIN, HIGH);
delay(100);
digitalWrite(BEEPPIN, LOW);
delay(100);
pinMode(voice, INPUT);
dht.begin();
sensor_t sensor;
delayMS = sensor.min_delay / 1000;
Serial.begin(9600);
Serial.println(F("Temperature and Humidity Test Starting..."));
tft.fillScreen(BLACK);
tft.setCursor(0, 0);
tft.println();
tft.setTextColor(GREEN);
tft.setTextSize(2);
tft.reset();
uint16_t identifier = tft.readID();
tft.begin(0x9486);
tft.fillScreen(BLACK);
}
void loop(void) {
tft.setTextColor(GREEN);
int valVoice = analogRead(voice);
//tft.fillScreen(BLACK);
tft.setCursor(5, 0);
sensors_event_t event;
dht.temperature().getEvent(&event);
tft.println();
tft.print(F("Temperature: "));
tft.setTextColor(BLACK);
int locationX = tft.getCursorX();
int locationY = tft.getCursorY();
tft.print(TemperatureOld);
tft.setTextColor(GREEN);
tft.setCursor(locationX, locationY);
tft.print(event.temperature);
TemperatureOld = event.temperature;
tft.println(F(" C"));
dht.humidity().getEvent(&event);
tft.println();
tft.print(F("Humidity: "));
tft.setTextColor(BLACK);
int locationX2 = tft.getCursorX();
int locationY2 = tft.getCursorY();
tft.print(HumidityOld);
tft.setTextColor(GREEN);
tft.setCursor(locationX2, locationY2);
tft.print(event.relative_humidity);
tft.println(F(" %"));
HumidityOld = event.relative_humidity;
tft.println();
tft.print("Voice Value = ");
int locationX3 = tft.getCursorX();
int locationY3 = tft.getCursorY();
tft.setCursor(locationX3, locationY3);
tft.setTextColor(BLACK);
tft.print(VoiceOld);
tft.setCursor(locationX3, locationY3);
tft.setTextColor(GREEN);
tft.print(valVoice);
Serial.print(analogRead(voice));
VoiceOld = valVoice;
if (valVoice == 1023) {
tft.println();
tft.setCursor(5, 150);
tft.print("BEEP is =");
locationX5 = tft.getCursorX();
locationY5 = tft.getCursorY();
tft.setCursor(locationX5, locationY5);
tft.setTextColor(BLACK);
tft.print(" OFF ");
tft.setTextColor(GREEN);
tft.setCursor(locationX5, locationY5);
tft.print(" ON ");
digitalWrite(BEEPPIN, LOW);
delay(50);
}
digitalWrite(BEEPPIN, HIGH);
tft.setCursor(locationX5, locationY5);
tft.setTextColor(BLACK);
tft.print(" ON ");
delay(300);
tft.setCursor(locationX5, locationY5);
tft.setTextColor(GREEN);
tft.print(" OFF ");
delay(3000);
tft.flush();
tft.setTextColor(GREEN);
}