Here is part 1 of my final project.
Below is the code and working display of my project. A lot of the live demonstration of my project can't be done due to the risk. I won't drive unsafely or get into an accident in order to show how the project works. I will give written examples of how the project works but like I said, I won't put mine or anyone's safety at risk for any project. If this will effect the outcome of my project, I am good with that.
Here is the code I used for the GPS Speedometer and the CO sensor:
#include <Adafruit_GFX.h> // Core graphics library #include <Adafruit_ST7735.h> // Hardware-specific library for ST7735 #include <Adafruit_ST7789.h> // Hardware-specific library for ST7789 #include <SPI.h> #include <SoftwareSerial.h> #include <TinyGPS.h> // Red = Ground // Black = 3v3 #define TFT_CS 10 // Yellow #define TFT_RST 9 // Or set to -1 and connect to Arduino RESET pin Orange #define TFT_DC 8 // White #define TFT_MOSI 11 // Data out Blue #define TFT_SCLK 13 // Clock out Green Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST); float lat, lon, lat2, lon2; // create variable for latitude and longitude object SoftwareSerial gpsSerial(3, 4); //rx,tx TinyGPS gps; // Push button setup const int buttonPin1 = 5; const int buttonPin2 = 6; const int buttonPin3 = 7; int buttonState1 = LOW; int buttonState2 = LOW; int buttonState3 = LOW; int x = 0; int y = 0; int z = 0; int a = 0; int b = 0; int c = 0; double kmh = 0; void setup(void) { Serial.begin(9600); gpsSerial.begin(9600); pinMode(2, OUTPUT); pinMode(buttonPin1, INPUT); pinMode(buttonPin2, INPUT); pinMode(buttonPin3, INPUT); // Use this initializer if using a 1.8" TFT screen: tft.initR(INITR_BLACKTAB); // Init ST7735S chip, black tab // Screen size 128x160 // SPI speed defaults to SPI_DEFAULT_FREQ defined in the library, you can override it here // Note that speed allowable depends on chip and quality of wiring, if you go too fast, you // may end up with a black screen some times, or all the time. //tft.setSPISpeed(40000000); tft.fillScreen(ST77XX_BLACK); tft.fillScreen(ST77XX_BLACK); tft.setCursor(30, 40); tft.setTextColor(ST77XX_RED); tft.setTextSize(1); tft.println("Just"); tft.setCursor(30, 60); tft.println("Encase"); tft.setCursor(30, 80); tft.println("Design"); tft.setCursor(30, 100); tft.println("Challenge !!"); delay(1500); tft.fillScreen(ST77XX_BLACK); tft.setCursor(30, 40); tft.println("Winter"); tft.setCursor(30, 60); tft.println("Driving"); tft.setCursor(30, 80); tft.println("Safety!! "); delay(1500); tft.fillScreen(ST77XX_BLACK); } void loop() { // Set current road conditions tft.setTextColor(ST77XX_RED); tft.setCursor(0, 20); tft.println("What are the current"); tft.setCursor(0, 40); tft.println("road Conditions?"); tft.setCursor(0, 80); tft.println("Press A for Clear"); tft.setCursor(0, 100); tft.println("Press B for Snow"); tft.setCursor(0, 120); tft.println("Press C for Icy"); do { buttonState1 = digitalRead(5); buttonState2 = digitalRead(6); buttonState3 = digitalRead(7); if (buttonState1 == HIGH) { x = 1; } if (buttonState2 == HIGH) { y = 1; } if (buttonState3 == HIGH) { z = 1; } }while (x == 0 & y == 0 & z == 0); tft.fillScreen(ST77XX_BLACK); buttonState1 = 0; buttonState2 = 0; buttonState3 = 0; // Set driving area do { tft.setTextColor(ST77XX_RED); tft.setCursor(0, 20); tft.println("Driving area?"); tft.setCursor(0, 40); tft.println("Press A for in Town"); tft.setCursor(0, 80); tft.println("Press B for Highway"); buttonState1 = digitalRead(buttonPin1); buttonState2 = digitalRead(buttonPin2); if (buttonState1 == HIGH) { a = 1; } if (buttonState2 == HIGH) { b = 1; } }while (a == 0 & b == 0); tft.fillScreen(ST77XX_BLACK); while (x == 1) { includeAllClear(); } while (y == 1) { includeAllSnow(); } while (z == 1) { includeAllIcy(); } } //Clear Road Condition void includeAllClear() { //Start and display CO levels tft.setTextColor(ST77XX_GREEN); int sensorValue = analogRead(A3); tft.setCursor(0, 0); tft.println("CO Levels: "); tft.setCursor(60, 0); tft.println(sensorValue / 10); delay(1); //Get GPS information while (gpsSerial.available()) { // check for gps data if (gps.encode(gpsSerial.read())) // encode gps data { gps.f_get_position(&lat, &lon); // get latitude and longitude } } delay(5000); while (gpsSerial.available()) { // check for gps data if (gps.encode(gpsSerial.read())) // encode gps data { gps.f_get_position(&lat2, &lon2); // get latitude and longitude } } double kmh = acos(cos(radians(90 - (lat, 6))) * cos(radians(90 - (lat2, 6))) + sin(radians(90 - (lat, 6))) * sin(radians(90 - (lat2, 6))) * cos(radians((lon, 6) - (lon2, 6)))) * 6371 * 60 * 12; tft.setTextSize(1); tft.setCursor(0, 60); tft.println("Speed Km/h:"); tft.setCursor(0, 100); tft.println(kmh, 1); if (a = 1 && kmh > 60) { tft.setTextColor(ST77XX_RED); tft.setCursor(0, 120); tft.println("Slow Down!!"); delay(2000); tft.setTextColor(ST77XX_BLACK); tft.setCursor(0, 120); tft.println("Slow Down!!"); } if (b = 1 && kmh > 100) { tft.setTextColor(ST77XX_RED); tft.setCursor(0, 120); tft.println("Slow Down!!"); delay(2000); tft.setTextColor(ST77XX_BLACK); tft.setCursor(0, 120); tft.println("Slow Down!!"); } delay(2000); //Clear displayed information tft.setTextColor(ST77XX_BLACK); tft.setTextSize(1); tft.setCursor(60, 0); tft.println(sensorValue / 10); tft.setCursor(0, 100); tft.println(kmh, 1); } //Snow Road Condition void includeAllSnow() { //Start and display CO levels tft.setTextColor(ST77XX_GREEN); int sensorValue = analogRead(A3); tft.setCursor(0, 0); tft.println("CO Levels: "); tft.setCursor(60, 0); tft.println(sensorValue / 10); delay(1); //Get GPS information while (gpsSerial.available()) { // check for gps data if (gps.encode(gpsSerial.read())) // encode gps data { gps.f_get_position(&lat, &lon); // get latitude and longitude } } delay(5000); while (gpsSerial.available()) { // check for gps data if (gps.encode(gpsSerial.read())) // encode gps data { gps.f_get_position(&lat2, &lon2); // get latitude and longitude } } double kmh = acos(cos(radians(90 - (lat, 6))) * cos(radians(90 - (lat2, 6))) + sin(radians(90 - (lat, 6))) * sin(radians(90 - (lat2, 6))) * cos(radians((lon, 6) - (lon2, 6)))) * 6371 * 60 * 12; tft.setTextSize(1); tft.setCursor(0, 60); tft.println("Speed Km/h:"); tft.setCursor(0, 100); tft.println(kmh, 1); if (a = 1 && kmh > 45) { tft.setTextColor(ST77XX_RED); tft.setCursor(0, 120); tft.println("Slow Down!!"); delay(2000); tft.setTextColor(ST77XX_BLACK); tft.setCursor(0, 120); tft.println("Slow Down!!"); } if (b = 1 && kmh > 80) { tft.setTextColor(ST77XX_RED); tft.setCursor(0, 120); tft.println("Slow Down!!"); delay(2000); tft.setTextColor(ST77XX_BLACK); tft.setCursor(0, 120); tft.println("Slow Down!!"); } delay(2000); //Clear displayed information tft.setTextColor(ST77XX_BLACK); tft.setTextSize(1); tft.setCursor(60, 0); tft.println(sensorValue / 10); tft.setCursor(0, 100); tft.println(kmh, 1); } // Icy Road Condition void includeAllIcy() { //Start and display CO levels tft.setTextColor(ST77XX_RED); int sensorValue = analogRead(A3); tft.setCursor(0, 0); tft.println("CO Levels: "); tft.setCursor(60, 0); tft.println(sensorValue / 10); delay(1); //Get GPS information while (gpsSerial.available()) { // check for gps data if (gps.encode(gpsSerial.read())) // encode gps data { gps.f_get_position(&lat, &lon); // get latitude and longitude } } delay(5000); while (gpsSerial.available()) { // check for gps data if (gps.encode(gpsSerial.read())) // encode gps data { gps.f_get_position(&lat2, &lon2); // get latitude and longitude } } double kmh = acos(cos(radians(90 - (lat, 6))) * cos(radians(90 - (lat2, 6))) + sin(radians(90 - (lat, 6))) * sin(radians(90 - (lat2, 6))) * cos(radians((lon, 6) - (lon2, 6)))) * 6371 * 60 * 12; tft.setTextSize(1); tft.setCursor(0, 60); tft.println("Speed Km/h:"); tft.setCursor(0, 100); tft.println(kmh, 1); if (a = 1 && kmh > 40) { tft.setTextColor(ST77XX_RED); tft.setCursor(0, 120); tft.println("Slow Down!!"); delay(2000); tft.setTextColor(ST77XX_BLACK); tft.setCursor(0, 120); tft.println("Slow Down!!"); } if (b = 1 && kmh > 75) { tft.setTextColor(ST77XX_RED); tft.setCursor(0, 120); tft.println("Slow Down!!"); delay(2000); tft.setTextColor(ST77XX_BLACK); tft.setCursor(0, 120); tft.println("Slow Down!!"); } delay(2000); //Clear displayed information tft.setTextColor(ST77XX_BLACK); tft.setTextSize(1); tft.setCursor(60, 0); tft.println(sensorValue / 10); tft.setCursor(0, 100); tft.println(kmh, 1); }
Accelerometer and Weather Station Code:
#include <Wire.h> #include "i2c.h" #include <LiquidCrystal.h> #include "i2c_BMP280.h" #include "ADXL335.h" BMP280 bmp280; float pascalhigh; float pascallow; float pressurerise; float pressuredrop; float pascaldiff; float pascaldiff1; float hg; float hg2; ADXL335 accelerometer; const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2; LiquidCrystal lcd(rs, en, d4, d5, d6, d7); // Connect BMP280 sensor to SDA (Y):20 SCL (B):21 // Connect Accel Sensor to O-A1, G-A2, Y-A3 void setup() { Serial.begin(115200); lcd.begin(16, 2); accelerometer.begin(); Serial.print("Probe BMP280: "); if (bmp280.initialize()) Serial.println("Sensor found"); else { Serial.println("Sensor missing"); while (1) {} } // onetime-measure: bmp280.setEnabled(0); bmp280.triggerMeasurement(); float pascal; bmp280.getPressure(pascal); pascalhigh = 0; pascallow = 2000000; pascaldiff1 = 0; hg2 = pascal; } void loop() { int x,y,z; accelerometer.getXYZ(&x,&y,&z); float ax,ay,az; accelerometer.getAcceleration(&ax,&ay,&az); Serial.println("accleration of X/Y/Z: "); Serial.print(ax); Serial.println(" g"); Serial.print(ay); Serial.println(" g"); Serial.print(az); Serial.println(" g"); if ((ax+ay+az)>19){ Serial.println("Danger, Slow Down"); lcd.setCursor(0,1); lcd.print("Slow Down!!"); delay(1000); } bmp280.awaitMeasurement(); float temperature; bmp280.getTemperature(temperature); float pascal; bmp280.getPressure(pascal); hg = pascal / 3386.389; static float meters, metersold; bmp280.getAltitude(meters); metersold = (metersold * 10 + meters)/11; bmp280.triggerMeasurement(); Serial.print("- Altitude: "); Serial.print(metersold); Serial.print(" - Temp "); Serial.print(temperature); Serial.println(" °C "); if(pascal>100914.3 && pascal<102269.0) { Serial.print(hg); Serial.print(" Hg; "); Serial.print(": Change"); lcd.setCursor(0,0); lcd.print("Weather Change"); } if(pascal<100914.4 && pascal > 97000.00) { Serial.print(hg); Serial.print(" Hg; "); Serial.print(": Rain/Snow"); lcd.setCursor(0,0); lcd.print("Rain/Snow"); } if(pascal<97000.1) { Serial.print(hg); Serial.print(" Hg; "); Serial.print(": Stormy"); lcd.setCursor(0,0); lcd.print("Stormy"); } if(pascal>102268.9 && pascal<105000.0) { Serial.print(hg); Serial.print(" Hg; "); Serial.print(": Fair"); lcd.setCursor(0,0); lcd.print("Fair"); } if(pascal>104999.9) { Serial.print(hg); Serial.print(" Hg; "); Serial.print(": Dry"); lcd.setCursor(0,0); lcd.print("Dry"); } if(hg2 > (hg-.05)or hg2 < (hg+.05)) { Serial.print(" : No Change "); lcd.setCursor(0,1); lcd.print("No Change"); } else if(hg2 > (hg-.15)) { Serial.print(" : Weather to Improve "); lcd.setCursor(0,1); lcd.print("Weather Improve"); } else if(hg2 < (hg+.15)) { Serial.print(" : Weather to Decline "); lcd.setCursor(0,1); lcd.print("Weather Decline"); } if(pascalhigh < pascal) { pascalhigh = pascal; } if(pascallow > pascal) { pascallow = pascal; } }
Here are pictures of the Speedometer/CO Sensor Output:
The first screen is where you set the current road conditions:
This screen is where you enter where you will be driving:
More to come tomorrow!!
Dale Winhold