Problem z kodem w tft LCD - Kuba323 - 16-08-2017
Witam,
Jestem początkującym i natknąłem się na problem, przez który nie mogę przebrnąć, mianowicie chciałbym zrobić stacje pogody z dotykowego wyświetlacza i na tym wyświetlaczu ekran startowy miałby się odświeżać co 1 min. ale nie wiem jak to ująć w kodzie próbowałem już z biblioteką timers i millis, ale mi nie wychodziło. Bardzo proszę o pomoc z góry dziękuję.
tutaj kod:
Kod: //#include <Timers.h>
#include <DS3231.h>
#include <SPFD5408_Adafruit_GFX.h>
#include <SPFD5408_Adafruit_TFTLCD.h>
#include <SPFD5408_TouchScreen.h>
#include <Wire.h>
//Timer mojTimer;
DS3231 clock;
RTCDateTime dt;
#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
#define LCD_RT 10
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
#define Orange 0xFD20
#define GreenYellow 0xAFE5
#define Navy 0x000F
#define DarkGreen 0x03E0
#define DarkCyan 0x03EF
#define Maroon 0x7800
#define Purple 0x780F
#define Olive 0x7BE0
#define LightGrey 0xC618
#define DarkGrey 0x7BEF
#define TS_MINX 100
#define TS_MINY 100
#define TS_MAXX 1000
#define TS_MAXY 1000
Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RT);
#define YP A2
#define XM A3
#define YM 8
#define XP 9
#define MINPRESSURE 200
#define MAXPRESSURE 350
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
long previousMillis = 0;
int interval = 1000;
char page;
void setup() {
tft.reset();
tft.begin(0x9341);
page = 0;
Serial.begin(9600);
Serial.println("Initialize DS3231");
Wire.begin();
clock.begin();
clock.setDateTime(2017, 8, 16, 19, 50, 00);
powitanie();
}
void loop() {
dt = clock.getDateTime();
dotyk();
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
ekranstartowy();
}
}
void dotyk () {
TSPoint p = ts.getPoint();
if (p.z > MINPRESSURE && p.z < MAXPRESSURE){
p.x = map(p.x, TS_MAXX, TS_MINX, 240, 0);
p.y = map(p.y, TS_MAXY, TS_MINY,320, 0);
if (page == 0) {
if(p.x>100 && p.x<155 && p.y>50 && p.y<300){
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
drawpg ();
page = 1;
}
if(p.x>150 && p.x<200 && p.y>50 && p.y<300){
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
drawtemp ();
page = 2;
}
}
}
if (page == 1) {
if(p.x>50 && p.x<100 && p.y>50 && p.y<300){
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
ekranstartowy();
page = 0;
}
}
if (page == 2) {
if(p.x>50 && p.x<100 && p.y>50 && p.y<300) {
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
ekranstartowy();
page = 0;
}
}
}
void ekranstartowy (){
tft.fillScreen(DarkCyan);
tft.setTextSize(2);
tft.setTextColor(Orange);
tft.setCursor(10,10);
tft.print("Data:");
tft.println(clock.dateFormat("d-m-Y", dt)) ;
tft.drawLine(0,33, 10000, 40, Orange);
tft.setCursor(10,40);
tft.print("Godzina:");
tft.println(clock.dateFormat("H:i:s", dt));
tft.drawLine(0,65, 10000, 40, Orange);
tft.setCursor(10,72);
tft.print("Temperatura:");
tft.setCursor(150,72);
//tft.print(clock.readTemperature());
tft.drawLine(0,95, 10000, 40, Orange);
tft.fillRect(10,110, 220, 80, DarkGreen);
tft.setTextSize(3);
tft.setTextColor(Orange);
tft.setCursor(20,120);
tft.print("Pogoda na ");
tft.setCursor(20, 150);
tft.print("polu");
tft.fillRect(10,210, 220, 80, DarkGreen);
tft.setTextSize(3);
tft.setTextColor( Orange);
tft.setCursor(20,220);
tft.print("Temperatura");
tft.setCursor(20, 250);
tft.print("na piecu");
}
void drawpg () {
tft.fillScreen(DarkCyan);
tft.setTextSize(3);
tft.fillRect(10,20,220,80, DarkGreen);
tft.setTextColor(Orange);
tft.setCursor(80,50);
tft.print("<-");
tft.setTextColor (Orange);
tft.setCursor(10, 120);
tft.print("Wilgotnosc");
tft.setTextColor(Orange);
tft.setCursor(10,210);
tft.print("Temperatura");
}
void drawtemp (){
tft.fillScreen(DarkCyan);
tft.setTextColor(Orange);
tft.setTextSize(3);
tft.setCursor(10,150);
tft.print("Temperatutra");
tft.fillRect(10,20,220,80, DarkGreen);
tft.setTextColor(Orange);
tft.setCursor(80,50);
tft.print("<-");
}
void powitanie () {
tft.fillScreen(DarkCyan);
tft.setTextColor(Orange);
tft.setTextSize(4);
tft.setCursor(50,70);
tft.print("Witaj");
}
RE: Problem z kodem w tft LCD - adix - 17-08-2017
Spróbuj zapętlić na while albo for może być pusta pętla w której zrobisz odliczanie ale to za mrozi program na minute
Wysłane z mojego Redmi Note 4X przy użyciu Tapatalka
RE: Problem z kodem w tft LCD - Kuba323 - 17-08-2017
Ale właśnie o to chodzi żeby program nie był zamrożony, bo dotyk nie będzie działał. Najlepsza byłaby funkcja millis, ale nie wiem jak jej użyć.
RE: Problem z kodem w tft LCD - adix - 17-08-2017
To odświeżanie dotyku wluz w pętlę jeśli co kolejek zrobisz to wyskoczysz z nie i wykonasz funkcje masz to zrobione be funkcjach więc nie ma z tym problemu
Wysłane z mojego Redmi Note 4X przy użyciu Tapatalka
RE: Problem z kodem w tft LCD - Robson Kerman - 21-08-2017
Przepraszam, że się wtrącam, ale takie rzeczy robi się na przerwaniach.
RE: Problem z kodem w tft LCD - kaczakat - 26-10-2017
Witam
Do przykładu z "Blink without Delay" wystarczy dodać swoje flagi sterujące i liczniki. Biblioteki robią to bardziej elegancko. W Twoim kodzie wystarczy zmienić zmienną int interval = 60000; i kod w tym warunku wykona się co 60s. Interwał powinien być tak dobrany, by można było sterować tym co ma być wykonane najczęściej, a resztę obrabiać licznikami. Np. sterowanie asynchronicznym miganiem trzema ledami:
Kod: /*
Blink without Delay
Turns on and off a light emitting diode (LED) connected to a digital pin,
without using the delay() function. This means that other code can run at the
same time without being interrupted by the LED code.
The circuit:
- Use the onboard LED.
- Note: Most Arduinos have an on-board LED you can control. On the UNO, MEGA
and ZERO it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN
is set to the correct LED pin independent of which board is used.
If you want to know what pin the on-board LED is connected to on your
Arduino model, check the Technical Specs of your board at:
https://www.arduino.cc/en/Main/Products
created 2005
by David A. Mellis
modified 8 Feb 2010
by Paul Stoffregen
modified 11 Nov 2013
by Scott Fitzgerald
modified 9 Jan 2017
by Arturo Guadalupi
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
*/
// constants won't change. Used here to set a pin number:
const int ledPin1 = 11;// the number of the LED pin
const int ledPin2 = 12;// the number of the LED pin
const int ledPin3 = LED_BUILTIN;// the number of the LED pin
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0; // will store last time LED was updated
// constants won't change:
const long interval = 100; // interval at which to blink (milliseconds)
uint8_t flaga100MS,licznikMS, flaga1S, licznikS,flaga1M,licznikM;// flagi pokazują czy upłynęła ta jednostka czasu, liczniki je zliczają
void setup() {
Serial.begin(115200);
// set the digital pin as output:
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
}
void loop() {
//zabawy z licznikami na poczatku loop
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
licznikMS++;
flaga100MS=1;
}
if(licznikMS>=10)
{
licznikMS=0;
licznikS++;
flaga1S=1;
}
if (licznikS>=60)
{
licznikS=0;
licznikM++;
flaga1M=1; //minuty się wyzerują po przepełnieniu zmiennej
}
//rzeczy do zrobienia
if(flaga100MS) //gdy minie 100ms
{
if(licznikMS%4==0) //to wykona sie co 400ms
{
digitalWrite(ledPin1,!(digitalRead(ledPin1)));//zmiana stanu wyjscia ledPin1
}
}
if(flaga1S) //gdy minie sekunda
{
Serial.print(licznikM);
Serial.print(":");
Serial.println(licznikS);
if(licznikS%3==0) //to wykona sie co 3s
{
digitalWrite(ledPin2,!(digitalRead(ledPin2)));//zmiana stanu wyjscia ledPin2
}
}
if(flaga1M) //gdy minie minuta
{
Serial.print("Mintka");
if(licznikM%2==0) //to wykona się co 2 minuty
{
digitalWrite(ledPin3,!(digitalRead(ledPin3)));//zmiana stanu wyjscia ledPin3
}
}
//zerujemy flagi na koniec petli loop
flaga100MS=0;flaga1S=0;flaga1M=0;
}
Im więcej do zrobienia w programie tym bardziej się takie liczenie milisami rozjeżdża.
|