Hi, I have a problem with my code, I hope somebody is willing to help.
I have my TFT display and a button to open and close a servo motor. But once I add the lines with readings from DHT temperature and humid. sensor, the button doesn't work anymore.
I will attach the code below. thank you
/// TFT TOCUH SCREEEN DISPLAY
#include <TouchScreen.h> //touch library
#include <LCDWIKI_GUI.h> //Core graphics library
#include <LCDWIKI_KBV.h> //Hardware-specific library
//const char temper = "Temperatura si umiditate";
LCDWIKI_KBV my_lcd(ILI9486, A3, A2, A1, A0, A4); //model,cs,cd,wr,rd,reset
#define BLACK 0x0000 /* 0, 0, 0 */
#define BLUE 0x001F /* 0, 0, 255 */
#define RED 0xF800 /* 255, 0, 0 */
#define GREEN 0x07E0 /* 0, 255, 0 */
#define CYAN 0x07FF /* 0, 255, 255 */
#define MAGENTA 0xF81F /* 255, 0, 255 */
#define YELLOW 0xFFE0 /* 255, 255, 0 */
#define WHITE 0xFFFF /* 255, 255, 255 */
#define NAVY 0x000F /* 0, 0, 128 */
#define DARKGREEN 0x03E0 /* 0, 128, 0 */
#define DARKCYAN 0x03EF /* 0, 128, 128 */
#define MAROON 0x7800 /* 128, 0, 0 */
#define PURPLE 0x780F /* 128, 0, 128 */
#define OLIVE 0x7BE0 /* 128, 128, 0 */
#define LIGHTGREY 0xC618 /* 192, 192, 192 */
#define DARKGREY 0x7BEF /* 128, 128, 128 */
#define ORANGE 0xFD20 /* 255, 165, 0 */
#define GREENYELLOW 0xAFE5 /* 173, 255, 47 */
#define PINK 0xF81F /* 255, 0, 255 */
#define YP A3 // must be an analog pin, use "An" notation!
#define XM A2 // must be an analog pin, use "An" notation!
#define YM 9 // can be a digital pin
#define XP 8 // can be a digital pin
//touch sensitivity for X
#define TS_MINX 871 // 906
#define TS_MAXX 124 // 116
//touch sensitivity for Y
#define TS_MINY 207 // 92
#define TS_MAXY 842 // 952
// We have a status line for like, is FONA working
#define STATUS_X 10
#define STATUS_Y 65
#define MINPRESSURE 10
#define MAXPRESSURE 1000
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
boolean is_pressed(int16_t x1,int16_t y1,int16_t x2,int16_t y2,int16_t px,int16_t py)
{
if((px > x1 && px < x2) && (py > y1 && py < y2))
{
return true;
}
else
{
return false;
}
}
boolean switch_flag_1 = true,switch_flag_2 = true;
void show_string(uint8_t *str, int16_t x, int16_t y, uint8_t csize, uint16_t fc, uint16_t bc, boolean mode)
{
my_lcd.Set_Text_Mode(mode);
my_lcd.Set_Text_Size(csize);
my_lcd.Set_Text_colour(fc);
my_lcd.Set_Text_Back_colour(bc);
my_lcd.Print_String(str, x, y);
}
//// 8X8 Control
#include "LedControl.h"
#include "binary.h"
LedControl lc = LedControl(48, 47, 49, 1);
byte af[8] = {0x3c, 0x42, 0xa1, 0x87, 0x8f, 0xfb, 0x7e, 0x3c};
byte ad[8] = {0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81};
//////////////// senzor temp
#include <dht.h>
dht DHT;
#define DHT11_PIN 52
const int buttonPin = 53;
unsigned long delaytime = 100;
////////////////// KEYPAD + PASSWORD + SERVO
#include <Keypad.h>
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int closeServo = 1; // Adjust the fine movement
int openServo = 90; // Adjust the fine movement
int buttoncheck; // initializeaza verificare buton
//keypad
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the two-dimensional array on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {38, 39, 40, 41}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {42, 43, 44, 45}; //connect to the column pinouts of the keypad
//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
// PASSWORD MANAGER
const char* password = "32"; // change the password here, just pick any numbers/letters
int lengthpwd = 2; // change the length of the password
int carriage = 0;
int attempt = 0;
// LED and sound
//unsigned int redLed = 45;
unsigned int greenLed = 46;
unsigned int piezo = 50;
unsigned int note = 300;
// Millis
unsigned long currentMillis;
unsigned long previousMillis; // will store last time LED was updated
unsigned long previousMillis2;
int ledState = LOW; // ledState used to set the LED
// constants won't change:
const long interval = 800;
const long Tinterval = 2000; //constanta millis Tinterval pt DHT/senzor temperatura-umiditate
int prevTempLevel = 0;
void setup() {
////////////////////////////DISPLAY
my_lcd.Init_LCD();
my_lcd.Fill_Screen(OLIVE);
// linii
my_lcd.Draw_Line(15, 40, 305, 40);
my_lcd.Set_Draw_color(BLACK);
show_string("TEMPERATURA SI UMIDITATE", 15, 13, 2, BLUE, 1, WHITE);
//butoane
my_lcd.Set_Draw_color(BLACK);
my_lcd.Fill_Round_Rectangle(158, 358, 282, 462, 3);
my_lcd.Set_Draw_color(RED);
my_lcd.Fill_Round_Rectangle(160, 360, 280, 460, 3);
//////////////////////////////8X8 matrix
lc.shutdown(0, false);
// Set brightness to a medium value
lc.setIntensity(0, 4);
// Clear the display
lc.clearDisplay(0);
//buttonnnn
buttoncheck = 0;
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT_PULLUP);
// keypadddd
Serial.begin(9600);
myservo.attach(51); // attaches the servo on pin 10 to the servo object
myservo.write(closeServo);
// pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
//digitalWrite(redLed, 1);
tone(piezo, note, 20);
delay(500);
// digitalWrite(greenLed, 1);
tone(piezo, note, 20);
delay(500);
digitalWrite(greenLed, 0);
// digitalWrite(redLed, 0);
}
//////////8x8 LED DRAW
void drawFaces() {
lc.setRow(0, 0, af[0]);
lc.setRow(0, 1, af[1]);
lc.setRow(0, 2, af[2]);
lc.setRow(0, 3, af[3]);
lc.setRow(0, 4, af[4]);
lc.setRow(0, 5, af[5]);
lc.setRow(0, 6, af[6]);
lc.setRow(0, 7, af[7]);
}
void drawFaces2() {
lc.setRow(0, 0, ad[0]);
lc.setRow(0, 1, ad[1]);
lc.setRow(0, 2, ad[2]);
lc.setRow(0, 3, ad[3]);
lc.setRow(0, 4, ad[4]);
lc.setRow(0, 5, ad[5]);
lc.setRow(0, 6, ad[6]);
lc.setRow(0, 7, ad[7]);
}
void loop() {
//////////////////////////////////////////////////////////////////////////DISPLAY
digitalWrite(13, HIGH);
TSPoint p = ts.getPoint();
digitalWrite(13, LOW);
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
if (p.z > MINPRESSURE && p.z < MAXPRESSURE)
{
//p.x = my_lcd.Get_Display_Width()-map(p.x, TS_MINX, TS_MAXX, my_lcd.Get_Display_Width(), 0);
//p.y = my_lcd.Get_Display_Height()-map(p.y, TS_MINY, TS_MAXY, my_lcd.Get_Display_Height(), 0);
p.x = map(p.x, TS_MINX, TS_MAXX, my_lcd.Get_Display_Width(),0);
p.y = map(p.y, TS_MINY, TS_MAXY, my_lcd.Get_Display_Height(),0);
if(is_pressed(160, 400, 280, 480,p.x,p.y))
{
if(switch_flag_1)
{ //display off
myservo.write(openServo);
tone(piezo, 200, 100);
delay(80);
tone(piezo, 200, 50);
delay(80);
tone(piezo, 100, 50);
delay(80);
tone(piezo, 70, 50);
delay(100);
// digitalWrite(redLed, 1);
digitalWrite(greenLed, 1);
my_lcd.Set_Draw_color(GREEN);
my_lcd.Fill_Round_Rectangle(160, 360, 280, 460, 3);
drawFaces();
switch_flag_1 = false;
}
else
{ //display on
myservo.write(closeServo);
tone(piezo, 200, 100);
delay(80);
tone(piezo, 200, 50);
delay(80);
tone(piezo, 100, 50);
delay(80);
tone(piezo, 70, 50);
delay(100);
// digitalWrite(redLed, 1);
digitalWrite(greenLed, 0);
my_lcd.Set_Draw_color(RED);
my_lcd.Fill_Round_Rectangle(160, 360, 280, 460, 3);
drawFaces2();
switch_flag_1 = true;
}
}
}
///////////// keypad
char key = customKeypad.getKey();
unsigned long currentMillis = millis();
if (key) {
tone(piezo, note, 20);
Serial.println("");
Serial.println("______________");
Serial.print("Button pressed: ");
Serial.println(key);
Serial.print("Carriage: ");
Serial.println(carriage);
Serial.print("Attempt: ");
Serial.println(attempt);
Serial.println("_____________");
attempt++;
}
if (key == '*' || key == '#')
{
carriage = 0;
attempt = 0;
myservo.write(closeServo);
Serial.println("Clear");
my_lcd.Set_Draw_color(RED);
my_lcd.Fill_Round_Rectangle(160, 360, 280, 460, 3);
tone(piezo, 800, 300);
delay(80);
tone(piezo, 800, 100);
delay(80);
tone(piezo, 700, 100);
delay(80);
tone(piezo, 600, 100);
delay(100);
// digitalWrite(redLed, 1);
digitalWrite(greenLed, 1);
delay(200);
// digitalWrite(redLed, 0);
digitalWrite(greenLed, 0);
drawFaces2();
}
if (key == password[carriage])
{
carriage ++;
Serial.println("--> Right!");
}
if (carriage == lengthpwd)
{
Serial.println("--> Bingo! <--");
carriage = 0;
attempt = 0;
myservo.write(openServo);
my_lcd.Set_Draw_color(GREEN);
my_lcd.Fill_Round_Rectangle(160, 360, 280, 460, 3);
tone(piezo, 600, 100);
delay(50);
tone(piezo, 700, 100);
delay(50);
tone(piezo, 800, 100);
delay(60);
tone(piezo, 800, 100);
delay(80);
tone(piezo, 800, 300);
for (int i = 0; i < 6; i++) {
digitalWrite(greenLed, 1);
delay(200);
digitalWrite(greenLed, 0);
delay(100);
}
digitalWrite(greenLed, 1);
drawFaces();
}
if (attempt >= lengthpwd) {
Serial.println("--> Not the right code! <--");
// digitalWrite(redLed, 1);
tone(piezo, 450, 100);
delay(100);
tone(piezo, 400, 200);
delay(500);
// digitalWrite(redLed, 0);
carriage = 0;
attempt = 0;
}
if (carriage > lengthpwd)
{
Serial.println("Superior");
carriage = 0;
}
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
// digitalWrite(redLed, ledState);
}
//butonnn
if (digitalRead(53) == HIGH) {
myservo.write(closeServo);
buttoncheck = 1;
Serial.println("Clear");
tone(piezo, 200, 100);
delay(80);
tone(piezo, 200, 50);
delay(80);
tone(piezo, 100, 50);
delay(80);
tone(piezo, 70, 50);
delay(100);
// digitalWrite(redLed, 1);
digitalWrite(greenLed, 1);
delay(200);
// digitalWrite(redLed, 0);
digitalWrite(greenLed, 0);
drawFaces2();
}
if (digitalRead(53) == HIGH && buttoncheck == 1) {
myservo.write(openServo);
buttoncheck = 0;
myservo.write(openServo);
buttoncheck = 1;
Serial.println("Clear");
tone(piezo, 200, 100);
delay(80);
tone(piezo, 200, 50);
delay(80);
tone(piezo, 100, 50);
delay(80);
tone(piezo, 70, 50);
for (int i = 0; i < 6; i++) {
digitalWrite(greenLed, 1);
delay(200);
digitalWrite(greenLed, 0);
delay(100);
}
digitalWrite(greenLed, 1);
drawFaces();
}
/// TEMPERATURA
int t = DHT.temperature;
if ((t <= 100 ) && (t >= 0 )) {
int T_bar = map(t, 0, 50, 0, 80);
if ((prevTempLevel - T_bar) != 0) {
// my_lcd.Set_Draw_color(ORANGE);
// my_lcd.Fill_Rectangle(70, 150 , 100 , 300 - T_bar );
}
prevTempLevel = T_bar;
}
//loop pt DHT senzor temperatura-umiditate
currentMillis = millis();
if (currentMillis - previousMillis2 >= Tinterval) {
previousMillis2 = currentMillis;
int chk = DHT.read11(DHT11_PIN);
// Serial.print("Temperatura = ""°");
//Serial.println(DHT.temperature);
// Serial.print("Umiditate = ""%");
//Serial.println(DHT.humidity);
} // temperatura si umiditate display
show_string("C", 245, 260, 3, ORANGE, OLIVE, 0);
my_lcd.Set_Text_Mode(0);
my_lcd.Set_Text_Back_colour(OLIVE);
my_lcd.Print_Number_Float(DHT.temperature, 1, 160, 260, '.', 3, ' ');
show_string("%", 245, 300, 3, CYAN, OLIVE, 0);
my_lcd.Set_Text_Mode(0);
my_lcd.Set_Text_Back_colour(OLIVE);
my_lcd.Print_Number_Float(DHT.humidity, 1, 160, 300, '.', 3, ' ');
}