Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.17 KB | None | 0 0
  1. #include <TinyGPS++.h>
  2. // Enable one of these two #includes and comment out the other.
  3. // Conditional #include doesn't work due to Arduino IDE shenanigans.
  4. #include <Wire.h> // Enable this line if using Arduino Uno, Mega, etc.
  5. //#include <TinyWireM.h> // Enable this line if using Adafruit Trinket, Gemma, etc.
  6.  
  7. #include "Adafruit_LEDBackpack.h"
  8. #include "Adafruit_GFX.h"
  9.  
  10. Adafruit_7segment red = Adafruit_7segment();
  11. Adafruit_7segment white = Adafruit_7segment();
  12.  
  13. static const uint32_t GPSBaud = 9600;
  14.  
  15. static const int lightPin = 5;
  16. static const int lowLight = 300;
  17. static const int highLight = 1000;
  18. static const int lowBrightness = 0;
  19. static const int highBrightness = 15;
  20.  
  21. static const int blackButtonPin = 12;
  22.  
  23. static const int clockOdoState = 0;
  24. static const int speedOdoState = 1;
  25. static const int brightnessState = 2;
  26. static const int batteryState = 3;
  27. static const int numStates = 4;
  28. int state = speedOdoState;
  29. boolean odoResetDisplay = false;
  30. static double tripDistance = 0.0; // miles
  31.  
  32. #define VBATPIN A9
  33.  
  34. // The TinyGPS++ object
  35. TinyGPSPlus gps;
  36. TinyGPSCustom fixQuality(gps, "GPGSA", 2); // fix
  37. static const int timeout = 5000;
  38.  
  39. void setup()
  40. {
  41. Serial.begin(9600);
  42. Serial1.begin(GPSBaud);
  43. red.begin(0x70);
  44. white.begin(0x71);
  45. pinMode(blackButtonPin, INPUT_PULLUP); // button
  46. }
  47.  
  48. void loop()
  49. {
  50. // This sketch displays information every time a new sentence is correctly encoded.
  51. while (Serial1.available() > 0)
  52. gps.encode(Serial1.read());
  53.  
  54. // read photoresister
  55. int light = constrain(analogRead(lightPin), lowLight, highLight);
  56. int brightness = map(light, lowLight, highLight, lowBrightness, highBrightness);
  57. red.setBrightness(brightness);
  58. white.setBrightness(brightness);
  59.  
  60. // handle the button
  61. odoButton(blackButtonPin);
  62.  
  63. // check to see if the gps was disconnected somehow
  64. if (gps.time.age() < timeout)
  65. {
  66. switch(state) {
  67. case clockOdoState:
  68. if(gps.time.isUpdated())
  69. displayClock(white);
  70. break;
  71.  
  72. case speedOdoState:
  73. if(gps.speed.isUpdated())
  74. displaySpeed(white);
  75. break;
  76.  
  77. case brightnessState:
  78. displayBrightness(white, brightness);
  79. break;
  80.  
  81. case batteryState:
  82. displayBatteryVoltage(white);
  83. break;
  84. }
  85.  
  86. if (gps.location.isUpdated()) {
  87. if(odoResetDisplay == false) {
  88. displayOdo(red);
  89. } else {
  90. red.printError();
  91. }
  92. }
  93.  
  94. } else {
  95. red.printError();
  96. white.printError();
  97. }
  98.  
  99. red.writeDisplay();
  100. white.writeDisplay();
  101. }
  102.  
  103. void displayBrightness(Adafruit_7segment &disp, int brightness)
  104. {
  105. disp.print(brightness);
  106.  
  107. // display a B or 8 or whatever you want to call it
  108. disp.writeDigitRaw(0, 0b11111111);
  109. }
  110.  
  111. void displayBatteryVoltage(Adafruit_7segment &disp)
  112. {
  113. float measuredvbat = analogRead(VBATPIN);
  114. measuredvbat *= 2; // we divided by 2, so multiply back
  115. measuredvbat *= 3.3; // Multiply by 3.3V, our reference voltage
  116. measuredvbat /= 1024; // convert to voltage
  117.  
  118. disp.print(measuredvbat);
  119. }
  120.  
  121. void displayClock(Adafruit_7segment &disp)
  122. {
  123. // assume clock is updated
  124.  
  125. int minutesecond = gps.time.minute() * 100 + gps.time.second();
  126. disp.print(minutesecond, DEC);
  127. disp.drawColon(true);
  128. }
  129.  
  130. void displayOdo(Adafruit_7segment &disp)
  131. {
  132. // assume gps.location is updated
  133. static TinyGPSLocation previousLocation;
  134.  
  135. if(previousLocation.isValid()) // not the first time
  136. {
  137. double distance = gps.distanceBetween(gps.location.lat(), gps.location.lng(),
  138. previousLocation.lat(), previousLocation.lng()) * _GPS_MILES_PER_METER;
  139.  
  140. if(gps.speed.isValid() && gps.speed.mph() > 1.0)
  141. {
  142. // increment the trip distance
  143. tripDistance += distance;
  144. }
  145.  
  146. // display the distance
  147. disp.print(tripDistance);
  148. }
  149.  
  150. previousLocation = gps.location;
  151. }
  152.  
  153. void displaySpeed(Adafruit_7segment &disp)
  154. {
  155. // assume gps.speed.updated()
  156. static int speed = 0;
  157.  
  158. if(gps.speed.isValid() && gps.speed.mph() != speed)
  159. {
  160. speed = gps.speed.mph();
  161. }
  162.  
  163. disp.print(speed);
  164.  
  165. // display fix quality
  166. static char mask = 0b01000000;
  167. if( fixQuality.isUpdated() )
  168. {
  169. int fix = atoi(fixQuality.value());
  170. if( fix == 2 ) {
  171. mask = 0b01001001;
  172. } else if ( fix == 3 ) {
  173. mask = 0b01001001;
  174. } else {
  175. mask = 0b01000000;
  176. }
  177. }
  178.  
  179. disp.writeDigitRaw(0, mask);
  180. }
  181.  
  182. void odoButton(int pin)
  183. {
  184. static long button_timer = 0;
  185. static const long longpress_time = 500;
  186. static const long shortpress_time = 50;
  187.  
  188. static boolean button_active = false;
  189. static boolean longpress_active = false;
  190.  
  191. if(digitalRead(pin) == LOW) {
  192. // button pressed
  193.  
  194. if (button_active == false) {
  195. button_active = true;
  196. button_timer = millis();
  197. }
  198.  
  199. if ((millis() - button_timer > longpress_time) && (longpress_active == false)) {
  200. longpress_active = true;
  201. odoResetDisplay = true;
  202.  
  203. // reset trip distance
  204. tripDistance = 0.0;
  205. }
  206. } else {
  207. // button not pressed
  208.  
  209. if(button_active == true) {
  210. if(longpress_active == true) {
  211. odoResetDisplay = false;
  212. longpress_active = false;
  213. } else if(millis() - button_timer > shortpress_time) {
  214. // switch state
  215. state = (state + 1) % numStates;
  216. }
  217. button_active = false;
  218. }
  219. }
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement