Advertisement
LaughingMan

Arduino: Hartclock

Dec 19th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 12.27 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <TFT_HX8357.h> // Display
  3. #include <DHT.h> // DHT22 Temp/Humidity Sensor
  4. #include "SparkFunMPL3115A2.h"
  5. #include <DHT_U.h>
  6. #include <DS3231.h> // Real-Time Clock
  7. #include <Mx2125.h> // Mx2125 2-Axis Accelerometer
  8. #include <L3G.h> // L3GD20004 3-Axis Gyro
  9.  
  10. #define BAUDRATE 250000
  11. #define GPSBAUD 9600
  12. #define ESPBAUD 9600
  13.  
  14. #define DRAWINTERVAL 1000
  15. #define READTEMPINTERVAL 3000
  16.  
  17. #define TEMPAPIN 10
  18. #define TEMPATYPE DHT22
  19.  
  20. #define TEMPBPIN SDA
  21. #define TEMPBTYPE _DS3231
  22.  
  23. #define TEMPCPIN SDA
  24. #define TEMPCTYPE _MPL3115A2
  25.  
  26. #define TEMPDPIN A0
  27. #define TEMPDTYPE _MX2125
  28.  
  29. #define ACCELXPIN 4
  30. #define ACCELYPIN 5
  31.  
  32. #define GYROINTA 2
  33. #define GYROINTB 3
  34.  
  35. Time GetCompileTime()
  36. {
  37.   char s_month[5];
  38.   int month, day, year;
  39.   Time t;
  40.   static const char month_names[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
  41.  
  42.   sscanf(__DATE__, "%s %d %d", s_month, &day, &year);
  43.  
  44.   month = ((strstr(month_names, s_month) - month_names) / 3) + 1;
  45.  
  46.   t.mon = month;
  47.   t.date = day;
  48.   t.year = year;
  49.  
  50.   t.hour = conv2d(__TIME__);
  51.   t.min = conv2d(__TIME__ + 3);
  52.   t.sec = conv2d(__TIME__ + 6);
  53.  
  54.   return t;
  55. }
  56.  
  57. float accelX,
  58.       accelY,
  59.       tiltX,
  60.       tiltY,
  61.       rotation,
  62.       gyroX,
  63.       gyroY,
  64.       gyroZ,
  65.       humidity,
  66.       heatIndex,
  67.       tempAC,
  68.       tempAF,
  69.       tempBC,
  70.       tempBF,
  71.       tempCC,
  72.       tempCF,
  73.       tempDC,
  74.       tempDF,
  75.       altitudeM,
  76.       altitudeF,
  77.       pressureP;
  78.  
  79. Time compileTime = GetCompileTime(),
  80.      currentTime,
  81.      lastTempReadTime,
  82.      lastDrawTime;
  83.  
  84. char* gpsData,
  85.       espData,
  86.       accelXStr,
  87.       accelYStr,
  88.       tiltXStr,
  89.       tiltYStr,
  90.       rotationStr,
  91.       gyroXStr,
  92.       gyroYStr,
  93.       gyroZStr,
  94.       humidityStr,
  95.       heatIndexStr,
  96.       tempACStr,
  97.       tempAFStr,
  98.       tempBCStr,
  99.       tempBFStr,
  100.       tempCCStr,
  101.       tempCFStr,
  102.       tempDCStr,
  103.       tempDFStr,
  104.       altitudeMStr,
  105.       altitudeFStr,
  106.       pressurePStr;
  107.  
  108. uint8_t HB = 0 ; // Heartbeat counter
  109.  
  110. TFT_HX8357 tft = TFT_HX8357();
  111. DHT dht(TEMPAPIN, TEMPATYPE);
  112. MPL3115A2 alti;
  113. DS3231  rtc(SDA, SCL);
  114. Mx2125  Accel(ACCELXPIN, ACCELYPIN, TEMPCPIN);
  115. L3G gyro;
  116.  
  117. HardwareSerial & GPS = Serial1;
  118. HardwareSerial & ESP = Serial2;
  119.  
  120. void setup() {
  121.   Initialize();
  122. }
  123.  
  124. void Initialize() {
  125.   SetTimers();
  126.  
  127.   SetInitialPinStates();
  128.  
  129.   AttachInterrupts();
  130.  
  131.   InitializeRTC();
  132.   InitializeTFT();
  133.   InitializeDHT();
  134.  
  135.   InitializeSerial();
  136.  
  137.   InitializeWire();
  138.  
  139.   DrawInterface();
  140. }
  141.  
  142. void SetTimers()
  143. {
  144.   cli(); // Stop Interrupts
  145.  
  146.   SetTimer3();
  147.   SetTimer4();
  148.   SetTimer5();
  149.  
  150.   sei(); // Allow Interrupts
  151. }
  152.  
  153. void SetTimer3() // One Second Timer
  154. {
  155.   TCCR3A = 0;// set entire TCCR4A register to 0
  156.   TCCR3B = 0;// same for TCCR4B
  157.   TCNT3  = 0;//initialize counter value to 0
  158.   // set compare match register for 1hz increments
  159.   OCR3A = 15624;// = (16*10^6) / (1*1024) - 1 (must be <65536)
  160.   // turn on CTC mode
  161.   TCCR3B |= (1 << WGM32);
  162.   // Set CS30 and CS32 bits for 1024 prescaler
  163.   TCCR3B |= (1 << CS32) | (1 << CS30);
  164.   // enable timer compare interrupt
  165.   TIMSK3 |= (1 << OCIE3A);
  166. }
  167.  
  168. void SetTimer4() // Three Second Timer
  169. {
  170.   TCCR4A = 0;// set entire TCCR4A register to 0
  171.   TCCR4B = 0;// same for TCCR4B
  172.   TCNT4  = 0;//initialize counter value to 0
  173.   // set compare match register for 1hz increments
  174.   OCR4A = 47348;// = (16*10^6) / (1*1024) - 1 (must be <65536)
  175.   // turn on CTC mode
  176.   TCCR4B |= (1 << WGM42);
  177.   // Set CS40 and CS42 bits for 1024 prescaler
  178.   TCCR4B |= (1 << CS42) | (1 << CS40);
  179.   // enable timer compare interrupt
  180.   TIMSK4 |= (1 << OCIE4A);
  181. }
  182.  
  183. void SetTimer5() // One Second Timer
  184. {
  185.   TCCR5A = 0;// set entire TCCR5A register to 0
  186.   TCCR5B = 0;// same for TCCR5B
  187.   TCNT5  = 0;//initialize counter value to 0
  188.   // set compare match register for 1hz increments
  189.   OCR5A = 15624;// = (16*10^6) / (1*1024) - 1 (must be <65536)
  190.   // turn on CTC mode
  191.   TCCR5B |= (1 << WGM52);
  192.   // Set CS50 and CS52 bits for 1024 prescaler
  193.   TCCR5B |= (1 << CS52) | (1 << CS50);
  194.   // enable timer compare interrupt
  195.   TIMSK5 |= (1 << OCIE5A);
  196. }
  197.  
  198. void SetInitialPinStates()
  199. {
  200.   pinMode(TEMPAPIN, INPUT);
  201.   pinMode(TEMPBPIN, INPUT);
  202.   pinMode(TEMPCPIN, INPUT);
  203.  
  204.   pinMode(ACCELXPIN, INPUT);
  205.   pinMode(ACCELYPIN, INPUT);
  206. }
  207.  
  208. void AttachInterrupts()
  209. {
  210.  
  211. }
  212.  
  213. void InitializeRTC()
  214. {
  215.   rtc.begin();
  216.   rtc.setDate(compileTime, true);
  217.   rtc.setDOW();
  218.  
  219.   ReadClock();
  220.  
  221.   lastTempReadTime = currentTime;
  222.   lastDrawTime = currentTime;
  223. }
  224.  
  225. void InitializeTFT()
  226. {
  227.   tft.begin();
  228. }
  229.  
  230. void InitializeDHT()
  231. {
  232.   dht.begin();
  233. }
  234.  
  235. void InitializeSerial()
  236. {
  237.   Serial.begin(BAUDRATE);
  238.  
  239.   InitializeGPS();
  240.   InitializeESP();
  241. }
  242.  
  243. void InitializeWire()
  244. {
  245.   Wire.begin();
  246.  
  247.   InitializeGyro();
  248.   InitializeAltimeter();
  249. }
  250.  
  251. void DrawInterface()
  252. {
  253.   tft.setRotation(1);
  254.  
  255.   tft.fillScreen(TFT_NAVY);
  256.  
  257.   tft.setTextColor(TFT_WHITE);
  258.   tft.setTextSize(2);
  259.  
  260.   tft.setCursor(20, 10);
  261.   tft.println("Temp");
  262.  
  263.   tft.setCursor(10, 35);
  264.   tft.println("A");
  265.  
  266.   tft.setCursor(10, 65);
  267.   tft.println("B");
  268.  
  269.   tft.setCursor(10, 95);
  270.   tft.println("C");
  271.  
  272.   tft.setCursor(10, 125);
  273.   tft.println("D");
  274.  
  275.   tft.setCursor(20, 155);
  276.   tft.println("Humidity");
  277.  
  278.   //tft.setCursor(10, 100);
  279.   //tft.println("Acceleration");
  280.  
  281.   //tft.setCursor(10, 160);
  282.   //tft.println("X");
  283.  
  284.   //tft.setCursor(50, 160);
  285.   //tft.println("Y");
  286.  
  287.   //tft.setCursor(130, 100);
  288.   //tft.println("Tilt");
  289.  
  290.   //tft.setCursor(130, 160);
  291.   //tft.println("X");
  292.  
  293.   //tft.setCursor(170, 160);
  294.   //tft.println("Y");
  295.  
  296.   //tft.setCursor(70, 160);
  297.   //tft.println("Rotation");
  298. }
  299.  
  300. void ReadClock()
  301. {
  302.   currentTime = rtc.getTime();
  303. }
  304.  
  305. void InitializeGPS()
  306. {
  307.   GPS.begin(GPSBAUD);
  308. }
  309.  
  310. void InitializeESP()
  311. {
  312.   ESP.begin(ESPBAUD);
  313. }
  314.  
  315. void InitializeGyro()
  316. {
  317.   if (!gyro.init())
  318.   {
  319.     char *message = "Failed to auto-detect Gyro type!";
  320.     Error(message);
  321.   }
  322.  
  323.   gyro.enableDefault();
  324. }
  325.  
  326. void InitializeAltimeter()
  327. {
  328.   alti.begin();
  329.  
  330.   alti.setOversampleRate(7);
  331.   alti.enableEventFlags();
  332.  
  333.   alti.setModeActive();
  334. }
  335.  
  336. ISR(TIMER3_COMPA_vect)
  337. {
  338.   //Serial.println("Timer 3");
  339.  
  340.   SerialOutput();
  341. }
  342.  
  343. ISR(TIMER4_COMPA_vect)
  344. {
  345.   //Serial.println("Timer 4");
  346.  
  347.   ReadTemp();
  348.  
  349.   ReadAltimeter();
  350. }
  351.  
  352. ISR(TIMER5_COMPA_vect)
  353. {
  354.   ReadClock();
  355.   //Serial.println("Timer 5");
  356.  
  357.   Draw();
  358. }
  359.  
  360. void loop() {
  361.   if (ESP.available())
  362.   {
  363.     //ReadESP();
  364.   }
  365.  
  366.   if (GPS.available())
  367.   {
  368.     //ReadGPS();
  369.   }
  370.  
  371.   ReadAccelerometer();
  372.  
  373.   ReadGyro();
  374. }
  375.  
  376. void ReadESP()
  377. {
  378.   //espData = "";
  379.  
  380.   while (ESP.available())
  381.   {
  382.     char inByte = ESP.read();
  383.     //espData += inByte;
  384.     Serial.print(inByte);
  385.   }
  386.  
  387.   //Serial.println(espData);
  388. }
  389.  
  390. void ReadGPS()
  391. {
  392.   //gpsData = "";
  393.  
  394.   while (GPS.available())
  395.   {
  396.     char inByte = GPS.read();
  397.     //strcat(gpsData, inByte);
  398.     //gpsData += inByte;
  399.     Serial.print(inByte);
  400.   }
  401.  
  402.   //Serial.println(gpsData);
  403. }
  404.  
  405. void ReadAltimeter()
  406. {
  407.   alti.setModeBarometer();
  408.   pressureP = alti.readPressure();
  409.  
  410.   alti.setModeAltimeter();
  411.   altitudeM = alti.readAltitude();
  412. }
  413.  
  414. void ReadAccelerometer()
  415. {
  416.   accelX = Accel.mx_acceleration_x();
  417.   accelY = Accel.mx_acceleration_y();
  418.  
  419.   tiltX = Accel.mx_tilt_x();
  420.   tiltY = Accel.mx_tilt_y();
  421.  
  422.   rotation = Accel.mx_rotation();
  423. }
  424.  
  425. void ReadGyro()
  426. {
  427.   gyro.read();
  428.  
  429.   gyroX = gyro.g.x;
  430.   gyroY = gyro.g.y;
  431.   gyroZ = gyro.g.z;
  432. }
  433.  
  434. void ReadTemp()
  435. {
  436.   humidity = dht.readHumidity();
  437.  
  438.   heatIndex = dht.computeHeatIndex();
  439.  
  440.   tempAC = dht.readTemperature();
  441.   tempBC = rtc.getTemp();
  442.   tempCC = alti.readTemp();
  443.   tempDC = Accel.mx_temperature();
  444.  
  445.   tempAF = convertCtoF(tempAC);
  446.   tempBF = convertCtoF(tempBC);
  447.   tempCF = convertCtoF(tempCC);
  448.   tempDF = convertCtoF(tempDC);
  449. }
  450.  
  451. void Draw()
  452. {
  453.   DrawDate();
  454.   DrawTime();
  455.   DrawTemp();
  456.   DrawAccelerometer();
  457.   DrawGyro();
  458. }
  459.  
  460. void SerialOutput()
  461. {
  462.   Serial.println();
  463.  
  464.   Serial.print(__DATE__);
  465.   Serial.print(" ");
  466.   Serial.print(__TIME__);
  467.   Serial.print(": ");
  468.   Serial.println(__FILE__);
  469.  
  470.   Serial.print("Compiled: ");
  471.   Serial.print(compileTime.mon);
  472.   Serial.print("/");
  473.   Serial.print(compileTime.date);
  474.   Serial.print("/");
  475.   Serial.print(compileTime.year);
  476.   Serial.print(" ");
  477.   Serial.print(compileTime.hour);
  478.   Serial.print(":");
  479.   Serial.print(compileTime.min);
  480.   Serial.print(":");
  481.   Serial.println(compileTime.sec);
  482.  
  483.   Serial.print("Current: ");
  484.   Serial.print(currentTime.mon);
  485.   Serial.print("/");
  486.   Serial.print(currentTime.date);
  487.   Serial.print("/");
  488.   Serial.print(currentTime.year);
  489.   Serial.print(" ");
  490.   Serial.print(currentTime.hour);
  491.   Serial.print(":");
  492.   Serial.print(currentTime.min);
  493.   Serial.print(":");
  494.   Serial.println(currentTime.sec);
  495.  
  496.   Serial.print("Temp A: ");
  497.   Serial.print(tempAC);
  498.   Serial.print(" (");
  499.   Serial.print(tempAF);
  500.   Serial.print("F) | Temp B: ");
  501.   Serial.print(tempBC);
  502.   Serial.print(" (");
  503.   Serial.print(tempBF);
  504.   Serial.print("F) | Temp C: ");
  505.   Serial.print(tempCC);
  506.   Serial.print(" (");
  507.   Serial.print(tempCF);
  508.   Serial.print("F) | Temp D: ");
  509.   Serial.print(tempDC);
  510.   Serial.print(" (");
  511.   Serial.print(tempDF);
  512.   Serial.println("F)");
  513.  
  514.   Serial.print("Humidity: ");
  515.   Serial.print(humidity);
  516.   Serial.print("% | Heat Index: ");
  517.   Serial.println(heatIndex);
  518.  
  519.   Serial.print("Pressure: ");
  520.   Serial.print(pressureP);
  521.   Serial.print(" pascals | Altitude: ");
  522.   Serial.print(altitudeM);
  523.   Serial.println(" meters");
  524.  
  525. //   Serial.print("Accel: ");
  526. //   Serial.print(accelX);
  527. //   Serial.print("X " );
  528. //   Serial.print(accelY);
  529. //   Serial.print("Y | Tilt: ");
  530. //   Serial.print(tiltX);
  531. //   Serial.print("X ");
  532. //   Serial.print(tiltY);
  533. //   Serial.print("Y | Rotation: ");
  534. //   Serial.println(rotation);
  535.  
  536. //   Serial.print("Gyro: X");
  537. //   Serial.print(gyroX);
  538. //   Serial.print(" Y");
  539. //   Serial.print(gyroY);
  540. //   Serial.print(" Z");
  541. //   Serial.println(gyroZ);
  542.  
  543.   //Serial.println(gpsData);
  544.   //Serial.println(espData);
  545. }
  546.  
  547. void DrawTemp()
  548. {
  549.   tft.setCursor(30, 35);
  550.   tft.println(tempAF);
  551.   //tft.drawCentreString(&tempAFStr, 30, 35, 4);
  552.  
  553.   tft.setCursor(30, 65);
  554.   tft.println(tempBF);
  555.   //tft.drawCentreString(&tempBFStr, 30, 65, 4);
  556.  
  557.   tft.setCursor(30, 95);
  558.   tft.println(tempCF);
  559.   //tft.drawCentreString(&tempCFStr, 30, 95, 4);
  560.  
  561.   tft.setCursor(30, 125);
  562.   tft.println(tempDF);
  563.   //tft.drawCentreString(&tempCFStr, 30, 95, 4);
  564.  
  565.   tft.setCursor(25, 180);
  566.   tft.println(humidity);
  567.   tft.println("%");
  568.   //tft.drawCentreString(&humidityStr, 25, 180, 4);
  569. }
  570.  
  571. void DrawDate()
  572. {
  573.   tft.setCursor(250, 20);
  574.   tft.println(rtc.getDateStr());
  575.   //tft.drawCentreString(rtc.getDateStr(), 250, 20, 4);
  576. }
  577.  
  578. void DrawDate()
  579. {
  580.   tft.setCursor(250, 50);
  581.   tft.println(rtc.getTimeStr());
  582.   //tft.drawCentreString(rtc.getTimeStr(), 250, 60, 4);
  583. }
  584.  
  585. void DrawAccelerometer()
  586. {
  587.  
  588. }
  589.  
  590. void DrawGyro()
  591. {
  592.  
  593. }
  594.  
  595. void DrawAltimeter()
  596. {
  597.  
  598. }
  599.  
  600. /* *** HELPER METHODS *** */
  601. void Error(char* message)
  602. {
  603.   tft.fillScreen(TFT_RED);
  604.   tft.setTextColor(TFT_YELLOW);
  605.   tft.setTextSize(5);
  606.   tft.setCursor(160, 240);
  607.   tft.drawCentreString(message, 160, 240, 5);
  608.   Serial.println(message);
  609.   while (1);
  610. }
  611.  
  612. int convMonth(char* m)
  613. {
  614.   if     ( m == "Jan" ) return 1;
  615.   else if ( m == "Feb" ) return 2;
  616.   else if ( m == "Mar" ) return 3;
  617.   else if ( m == "Apr" ) return 4;
  618.   else if ( m == "May" ) return 5;
  619.   else if ( m == "Jun" ) return 6;
  620.   else if ( m == "Jul" ) return 7;
  621.   else if ( m == "Aug" ) return 8;
  622.   else if ( m == "Sep" ) return 9;
  623.   else if ( m == "Oct" ) return 10;
  624.   else if ( m == "Nov" ) return 11;
  625.   else if ( m == "Dec" ) return 12;
  626.   else
  627.   {
  628.     char *message = "Failed to parse month string to int!";
  629.     Error(message);
  630.     exit(-1);
  631.   }
  632. }
  633.  
  634. uint8_t conv2d(const char* p)
  635. {
  636.   uint8_t v = 0;
  637.   if ('0' <= *p && *p <= '9')
  638.     v = *p - '0';
  639.   return 10 * v + *++p - '0';
  640. }
  641.  
  642. bool TimeTrigger(const Time &lastTime)
  643. {
  644.   return lastTime.year < currentTime.year ||
  645.          lastTime.mon < currentTime.mon ||
  646.          lastTime.date < currentTime.date ||
  647.          lastTime.hour < currentTime.hour ||
  648.          lastTime.min < currentTime.min ||
  649.          lastTime.sec < currentTime.sec;
  650. }
  651.  
  652. float convertCtoF(const float & c)
  653. {
  654.   return dht.convertCtoF(c);
  655. }
  656.  
  657. float convertFtoC(const float & f)
  658. {
  659.   return dht.convertFtoC(f);
  660. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement