I have tried almost every tutorial, read all I could find on displaying images. Nothing is working. Can you fine folks look a the code and tell me what you see I am doing wrong? I used the Adafruit image2code tool to convert the image.
While your looking at it, can you tell how to debounce the encoder? The adjustment is not very reliable and it doesn't seem to have to take the input all the time. Thanks!
#include <SPI.h>
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;
#include <gfxfont.h>
#include <Adafruit_GFX.h>
//#include <TouchScreen.h>
#include <Adafruit_TFTLCD.h>
#include <Fonts/FreeMonoBoldOblique12pt7b.h>
#include <Fonts/FreeSans12pt7b.h>
#include <Fonts/FreeSerif9pt7B.h>
//Touch Screen Calibartion
const int XP=7,XM=A1,YP=A2,YM=6; //240x400 ID=0x7793
const int TS_LEFT = 142, TS_RT = 958, TS_TOP = 934, TS_BOT = 166;
//define RGB565
#define TFT_BLACK 0x0000
#define TFT_NAVY 0x000F
#define TFT_DARKGREEN 0x03E0
#define TFT_DARKCYAN 0x03EF
#define TFT_MAROON 0x7800
#define TFT_PURPLE 0x780F
#define TFT_OLIVE 0x7BE0
#define TFT_LIGHTGREY 0xC618
#define TFT_DARKGREY 0x7BEF
#define TFT_BLUE 0x001F
#define TFT_GREEN 0x07E0
#define TFT_CYAN 0x07FF
#define TFT_RED 0xF800
#define TFT_MAGENTA 0xF81F
#define TFT_YELLOW 0xFFE0
#define TFT_WHITE 0xFFFF
#define TFT_ORANGE 0xFDA0
#define TFT_GREENYELLOW 0xB7E0
#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
#define LCD_RESET -1 // Can alternately just connect to Arduino's reset pin
//DHT SENSOR
#include <DHT.h>
#include <DHT_U.h>
#define DHTPIN 53
#define DHTTYPE DHT22
DHT dht (DHTPIN, DHTTYPE);
//#define SETPOINT 75
#define DEADBAND 2
#define MISTER 37
#define ON true
#define OFF false
int setPoint=75;
//Rotray Encorder
const int clkPin= 31;
const int dtPin= 33;
const int swPin= 35;
int aState;
int aLastState;
void setup() {
pinMode (clkPin, INPUT);
pinMode (dtPin, INPUT);
pinMode (swPin, INPUT);
pinMode (MISTER, OUTPUT);
digitalWrite (MISTER, OFF);
dht.begin();
//TFT Display Boot
Serial.begin(9600);
tft.reset();
uint16_t ID=tft.readID(), w, h; //auto read identifier of TFT
Serial.print("TFT ID = 0x");
Serial.println(ID, HEX);
int16_t x1, y1;
tft.begin(ID); //get ready
tft.setRotation(1);
void setRotation(uint8_t rotation3);
tft.setFont(&FreeSans12pt7b);
tft.fillScreen(TFT_BLACK);
//tft.drawRect(5, 5, 390, 230, TFT_WHITE);
tft.setCursor(135, 35);
tft.setTextColor(TFT_LIGHTGREY);
tft.setTextSize(1);
tft.println("GRATUS CO");
tft.setCursor(90, 60);
tft.setTextSize(1);
tft.println("LUXURY HUMIDORS");
//tft.setCursor(105, 80);
//tft.setTextSize(1);
//tft.println("www.gratusco.com");
tft.setTextColor(TFT_WHITE);
tft.setTextSize(1);
tft.setCursor(15, 117);
tft.println("Humidity Setpoint: ");
tft.setCursor(15, 147);
tft.println("Current Humidity: ");
tft.setCursor(15, 178);
tft.println("Current Temp: ");
//tft.drawRect(74, 194, 102, 32, TFT_WHITE);
//tft.setCursor(87, 218);
//tft.print("DOWN");
//tft.drawRect(224, 194, 102, 32, TFT_WHITE);
//tft.setCursor(260, 218);
//tft.print("UP");
tft.setFont();
}
void loop() {
displayText();
adjustRH();
sensorRead();
}
//TFT Display text
void displayText() {
tft.setFont();
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.setTextSize(2);
tft.setCursor(220, 105);
tft.print(setPoint);
tft.println(" %");
tft.setCursor(220, 135);
tft.print(dht.readHumidity());
tft.println(" %RH");
tft.setCursor(220, 165);
tft.print(dht.readTemperature()*9/5+32);
tft.println(" F");
//tft.drawBitmap(20, 5, lion, 60, 60, TFT_WHITE);
extern const uint8_t lion[];
tft.setAddrWindow(10, 10, 61, 61);
tft.pushColors(lion, 3200, 1);
//return;
}
void drawBitmap(int16_t x, int16_t y,
const uint8_t *bitmap, int16_t w, int16_t h, uint16_t color){
int16_t i, j, byteWidth = (w + 7) / 8;
uint8_t byte;
for(j=0; j<h; j++) {
for(i=0; i<w; i++) {
if (i & 7) byte <<= 1;
else byte = pgm_read_byte(bitmap + j * byteWidth + i/8);
if (byte & 0x80) tft.drawPixel(x+i, y+j, color);
}
}
}
void adjustRH() {
int change = getEncoderTurn();//
setPoint = setPoint + change;
if(digitalRead(swPin) == LOW)//if button pull down
{setPoint = 75;}
}
int getEncoderTurn(void)
{
static int oldA = HIGH; //set the oldA as HIGH
static int oldB = HIGH; //set the oldB as HIGH
int result = 0;
int newA = digitalRead(clkPin);//read the value of clkPin to newA
int newB = digitalRead(dtPin);//read the value of dtPin to newB
if (newA != oldA || newB != oldB) //if the value of clkPin or the dtPin has changed
{
// something has changed
if (oldA == HIGH && newA == LOW) setPoint ++;
{
result = (oldB * 2 - 1);
setPoint --;
}
}
oldA = newA;
oldB = newB;
return result;
}
//Reading the sensor
void sensorRead() {
float h = dht.readHumidity();
float t = dht.readTemperature();
//Display and read-out code:
//Serial.begin (9600);
//Serial.print ("Humidity: "); Serial.print (h); Serial.println (" % ");
//Serial.print ("Temperature: "); Serial.print (t*9/5+32); Serial.print (" °F ");
//Serial.print ("RH Setpoint: "); Serial.println (setPoint);
//Serial.print ("Touched?: "); Serial.println (Touch_getXY());
//Serial.print ("Rotarty SW: "); Serial.println (digitalRead (swPin));
//Serial.print ("X = "); Serial.print (p_x); Serial.print("Y = "); Serial.println (p_y);
//Serial.print("Width: "); Serial.print (ww); Serial.print (" Height: "); Serial.println(hh);
//Humidor control logic:
if (digitalRead(MISTER) == ON)
{
if (h > setPoint + DEADBAND)
{
digitalWrite(MISTER, OFF);
tft.drawCircle(360, 142, 11, TFT_WHITE);
tft.fillCircle(360, 142, 10, TFT_DARKGREY);
}
}
else
{
if (h < setPoint - DEADBAND)
{
digitalWrite (MISTER, ON);
tft.drawCircle(360, 142, 11, TFT_WHITE);
tft.fillCircle(360, 142, 10, TFT_GREEN);
}
}
}