1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
| #include<math.h> #include <Adafruit_GFX.h> #include <MCUFRIEND_kbv.h> MCUFRIEND_kbv tft; #include <TouchScreen.h>
#define BLACK 0x0000 #define BLUE 0x001F #define RED 0xF800 #define GREEN 0x07E0 #define CYAN 0x07FF #define MAGENTA 0xF81F #define YELLOW 0xFFE0 #define WHITE 0xFFFF
const int XP = 8, XM = A2, YP = A3, YM = 9; const int TS_LEFT = 922, TS_RT = 122, TS_TOP = 53, TS_BOT = 933;
#define MINPRESSURE 200 #define MAXPRESSURE 1000
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300); TSPoint tp;
byte touch;
const uint16_t line_x = 8, line_y = 20, line_distance = 40;
const uint16_t initial_x = 300 , initial_y = line_y - 3 , side = 30, x_distance = 50, y_distance = 40 , symbol_x = 7, symbol_y = 5;
int16_t BOXSIZE; int16_t PENRADIUS = 1; uint16_t ID, oldcolor, currentcolor; const byte Orientation = 1;
#define Gate 44
const float voltagepower = 5.0; const float R = 51; const int B = 3950; const double T1 = 273.15 + 25; const double R1 = 100; const byte analogid = 8;
byte MinTemp = 25; byte MaxTemp = 32;
byte analogvalue;
void show_threshold(void) { tft.setCursor(line_x, line_y); tft.print("MinTemp:"); tft.print(MinTemp); tft.println(" \367C"); tft.setCursor(line_x, line_y + line_distance * 1); tft.print("MaxTemp:"); tft.print(MaxTemp); tft.println(" \367C"); }
void showTempAndControlFan(void) { double digitalValue = analogRead(analogid); double voltageValue = (digitalValue / 1023) * 5; double Rt = ((voltagepower - voltageValue) * R) / voltageValue; double temp = ((T1 * B) / (B + T1 * log(Rt / R1))) - 273.15; tft.setCursor(line_x, line_y + line_distance * 2); tft.print("Current Temp: "); tft.print((int)temp); tft.print(" \367C");
if (temp >= MaxTemp) { analogvalue = 255; } else if (temp >= MinTemp) { analogvalue = map(temp, MinTemp, MaxTemp, 128, 255); } else { analogvalue = 0; } analogWrite(Gate, analogvalue); }
void show_fan(void) { for (int i = 1; i <= 4; i++) { tft.setCursor(line_x, line_y + line_distance * (2 + 1 * i)); tft.print("Fan"); tft.print(i); tft.print(" Speed: "); if (analogvalue == 255) { tft.print("100%"); } else if (analogvalue == 0) { tft.print(" 0%"); } else { tft.print(" "); tft.print(analogvalue * 100 / 255); tft.print("%"); } } }
void setup() { Serial.begin(9600); Serial.println("start...");
tft.reset(); ID = tft.readID(); tft.begin(ID); tft.setRotation(Orientation); tft.fillScreen(BLACK); tft.setTextColor(WHITE); tft.setTextSize(2);
tft.fillRect(initial_x, initial_y, side, side, MAGENTA); tft.fillRect(initial_x + x_distance, initial_y, side, side, MAGENTA); tft.fillRect(initial_x, initial_y + y_distance , side, side, MAGENTA); tft.fillRect(initial_x + x_distance, initial_y + y_distance, side, side, MAGENTA); tft.setCursor(initial_x + symbol_x, initial_y + symbol_y); tft.print("+"); tft.setCursor(initial_x + symbol_x + x_distance, initial_y + symbol_y); tft.print("-"); tft.setCursor(initial_x + symbol_x, initial_y + symbol_y + y_distance); tft.print("+"); tft.setCursor(initial_x + symbol_x + x_distance, initial_y + symbol_y + y_distance); tft.print("-"); tft.setTextColor(WHITE, BLACK); Serial.println("Screen is " + String(tft.width()) + "x" + String(tft.height())); }
void loop() { show_threshold(); showTempAndControlFan(); show_fan();
uint16_t xpos, ypos; tp = ts.getPoint();
pinMode(XM, OUTPUT); pinMode(YP, OUTPUT);
if (tp.z > MINPRESSURE && tp.z < MAXPRESSURE) { xpos = map(tp.y, TS_TOP, TS_BOT, 0, tft.width()); ypos = map(tp.x, TS_RT, TS_LEFT, 0, tft.height()); Serial.println(String(xpos) + "," + String(ypos)); if (xpos > initial_x && xpos < initial_x + side) { if (ypos > initial_y && ypos < initial_y + side) { touch = 1; } } if (xpos > initial_x + x_distance && xpos < initial_x + x_distance + side) { if (ypos > initial_y && ypos < initial_y + side) { touch = 2; } } if (xpos > initial_x && xpos < initial_x + side) { if (ypos > initial_y + y_distance && ypos < initial_y + y_distance + side) { touch = 3; } } if (xpos > initial_x + x_distance && xpos < initial_x + x_distance + side) { if (ypos > initial_y + y_distance && ypos < initial_y + y_distance + side) { touch = 4; } } }
if (touch == 1) { MinTemp++; touch = 0; delay(200); }
if (touch == 2) { MinTemp--; touch = 0; delay(200); }
if (touch == 3) { MaxTemp++; touch = 0; delay(200); }
if (touch == 4) { MaxTemp--; touch = 0; delay(200); }
}
|