Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- #include <TFT_HX8357.h> // Display
- #include <DHT.h> // DHT22 Temp/Humidity Sensor
- #include "SparkFunMPL3115A2.h"
- #include <DHT_U.h>
- #include <DS3231.h> // Real-Time Clock
- #include <Mx2125.h> // Mx2125 2-Axis Accelerometer
- #include <L3G.h> // L3GD20004 3-Axis Gyro
- #define BAUDRATE 250000
- #define GPSBAUD 9600
- #define ESPBAUD 9600
- #define DRAWINTERVAL 1000
- #define READTEMPINTERVAL 3000
- #define TEMPAPIN 10
- #define TEMPATYPE DHT22
- #define TEMPBPIN SDA
- #define TEMPBTYPE _DS3231
- #define TEMPCPIN SDA
- #define TEMPCTYPE _MPL3115A2
- #define TEMPDPIN A0
- #define TEMPDTYPE _MX2125
- #define ACCELXPIN 4
- #define ACCELYPIN 5
- #define GYROINTA 2
- #define GYROINTB 3
- Time GetCompileTime()
- {
- char s_month[5];
- int month, day, year;
- Time t;
- static const char month_names[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
- sscanf(__DATE__, "%s %d %d", s_month, &day, &year);
- month = ((strstr(month_names, s_month) - month_names) / 3) + 1;
- t.mon = month;
- t.date = day;
- t.year = year;
- t.hour = conv2d(__TIME__);
- t.min = conv2d(__TIME__ + 3);
- t.sec = conv2d(__TIME__ + 6);
- return t;
- }
- float accelX,
- accelY,
- tiltX,
- tiltY,
- rotation,
- gyroX,
- gyroY,
- gyroZ,
- humidity,
- heatIndex,
- tempAC,
- tempAF,
- tempBC,
- tempBF,
- tempCC,
- tempCF,
- tempDC,
- tempDF,
- altitudeM,
- altitudeF,
- pressureP;
- Time compileTime = GetCompileTime(),
- currentTime,
- lastTempReadTime,
- lastDrawTime;
- char* gpsData,
- espData,
- accelXStr,
- accelYStr,
- tiltXStr,
- tiltYStr,
- rotationStr,
- gyroXStr,
- gyroYStr,
- gyroZStr,
- humidityStr,
- heatIndexStr,
- tempACStr,
- tempAFStr,
- tempBCStr,
- tempBFStr,
- tempCCStr,
- tempCFStr,
- tempDCStr,
- tempDFStr,
- altitudeMStr,
- altitudeFStr,
- pressurePStr;
- uint8_t HB = 0 ; // Heartbeat counter
- TFT_HX8357 tft = TFT_HX8357();
- DHT dht(TEMPAPIN, TEMPATYPE);
- MPL3115A2 alti;
- DS3231 rtc(SDA, SCL);
- Mx2125 Accel(ACCELXPIN, ACCELYPIN, TEMPCPIN);
- L3G gyro;
- HardwareSerial & GPS = Serial1;
- HardwareSerial & ESP = Serial2;
- void setup() {
- Initialize();
- }
- void Initialize() {
- SetTimers();
- SetInitialPinStates();
- AttachInterrupts();
- InitializeRTC();
- InitializeTFT();
- InitializeDHT();
- InitializeSerial();
- InitializeWire();
- DrawInterface();
- }
- void SetTimers()
- {
- cli(); // Stop Interrupts
- SetTimer3();
- SetTimer4();
- SetTimer5();
- sei(); // Allow Interrupts
- }
- void SetTimer3() // One Second Timer
- {
- TCCR3A = 0;// set entire TCCR4A register to 0
- TCCR3B = 0;// same for TCCR4B
- TCNT3 = 0;//initialize counter value to 0
- // set compare match register for 1hz increments
- OCR3A = 15624;// = (16*10^6) / (1*1024) - 1 (must be <65536)
- // turn on CTC mode
- TCCR3B |= (1 << WGM32);
- // Set CS30 and CS32 bits for 1024 prescaler
- TCCR3B |= (1 << CS32) | (1 << CS30);
- // enable timer compare interrupt
- TIMSK3 |= (1 << OCIE3A);
- }
- void SetTimer4() // Three Second Timer
- {
- TCCR4A = 0;// set entire TCCR4A register to 0
- TCCR4B = 0;// same for TCCR4B
- TCNT4 = 0;//initialize counter value to 0
- // set compare match register for 1hz increments
- OCR4A = 47348;// = (16*10^6) / (1*1024) - 1 (must be <65536)
- // turn on CTC mode
- TCCR4B |= (1 << WGM42);
- // Set CS40 and CS42 bits for 1024 prescaler
- TCCR4B |= (1 << CS42) | (1 << CS40);
- // enable timer compare interrupt
- TIMSK4 |= (1 << OCIE4A);
- }
- void SetTimer5() // One Second Timer
- {
- TCCR5A = 0;// set entire TCCR5A register to 0
- TCCR5B = 0;// same for TCCR5B
- TCNT5 = 0;//initialize counter value to 0
- // set compare match register for 1hz increments
- OCR5A = 15624;// = (16*10^6) / (1*1024) - 1 (must be <65536)
- // turn on CTC mode
- TCCR5B |= (1 << WGM52);
- // Set CS50 and CS52 bits for 1024 prescaler
- TCCR5B |= (1 << CS52) | (1 << CS50);
- // enable timer compare interrupt
- TIMSK5 |= (1 << OCIE5A);
- }
- void SetInitialPinStates()
- {
- pinMode(TEMPAPIN, INPUT);
- pinMode(TEMPBPIN, INPUT);
- pinMode(TEMPCPIN, INPUT);
- pinMode(ACCELXPIN, INPUT);
- pinMode(ACCELYPIN, INPUT);
- }
- void AttachInterrupts()
- {
- }
- void InitializeRTC()
- {
- rtc.begin();
- rtc.setDate(compileTime, true);
- rtc.setDOW();
- ReadClock();
- lastTempReadTime = currentTime;
- lastDrawTime = currentTime;
- }
- void InitializeTFT()
- {
- tft.begin();
- }
- void InitializeDHT()
- {
- dht.begin();
- }
- void InitializeSerial()
- {
- Serial.begin(BAUDRATE);
- InitializeGPS();
- InitializeESP();
- }
- void InitializeWire()
- {
- Wire.begin();
- InitializeGyro();
- InitializeAltimeter();
- }
- void DrawInterface()
- {
- tft.setRotation(1);
- tft.fillScreen(TFT_NAVY);
- tft.setTextColor(TFT_WHITE);
- tft.setTextSize(2);
- tft.setCursor(20, 10);
- tft.println("Temp");
- tft.setCursor(10, 35);
- tft.println("A");
- tft.setCursor(10, 65);
- tft.println("B");
- tft.setCursor(10, 95);
- tft.println("C");
- tft.setCursor(10, 125);
- tft.println("D");
- tft.setCursor(20, 155);
- tft.println("Humidity");
- //tft.setCursor(10, 100);
- //tft.println("Acceleration");
- //tft.setCursor(10, 160);
- //tft.println("X");
- //tft.setCursor(50, 160);
- //tft.println("Y");
- //tft.setCursor(130, 100);
- //tft.println("Tilt");
- //tft.setCursor(130, 160);
- //tft.println("X");
- //tft.setCursor(170, 160);
- //tft.println("Y");
- //tft.setCursor(70, 160);
- //tft.println("Rotation");
- }
- void ReadClock()
- {
- currentTime = rtc.getTime();
- }
- void InitializeGPS()
- {
- GPS.begin(GPSBAUD);
- }
- void InitializeESP()
- {
- ESP.begin(ESPBAUD);
- }
- void InitializeGyro()
- {
- if (!gyro.init())
- {
- char *message = "Failed to auto-detect Gyro type!";
- Error(message);
- }
- gyro.enableDefault();
- }
- void InitializeAltimeter()
- {
- alti.begin();
- alti.setOversampleRate(7);
- alti.enableEventFlags();
- alti.setModeActive();
- }
- ISR(TIMER3_COMPA_vect)
- {
- //Serial.println("Timer 3");
- SerialOutput();
- }
- ISR(TIMER4_COMPA_vect)
- {
- //Serial.println("Timer 4");
- ReadTemp();
- ReadAltimeter();
- }
- ISR(TIMER5_COMPA_vect)
- {
- ReadClock();
- //Serial.println("Timer 5");
- Draw();
- }
- void loop() {
- if (ESP.available())
- {
- //ReadESP();
- }
- if (GPS.available())
- {
- //ReadGPS();
- }
- ReadAccelerometer();
- ReadGyro();
- }
- void ReadESP()
- {
- //espData = "";
- while (ESP.available())
- {
- char inByte = ESP.read();
- //espData += inByte;
- Serial.print(inByte);
- }
- //Serial.println(espData);
- }
- void ReadGPS()
- {
- //gpsData = "";
- while (GPS.available())
- {
- char inByte = GPS.read();
- //strcat(gpsData, inByte);
- //gpsData += inByte;
- Serial.print(inByte);
- }
- //Serial.println(gpsData);
- }
- void ReadAltimeter()
- {
- alti.setModeBarometer();
- pressureP = alti.readPressure();
- alti.setModeAltimeter();
- altitudeM = alti.readAltitude();
- }
- void ReadAccelerometer()
- {
- accelX = Accel.mx_acceleration_x();
- accelY = Accel.mx_acceleration_y();
- tiltX = Accel.mx_tilt_x();
- tiltY = Accel.mx_tilt_y();
- rotation = Accel.mx_rotation();
- }
- void ReadGyro()
- {
- gyro.read();
- gyroX = gyro.g.x;
- gyroY = gyro.g.y;
- gyroZ = gyro.g.z;
- }
- void ReadTemp()
- {
- humidity = dht.readHumidity();
- heatIndex = dht.computeHeatIndex();
- tempAC = dht.readTemperature();
- tempBC = rtc.getTemp();
- tempCC = alti.readTemp();
- tempDC = Accel.mx_temperature();
- tempAF = convertCtoF(tempAC);
- tempBF = convertCtoF(tempBC);
- tempCF = convertCtoF(tempCC);
- tempDF = convertCtoF(tempDC);
- }
- void Draw()
- {
- DrawDate();
- DrawTime();
- DrawTemp();
- DrawAccelerometer();
- DrawGyro();
- }
- void SerialOutput()
- {
- Serial.println();
- Serial.print(__DATE__);
- Serial.print(" ");
- Serial.print(__TIME__);
- Serial.print(": ");
- Serial.println(__FILE__);
- Serial.print("Compiled: ");
- Serial.print(compileTime.mon);
- Serial.print("/");
- Serial.print(compileTime.date);
- Serial.print("/");
- Serial.print(compileTime.year);
- Serial.print(" ");
- Serial.print(compileTime.hour);
- Serial.print(":");
- Serial.print(compileTime.min);
- Serial.print(":");
- Serial.println(compileTime.sec);
- Serial.print("Current: ");
- Serial.print(currentTime.mon);
- Serial.print("/");
- Serial.print(currentTime.date);
- Serial.print("/");
- Serial.print(currentTime.year);
- Serial.print(" ");
- Serial.print(currentTime.hour);
- Serial.print(":");
- Serial.print(currentTime.min);
- Serial.print(":");
- Serial.println(currentTime.sec);
- Serial.print("Temp A: ");
- Serial.print(tempAC);
- Serial.print(" (");
- Serial.print(tempAF);
- Serial.print("F) | Temp B: ");
- Serial.print(tempBC);
- Serial.print(" (");
- Serial.print(tempBF);
- Serial.print("F) | Temp C: ");
- Serial.print(tempCC);
- Serial.print(" (");
- Serial.print(tempCF);
- Serial.print("F) | Temp D: ");
- Serial.print(tempDC);
- Serial.print(" (");
- Serial.print(tempDF);
- Serial.println("F)");
- Serial.print("Humidity: ");
- Serial.print(humidity);
- Serial.print("% | Heat Index: ");
- Serial.println(heatIndex);
- Serial.print("Pressure: ");
- Serial.print(pressureP);
- Serial.print(" pascals | Altitude: ");
- Serial.print(altitudeM);
- Serial.println(" meters");
- // Serial.print("Accel: ");
- // Serial.print(accelX);
- // Serial.print("X " );
- // Serial.print(accelY);
- // Serial.print("Y | Tilt: ");
- // Serial.print(tiltX);
- // Serial.print("X ");
- // Serial.print(tiltY);
- // Serial.print("Y | Rotation: ");
- // Serial.println(rotation);
- // Serial.print("Gyro: X");
- // Serial.print(gyroX);
- // Serial.print(" Y");
- // Serial.print(gyroY);
- // Serial.print(" Z");
- // Serial.println(gyroZ);
- //Serial.println(gpsData);
- //Serial.println(espData);
- }
- void DrawTemp()
- {
- tft.setCursor(30, 35);
- tft.println(tempAF);
- //tft.drawCentreString(&tempAFStr, 30, 35, 4);
- tft.setCursor(30, 65);
- tft.println(tempBF);
- //tft.drawCentreString(&tempBFStr, 30, 65, 4);
- tft.setCursor(30, 95);
- tft.println(tempCF);
- //tft.drawCentreString(&tempCFStr, 30, 95, 4);
- tft.setCursor(30, 125);
- tft.println(tempDF);
- //tft.drawCentreString(&tempCFStr, 30, 95, 4);
- tft.setCursor(25, 180);
- tft.println(humidity);
- tft.println("%");
- //tft.drawCentreString(&humidityStr, 25, 180, 4);
- }
- void DrawDate()
- {
- tft.setCursor(250, 20);
- tft.println(rtc.getDateStr());
- //tft.drawCentreString(rtc.getDateStr(), 250, 20, 4);
- }
- void DrawDate()
- {
- tft.setCursor(250, 50);
- tft.println(rtc.getTimeStr());
- //tft.drawCentreString(rtc.getTimeStr(), 250, 60, 4);
- }
- void DrawAccelerometer()
- {
- }
- void DrawGyro()
- {
- }
- void DrawAltimeter()
- {
- }
- /* *** HELPER METHODS *** */
- void Error(char* message)
- {
- tft.fillScreen(TFT_RED);
- tft.setTextColor(TFT_YELLOW);
- tft.setTextSize(5);
- tft.setCursor(160, 240);
- tft.drawCentreString(message, 160, 240, 5);
- Serial.println(message);
- while (1);
- }
- int convMonth(char* m)
- {
- if ( m == "Jan" ) return 1;
- else if ( m == "Feb" ) return 2;
- else if ( m == "Mar" ) return 3;
- else if ( m == "Apr" ) return 4;
- else if ( m == "May" ) return 5;
- else if ( m == "Jun" ) return 6;
- else if ( m == "Jul" ) return 7;
- else if ( m == "Aug" ) return 8;
- else if ( m == "Sep" ) return 9;
- else if ( m == "Oct" ) return 10;
- else if ( m == "Nov" ) return 11;
- else if ( m == "Dec" ) return 12;
- else
- {
- char *message = "Failed to parse month string to int!";
- Error(message);
- exit(-1);
- }
- }
- uint8_t conv2d(const char* p)
- {
- uint8_t v = 0;
- if ('0' <= *p && *p <= '9')
- v = *p - '0';
- return 10 * v + *++p - '0';
- }
- bool TimeTrigger(const Time &lastTime)
- {
- return lastTime.year < currentTime.year ||
- lastTime.mon < currentTime.mon ||
- lastTime.date < currentTime.date ||
- lastTime.hour < currentTime.hour ||
- lastTime.min < currentTime.min ||
- lastTime.sec < currentTime.sec;
- }
- float convertCtoF(const float & c)
- {
- return dht.convertCtoF(c);
- }
- float convertFtoC(const float & f)
- {
- return dht.convertFtoC(f);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement