Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.44 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <Adafruit_Sensor.h>
  3. #include <Adafruit_GFX.h> // Core graphics library
  4. #include <Adafruit_ST7735.h> // Hardware-specific library
  5. #include <Adafruit_BME280.h>
  6. #include <SPI.h>
  7. #include <math.h>
  8. #include "Adafruit_TMP007.h"
  9. #define TFT_CS 10
  10. #define TFT_RST 0 // Tied to Arduino Reset
  11. #define TFT_DC 8 // A0 China Board
  12. #define TFT_SCLK 13 // set these to be whatever pins you like!
  13. #define TFT_MOSI 11 // SDA CHINA
  14. #define SEALEVELPRESSURE_HPA (1013.25)
  15. int UVOUT = A0; //Output from ML8511
  16. int REF_3V3 = A3; //3.3V power for Reference calculation
  17. float dew;
  18. float pressure;
  19. double VP;
  20. float temp;
  21. float hum;
  22. int page = 1;
  23. int selection = 0;
  24. unsigned long color;
  25. int pagechange = 0;
  26. int keyHit = 0;
  27. int changescreen = 0;
  28. int sleepstatus = 1;
  29. int brightness = 200;
  30. volatile int readstatus = 0;
  31. volatile int ButtonSelection = 0;
  32. Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
  33. Adafruit_BME280 bme;
  34. Adafruit_TMP007 tmp007;
  35.  
  36.  
  37. void setup(void) {
  38. analogWrite(5, brightness); // Screen Backlight set
  39. Wire.begin();
  40. buttonrequest();
  41. bme.begin();
  42. tmp007.begin(TMP007_CFG_16SAMPLE);
  43. pinMode(UVOUT, INPUT);
  44. pinMode(REF_3V3, INPUT);
  45. pinMode(2, INPUT_PULLUP);
  46. pinMode(3, INPUT_PULLUP);
  47. pinMode(9, OUTPUT);
  48. analogWrite(5, brightness); // Screen Backlight set
  49. digitalWrite(7, LOW);
  50. Serial.begin(9600);
  51. attachInterrupt(digitalPinToInterrupt(2), buttonread, FALLING);
  52. tft.initR(INITR_BLACKTAB); // initialize a ST7735S chip
  53. for (int i = 0x20; i <=0x24; i++) // Loop to set Button Negative Thresholds
  54. {
  55. Wire.beginTransmission(0x1B);
  56. Wire.write(i);
  57. Wire.write(15);
  58. Wire.endTransmission();
  59. delay(100);
  60. }
  61.  
  62. Serial.println("Setup Complete");
  63. }
  64.  
  65. void loop() {
  66.  
  67. if (page == 1) homescreen();
  68. if (page == 2) temperature();
  69. if (page == 3) wind();
  70. if (page == 4) light();
  71. if (page == 5) altitude();
  72. if (page == 6) humidity();
  73. if (page == 7) settings();
  74. }
  75.  
  76.  
  77. void settings(){
  78. int select = 0;
  79. int brightnessbar;
  80. detachInterrupt(digitalPinToInterrupt(2));
  81. tft.fillScreen(ST7735_BLACK);
  82. for (int box = 0; box <=3; box++){ //predraw selection boxes
  83. tft.drawRect(0, 20+(box*35), 128, 34, ST7735_YELLOW);
  84. }
  85.  
  86. tft.fillRect(5, 34, 118, 15, ST7735_YELLOW); // predraw Brightness
  87. brightnessupdate();
  88.  
  89.  
  90. tft.setCursor(35, 5);tft.print("SETTINGS");
  91. tft.setCursor(30, 24);tft.print("BRIGHTNESS");
  92. tft.setCursor(25, 58);tft.print("AUTO-OFF TIME");
  93. tft.setCursor(45, 93);tft.print("UNITS");
  94. tft.setCursor(11, 128);tft.print("SEA LEVEL PRESSURE");
  95. tft.setTextSize(2);
  96. while (changescreen == 0) {
  97. tft.drawRect(0, 20+(select*35), 128, 34, ST7735_WHITE);
  98. tft.drawRect(1, 21+(select*35), 126, 32, ST7735_WHITE);
  99. while (readstatus == 0){
  100. buttonrequest();
  101. }
  102. if (ButtonSelection == 1 || ButtonSelection == 8) {
  103. tft.drawRect(0, 20+(select*35), 128, 34, ST7735_YELLOW);
  104. tft.drawRect(1, 21+(select*35), 126, 32, ST7735_BLACK);
  105. if (ButtonSelection == 1) select = select - 1;
  106. if (ButtonSelection == 8) select = select + 1;
  107. if (select < 0) select = 3;
  108. if (select > 3) select = 0;
  109.  
  110. while (readstatus == 1) buttonrequest();
  111. }
  112.  
  113. if (ButtonSelection == 2 || ButtonSelection == 16) {
  114. if (ButtonSelection == 2 && brightness > 25) brightness = brightness - 2;
  115. if (ButtonSelection == 16 && brightness < 250) brightness = brightness + 2;
  116.  
  117. analogWrite(5, brightness); // Screen Backlight set
  118. brightnessupdate();
  119. }
  120.  
  121. if (ButtonSelection == 4){
  122. changescreen = 1;
  123. }
  124.  
  125.  
  126. readstatus = 0;
  127. // if (ButtonSelection == 16) right = 1;
  128. // if (ButtonSelection == 2) left = 1;
  129. // if (ButtonSelection == 4) select = 1;
  130.  
  131.  
  132. }
  133.  
  134. attachInterrupt(digitalPinToInterrupt(2), buttonread, FALLING);
  135.  
  136. Navigate();
  137.  
  138. }
  139.  
  140. void homescreen(){
  141.  
  142. tft.fillScreen(ST7735_BLACK);
  143. tft.setTextSize(1);
  144. tft.setTextColor(0xFE60, ST7735_BLACK);
  145. tft.setCursor(3, 0);tft.print("Aer One");
  146. tft.drawRect(88, 1, 15, 6, 0xFE60);
  147. tft.fillRect(90, 3, 8, 2, 0xFE60);
  148.  
  149.  
  150. tft.setTextSize(2);
  151. tft.setTextColor(ST7735_BLACK);
  152. drawButton(255,0,54,0,152,119,4,11);
  153. tft.setCursor(45, 15);tft.print("TEMP");
  154.  
  155. drawButton(255,91,0,0,101,0,4,36);
  156. tft.setCursor(45, 40);tft.print("WIND");
  157. drawButton(255,206,0,0,41,175,4,61);
  158. tft.setCursor(18, 65);tft.print("UV LIGHT");
  159. drawButton(59,170,72,0,63,0,4,86);
  160. tft.setCursor(18, 90);tft.print("PRESSURE");
  161. drawButton(0,100,255,0,100,0,4,111);
  162. tft.setCursor(18, 115);tft.print("HUMIDITY");
  163. drawButton(154,49,255,65,117,0,4,136);
  164. tft.setCursor(18, 140);tft.print("SETTINGS");
  165. tft.setTextSize(1);tft.setTextColor(ST7735_YELLOW,ST7735_BLACK);
  166. while (changescreen == 0) {
  167.  
  168.  
  169. tft.drawRect(3, (selection * 25)+10, 122, 24, ST7735_WHITE);
  170. tft.drawRect(4, (selection * 25)+11, 120, 22, ST7735_WHITE);
  171. while (ButtonSelection == 0) {
  172.  
  173. int battery = mapfloat(averageAnalogRead(7), 650, 850, 1, 100); // Air Indicator Check and Update
  174. tft.setCursor(107, 0);tft.print(battery); tft.print("%"); //battery indicator
  175. delay(1);
  176. }
  177. tft.drawRect(3, (selection * 25)+10, 122, 24, ST7735_BLACK);
  178. tft.drawRect(4, (selection * 25)+11, 120, 22, ST7735_BLACK);
  179. if (ButtonSelection == 1){
  180. selection = selection - 1;
  181. if (selection < 0) selection = 5;
  182. ButtonSelection = 0;
  183. }
  184. if (ButtonSelection == 8){
  185. selection = selection + 1;
  186. if (selection > 5) selection = 0;
  187. ButtonSelection = 0;
  188. }
  189. if (ButtonSelection == 4){
  190. changescreen = 1;
  191. ButtonSelection = 0;
  192. page = selection +2;
  193. }
  194. }
  195. Navigate();
  196. }
  197.  
  198. void temperature(){
  199. tft.fillScreen(ST7735_BLACK);
  200. float g = 0; float b = 255; float r = 0;
  201. int aircheck;
  202. int ircheck = 0;
  203. unsigned long gradientcolor;
  204.  
  205. // Air Temperature Gradient
  206. for (int t = 0 ; t <= 128 ; t++) {
  207. r = (r + 1.6);
  208. b = (b - 1.6);
  209. tft.drawFastVLine(t,15, 15, ((long (r) / 8) << 11) | ((long (g) / 4) << 5) | (long (b) / 8));
  210. }
  211.  
  212. //Infrared Temperature Gradient
  213. r = 0; g = 0; b = 255;
  214. for (int t = 0 ; t <= 64 ; t++) {
  215. r = (r + 3.8);
  216. b = (b - 3.8);
  217. tft.drawFastVLine(t,82, 15, ((long (r) / 8) << 11) | ((long (g) / 4) << 5) | (long (b) / 8));
  218. }
  219. for (int t = 65 ; t <= 128 ; t++) {
  220. g = (g + 3.8);
  221. tft.drawFastVLine(t,82, 15, ((long (r) / 8) << 11) | ((long (g) / 4) << 5) | (long (b) / 8));
  222. }
  223.  
  224.  
  225. tft.drawRect(0, 0, 128, 65, ST7735_YELLOW);
  226. tft.drawRect(0, 66, 128, 65, ST7735_WHITE);
  227. tft.setTextColor(ST7735_WHITE);
  228. tft.setCursor(3, 72); tft.print("Infrared Temperature");
  229. StandardText();
  230. tft.setCursor(20, 5); tft.print("Air Temperature");
  231. tft.setCursor(5, 140); tft.print("Heat Index:");
  232. tft.setCursor(5, 150); tft.print("Dew Point");
  233. tft.setCursor(120, 140); tft.print("F");
  234. tft.setCursor(120, 150); tft.print("F");
  235.  
  236. digitalWrite(9, HIGH); // Laser On
  237.  
  238. while (changescreen == 0) {
  239. pressure = (bme.readPressure()/ 100.0F);
  240. float irc = tmp007.readObjTempC();
  241. Serial.println("irc");
  242. Serial.println(irc);
  243. float irf = (CtoF(irc));
  244. Serial.println("irf");
  245. Serial.println(irf);
  246. float tempc = bme.readTemperature();
  247. Serial.println("tempc");
  248. Serial.println(tempc);
  249.  
  250. float tempf = (CtoF(tempc));
  251. Serial.println("tempf");
  252. Serial.println(tempf);
  253. float hum = (bme.readHumidity());
  254. dewPoint(bme.readTemperature(), hum);
  255.  
  256. tft.setTextSize(2);
  257. tft.setCursor(54, 45); tft.print((int)tempf); //Temperature
  258. tft.setCursor(54, 113); tft.print((int)irf);tft.print(" "); //IR Temperature
  259. int airtriangle = mapfloat(tempf, 32, 100, 5, 123); // Air Indicator Check and Update
  260. int irtriangle = mapfloat(irf, 32, 200, 5, 123); // ir Indicator Check and Update
  261.  
  262. if (irtriangle != ircheck) {
  263. tft.fillRect(1, 100, 126, 10, ST7735_BLACK); // Clear ir Indicator
  264. tft.fillTriangle(irtriangle, 100, irtriangle-5, 109, irtriangle+5, 109, ST7735_YELLOW);
  265. ircheck = irtriangle;
  266. }
  267.  
  268. if (airtriangle != aircheck) {
  269. tft.fillRect(1, 31, 126, 10, ST7735_BLACK); // Clear Air Indicator
  270. tft.fillTriangle(airtriangle, 31, airtriangle-5, 40, airtriangle+5, 40, ST7735_YELLOW);
  271. aircheck = airtriangle;
  272. }
  273.  
  274. StandardText();
  275. tft.setCursor(84, 140); tft.print((0.5 * (tempf + 61.0 + ((tempf-68.0)*1.2) + (hum*0.094)))); //Heat Index
  276. tft.setCursor(84, 150); tft.print(CtoF(dew));
  277. }
  278. digitalWrite(9, LOW); // laser Off
  279. Navigate();
  280.  
  281. }
  282.  
  283. void humidity(){
  284. int xval[] = {95, 114, 86, 104, 76, 110, 91, 98, 94, 100, 101, 90, 83, 110, 116, 114, 81, 74, 90};
  285. int yval[] = {48, 53, 63, 33, 46, 62, 27, 60, 34, 68, 50, 43, 32, 26, 38, 44, 53, 63, 68};
  286. unsigned long dots;
  287. float abshum;
  288.  
  289. tft.fillScreen(ST7735_BLACK);
  290. tft.fillCircle(31, 67, 17, 0x4EDC); //relative humidity droplet
  291. tft.fillTriangle(15, 60, 47, 60, 31, 25, 0x4EDC); //relative humidity droplet
  292. tft.fillTriangle(31, 32, 31, 40, 38, 50, ST7735_WHITE); //Droplet upper 3D Mark
  293. // tft.fillTriangle(30, 82, 19, 75, 22, 73, ST7735_WHITE); //Droplet lower 3D Mark
  294.  
  295. tft.drawRect(0, 0, 64, 91, 0xBA80); // UI top left box
  296. tft.drawRect(65, 0, 63, 91, 0xBA80); // UI top right box
  297. tft.drawRect(0, 138, 128, 22, 0xBA80); // UI bottom PPM
  298. tft.drawRect(0, 115, 128, 22, 0xBA80); // UI bottom saturation
  299. tft.drawRect(0, 92, 128, 22, 0xBA80); // UI bottom vapor pressure
  300.  
  301. for (int i = 0; i <= 33; i++) { // AH bottom plane
  302. tft.drawLine(86 + i, 57, 71 + i, 72, 0xFD6C);
  303. }
  304. for (int i = 0; i <= 34; i++) { // AHleft-side plane
  305. tft.drawLine(86, 57-i, 71, 72-i, 0xFD6C);
  306. }
  307. tft.fillRect(86, 23, 35, 35, 0xFD6C);
  308. tft.drawRect(86, 23, 35, 35, 0xBA80); // AH top right box
  309. tft.drawLine(86, 57, 86, 23, 0xE71C);// AH lighter line verticle
  310. tft.drawLine(86, 57, 119, 57, 0xE71C);// AH lighter line horizontal
  311. tft.drawRect(71, 38, 35, 35, 0xBA80); //AH bottom left box
  312. tft.drawLine(86, 23, 71, 38, 0xBA80); // AH top left
  313. tft.drawLine(120, 23, 105, 38, 0xBA80); // AH top right
  314. tft.drawLine(86, 57, 72, 71, 0xE71C);// AH bottom left angle line
  315. tft.drawLine(120, 57,105, 72, 0xBA80);// AH bottom right
  316.  
  317. StandardText();
  318. tft.setCursor(7, 3); tft.print("Relative");
  319. tft.setCursor(7, 13); tft.print("Humidity");
  320. tft.setCursor(73, 3); tft.print("Absolute ");
  321. tft.setCursor(73, 13); tft.print("Humidity");
  322. tft.setCursor(4, 94); tft.print("Water Vapor Pressure");
  323. tft.setCursor(7, 117); tft.print("Saturation Pressure");
  324. tft.setCursor(4, 140); tft.print("Water Concentration");
  325. tft.setCursor(101, 78); tft.print("g/m3");
  326. tft.setCursor(80, 104); tft.print("hPa"); // vapor pressure hPa
  327. tft.setCursor(80, 127); tft.print("hPa"); // saturation pressure hPa
  328. tft.setCursor(80, 150); tft.print("PPM"); // Water PPM
  329.  
  330.  
  331. tft.setTextSize(2);
  332. tft.setTextColor(0x4EDC, ST7735_BLACK);
  333. tft.setCursor(50, 60); tft.print("%");
  334.  
  335.  
  336. while (changescreen == 0) {
  337. hum = bme.readHumidity();
  338.  
  339. temp = bme.readTemperature();
  340. pressure = (bme.readPressure()/ 100.0F);
  341. dewPoint(temp, hum);
  342. abshum = (2.16679 * ((VP*1000)/(temp+273.15)));
  343.  
  344. tft.setTextColor(ST7735_BLACK,0x4EDC);
  345. tft.setTextSize(2);
  346. tft.setCursor(21, 60); tft.print(int(bme.readHumidity()));
  347. tft.setTextSize(1);
  348. tft.setTextColor(0x4EDC, ST7735_BLACK);
  349. if (abshum > 9.99) {
  350. tft.setCursor(69, 78);tft.print(abshum); //Absolute Humidity
  351. }
  352. else {
  353. tft.setCursor(69, 78);tft.print(" "); //Absolute Humidity
  354. tft.setCursor(74, 78);tft.print(abshum); //Absolute Humidity
  355. }
  356.  
  357. for (int i = 0; i < 19; i++) {
  358. if ((abshum - 4) * 5 > i) dots = 0x631F; //blue
  359. if ((abshum - 4) * 5 < i) dots = 0xFD6C; // light orange
  360. tft.fillCircle(xval[i], yval[i], 2, dots); // dots
  361. }
  362. tft.setCursor(40, 127); tft.print((VP / (hum/100)*10)); // saturation pressure
  363. tft.setCursor(40, 150); tft.print(int(pow(10, 6)*((VP*10)/(pressure-(VP*10)))));
  364. if ((VP*10)> 9.99) {
  365. tft.setCursor(40, 104);tft.print(VP*10); //Water Vapor Pressure
  366. }
  367. else {
  368. tft.setCursor(40, 104);tft.print(" "); //Water Vapor Pressure
  369. tft.setCursor(45, 104);tft.print(VP*10); //Water Vapor Pressure
  370. }
  371. }
  372. Navigate();
  373. }
  374.  
  375. void altitude(){
  376. tft.fillScreen(ST7735_BLACK);
  377. tft.fillTriangle(34, 90, 94, 90, 64, 20, 0x79E0); //Large Mountain
  378. tft.fillTriangle(8, 90, 64, 90, 44, 30, 0x79E0); //Left Mountain
  379. tft.fillTriangle(54, 90, 120, 90, 90, 40, 0x79E0); //Right Mountain
  380.  
  381. StandardText();
  382. tft.setCursor(28, 102);
  383. tft.print("Air Pressure:");
  384. tft.setCursor(40, 5);
  385. tft.print("Altitude");
  386. tft.setCursor(100, 125);
  387. tft.print(" hPa");
  388. tft.setTextColor(ST7735_YELLOW,0x79E0);
  389. tft.setCursor(100, 73);
  390. tft.print("m");
  391.  
  392.  
  393.  
  394. tft.setTextSize(2);
  395.  
  396. while (changescreen == 0) {
  397. pressure = (bme.readPressure()/ 100.0F);
  398.  
  399. tft.setTextColor(ST7735_YELLOW,0x79E0); tft.setCursor(34, 73); tft.print(int(bme.readAltitude(SEALEVELPRESSURE_HPA)));
  400. tft.setTextColor(ST7735_YELLOW,ST7735_BLACK); tft.setCursor(26, 120); tft.print(pressure);
  401. }
  402.  
  403. Navigate();
  404. }
  405.  
  406. void wind(){
  407. tft.fillScreen(ST7735_BLACK);
  408. digitalWrite(7, HIGH);
  409.  
  410. StandardText();
  411. tft.setCursor(3, 10);
  412. tft.print("Raw Wind Reading");
  413. tft.setCursor(3, 50);
  414. tft.print("Compensation Adjust");
  415. tft.setCursor(3, 90);
  416. tft.print("Wind Chill");
  417. tft.setTextSize(2);
  418.  
  419. while (changescreen == 0) {
  420. float T = CtoF(bme.readTemperature());
  421. float V = 10;
  422. float raw = analogRead(A1);
  423. float out = analogRead(A6);
  424.  
  425. float Windchill = 35.74 + 0.6215*T - 35.75*pow(V,0.16) + 0.4275*T*pow(V,0.16);
  426. tft.setCursor(10, 25);
  427. tft.print(raw);
  428. tft.setCursor(10, 65);
  429. tft.print(out);
  430. tft.setCursor(10, 105);
  431. tft.print(Windchill);
  432.  
  433. }
  434. digitalWrite(7, LOW);
  435. Navigate();
  436. }
  437.  
  438. void light(){
  439. float r = 255;
  440. float b = 0;
  441. float g = 255;
  442. int uvcheck = 0;
  443. int uvTriangle = 0;
  444. int flare = 1;
  445. int prevflare = 1;
  446. unsigned long color;
  447. tft.fillScreen(ST7735_BLACK);
  448. StandardText();
  449. tft.setCursor(35, 5); tft.print("UV Intensity");
  450. tft.fillCircle(64, 75, 22, ST7735_YELLOW); //light
  451.  
  452.  
  453.  
  454.  
  455.  
  456.  
  457. tft.setTextColor(ST7735_BLACK,ST7735_YELLOW);
  458.  
  459.  
  460. for (int t = 0 ; t <= 128 ; t++) {
  461. g = (g - 1.6);
  462. tft.drawFastVLine(t,145, 15, ((long (r) / 8) << 11) | ((long (g) / 4) << 5) | (long (b) / 8));
  463. }
  464. tft.setTextColor(ST7735_BLACK);
  465. tft.setCursor(2, 149); tft.print("LOW");
  466. tft.setCursor(98, 151); tft.print("HIGH");
  467. tft.setTextSize(2);
  468. tft.setTextColor(ST7735_BLACK, ST7735_YELLOW);
  469. while (changescreen == 0) {
  470.  
  471. int uvLevel = averageAnalogRead(UVOUT);
  472. int refLevel = averageAnalogRead(REF_3V3);
  473.  
  474. //Use the 3.3V power pin as a reference to get a very accurate output value from sensor
  475. float outputVoltage = 3.3 / refLevel * uvLevel;
  476.  
  477. float uvIntensity = mapfloat(outputVoltage, 0.99, 2.8, 0.0, 15.0); //Convert the voltage to a UV intensity level
  478.  
  479.  
  480.  
  481. if (uvIntensity >= 0.2){
  482. tft.setCursor(46, 69); tft.print(uvIntensity,1);
  483. uvTriangle = mapfloat(uvIntensity, 0, 15, 5, 123); // UV Indicator Check and Update
  484. flare = mapfloat(uvIntensity, 0, 6, 10, 25); // UV Indicator Check and Update
  485. }
  486. else {
  487.  
  488. tft.setCursor(46,69); tft.print("0.0");
  489. uvTriangle = 5;
  490. flare = 10;
  491. }
  492.  
  493. // for (int y = 1 ; y <= 25 ; y++) {
  494. while (flare != prevflare){
  495. if (prevflare > flare) color = ST7735_BLACK;
  496. if (prevflare < flare) color = ST7735_YELLOW;
  497.  
  498. tft.drawLine(59, 48,64, 49-prevflare, color);// Top Flare left line
  499. tft.drawLine(69, 48,64, 49-prevflare, color);// Top Flare right line
  500. tft.drawLine(59, 103,64, 102+prevflare, color);// Bottom Flare Left Line
  501. tft.drawLine(69, 103,64, 102+prevflare, color);// Bottom Flare Right Line
  502. tft.drawLine(91, 70,90+prevflare, 75, color);// Right Flare
  503. tft.drawLine(91, 80,90+prevflare, 75, color);// Right Flare
  504. tft.drawLine(37, 70,38-prevflare, 75, color);// Left Flare
  505. tft.drawLine(37, 80,38-prevflare, 75, color);// Left Flare
  506. tft.drawLine(78, 97,82+(prevflare/1.4), 94+(prevflare/1.4), color);// Bottom-Right Flare Left Line
  507. tft.drawLine(86, 91,82+(prevflare/1.4), 94+(prevflare/1.4), color);// Bottom-Right Flare Right Line
  508. tft.drawLine(42, 90,46-(prevflare/1.4), 94+(prevflare/1.4), color);// Bottom-Left Flare Left Line
  509. tft.drawLine(50, 97,46-(prevflare/1.4), 94+(prevflare/1.4), color);// Bottom-LEFT Flare Right Line
  510. tft.drawLine(42, 60,46-(prevflare/1.4), 57-(prevflare/1.4), color);// Top-Left Flare Left Line
  511. tft.drawLine(50, 54,46-(prevflare/1.4), 57-(prevflare/1.4), color);// Top-LEFT Flare Right Line
  512. tft.drawLine(78, 54,82+(prevflare/1.4), 57-(prevflare/1.4), color);// Top-Right Flare Left Line
  513. tft.drawLine(86, 60,82+(prevflare/1.4), 57-(prevflare/1.4), color);// Top-Right Flare Right Line
  514. if (prevflare > flare) {
  515. prevflare = prevflare - 1;
  516. }
  517. if (prevflare < flare) {
  518. prevflare = prevflare + 1;
  519. }
  520. }
  521.  
  522. if (uvTriangle != uvcheck) {
  523.  
  524. tft.fillRect(0, 135, 126, 10, ST7735_BLACK); // UV Indicator
  525. tft.fillTriangle(uvcheck, 144, uvcheck - 5, 135, uvcheck + 5, 135, ST7735_BLACK);
  526. tft.fillTriangle(uvTriangle, 144, uvTriangle - 5, 135, uvTriangle + 5, 135, ST7735_YELLOW);
  527. uvcheck = uvTriangle;
  528. }
  529. }
  530. Navigate();
  531. }
  532.  
  533. void buttonrequest(){
  534. Wire.beginTransmission(0x1B); // transmit to device
  535. Wire.write(0x02); // want to read detection status // set pointer
  536. Wire.endTransmission(); // stop transmitting
  537. Wire.requestFrom(0x1B, 1); // request 1 byte from slave device
  538. readstatus =Wire.read();
  539. Wire.beginTransmission(0x1B); // transmit to device
  540. Wire.write(0x03); // want to read key status // set pointer
  541. Wire.endTransmission(); // stop transmitting
  542. Wire.requestFrom(0x1B, 1); // request 1 byte from slave device
  543. ButtonSelection =Wire.read();
  544. delay(5);
  545. }
  546. void buttonread(){
  547.  
  548. sei();
  549. buttonrequest();
  550. // to clear change int we must read both status bytes 02 and 03
  551. if (sleepstatus == 0) {
  552. CLKPR = 0x80;
  553. CLKPR = 0x00;
  554. analogWrite(5, brightness); // Screen Backlight
  555. ButtonSelection = 0;
  556. sleepstatus = 1;
  557. }
  558.  
  559. if (ButtonSelection == 16 || ButtonSelection == 2 || ButtonSelection == 4) {
  560. changescreen = 1;
  561. }
  562.  
  563. if (ButtonSelection == 4) {
  564. unsigned long selectpressed, check;
  565. selectpressed = millis();
  566. while (readstatus != 0){
  567. buttonrequest();
  568. check = millis();
  569. if ((check - selectpressed) >= 3000){
  570. analogWrite(5, 0); // Screen Backlight
  571. delay(2000);
  572. CLKPR = 0x80;
  573. CLKPR = 0x04;
  574. ButtonSelection = 0;
  575. sleepstatus = 0;
  576. changescreen = 0;
  577. }
  578. else {
  579. ButtonSelection = 4;
  580. }
  581. }
  582. }
  583. Serial.println(ButtonSelection);
  584. Serial.println(readstatus);
  585. }
  586.  
  587.  
  588.  
  589. double dewPoint(double celsius, double humidity)
  590. {
  591.  
  592. double RATIO = 373.15 / (273.15 + celsius);
  593. double RHS = -7.90298 * (RATIO - 1);
  594. RHS += 5.02808 * log10(RATIO);
  595. RHS += -1.3816e-7 * (pow(10, (11.344 * (1 - 1/RATIO ))) - 1) ;
  596. RHS += 8.1328e-3 * (pow(10, (-3.49149 * (RATIO - 1))) - 1) ;
  597. RHS += log10(pressure);
  598.  
  599. // factor -3 is to adjust units - Vapor Pressure SVP * humidity
  600. VP = pow(10, RHS - 3) * humidity;
  601.  
  602. // (2) DEWPOINT = F(Vapor Pressure)
  603. double T = log(VP/0.61078); // temp var
  604. dew = (241.88 * T) / (17.558 - T);
  605.  
  606. }
  607.  
  608. float CtoF(float celcius)
  609. {
  610. return (celcius * 18 + 5)/10 + 32;
  611. }
  612.  
  613. //Takes an average of readings on a given pin
  614. //Returns the average
  615. int averageAnalogRead(int pinToRead)
  616. {
  617. byte numberOfReadings = 50;
  618. unsigned int runningValue = 0;
  619.  
  620. for(int x = 0 ; x < numberOfReadings ; x++)
  621. runningValue += analogRead(pinToRead);
  622. runningValue /= numberOfReadings;
  623.  
  624. return(runningValue);
  625. }
  626.  
  627. //The Arduino Map function but for floats
  628. //From: http://forum.arduino.cc/index.php?topic=3922.0
  629. float mapfloat(float x, float in_min, float in_max, float out_min, float out_max)
  630. {
  631. return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
  632. }
  633.  
  634. void StandardText() {
  635. tft.setTextColor(ST7735_YELLOW,ST7735_BLACK);
  636. tft.setTextSize(1);
  637. }
  638.  
  639. void Navigate(){
  640. if (ButtonSelection == 16){
  641. page = (page + 1);
  642. if (page > 6) page = 1;
  643. if (page < 1) page = 6;
  644. ButtonSelection = 0;
  645. }
  646. if (ButtonSelection == 2){
  647. page = (page - 1);
  648. if (page > 6) page = 1;
  649. if (page < 1) page = 6;
  650. ButtonSelection = 0;
  651. }
  652. if (ButtonSelection == 4){
  653. selection = page - 2;
  654. page = 1;
  655. ButtonSelection = 0;
  656.  
  657. }
  658. changescreen = 0;
  659.  
  660. }
  661.  
  662. void CalculateColor(long r, long g, long b){
  663. color = (r / 8) << 11 | (g / 4) << 5 | (b / 8);
  664. }
  665.  
  666. void drawButton(long r, long g, long b, long rchange, int gchange, int bchange, int x, int y) {
  667. for (int t=1; t<=10; t++){
  668. CalculateColor(((rchange/10)*t)+r, ((gchange/10)*t)+g, ((bchange/10)*t)+b);
  669. tft.drawRect(x+t, y+t, 120-(t*2), 22-(t*2), color);
  670. }
  671. }
  672.  
  673. void brightnessupdate(){
  674. int brightnessbar = int(mapfloat(brightness, 24, 250, 0, 118));
  675. tft.fillRect(5, 34, brightnessbar, 15, ST7735_YELLOW);
  676. tft.fillRect(brightnessbar + 6, 34, 118-brightnessbar, 7, ST7735_BLACK);
  677. tft.fillRect(brightnessbar + 6, 43, 118-brightnessbar, 7, ST7735_BLACK);
  678. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement