Advertisement
sprocket2cog

arduino bike dash 2.5

Dec 17th, 2015
1,078
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // two buttons on digital pins 7 and 9, voltage divder on pin ang0 and thermistor on pin Ang1
  2.  
  3. // Temerature and Voltage display using i2c 0.96 inch OLED screen
  4.  
  5. #include <SPI.h>
  6. #include <Wire.h>
  7. #include <EEPROM.h>
  8. #include <Adafruit_GFX.h>
  9. #include <Adafruit_SSD1306.h>
  10. #include <Thermistor.h>
  11. //----OLED setup---------------------------
  12. #define OLED_RESET 4
  13. Adafruit_SSD1306 display(OLED_RESET);
  14.  
  15. byte x0 = 60;                           //center of screen (pixels)
  16. byte y0 = 32;                           //center of screen
  17. byte r = 30;                           // radius of the gauge circle (pixels)
  18.  
  19. //-----------BUTTONS-----------------------
  20. const byte BUTTON_PIN_A = 7;                 // the number of the pushbutton pin A (select
  21. const byte BUTTON_PIN_B = 9;                 // the number of the pushbutton pin B (reset)
  22. byte READING_A = LOW;// button A
  23. byte READING_B = LOW;// button B
  24. byte LAST_STATE_A = LOW;                   // the previous reading from the input pin
  25. byte LAST_STATE_B = LOW;                  // the previous reading from the input pin
  26.  
  27. //--------------TEMP-----------------------
  28. Thermistor TEMP(1);                          // A1- TEMPERATURE probe
  29. byte TEMP_MAX = 0;                           // variable to store the max read temp.
  30. byte TEMPERATURE = 0;                        //setp TEMPERATURE
  31. int TEMP_SENSOR = 0;
  32.  
  33. //--------------VOLTAGE--------------------
  34. byte VOLTS_PIN = 0;                         // the A0 pin the voltage divider from the bike is on. (12 VOLTS=1.2 VOLTS)
  35. double VOLTS = 0;                           //setup VOLTS (read pin in to this variable)
  36. double VOLTS_MAX = 0;                      // variable to store the max read VOLTS.
  37. double VOLTS_SENSOR = 0;
  38.  
  39. //--------------VOLTAGE AVERAGING ARRAY-----
  40. const int NUM_READINGS = 20;           //number of samples to read to base the AVERAGE on
  41. double READINGS[NUM_READINGS];      // the READINGS array from the analog input VOLTS
  42. int READ_INDEX = 0;              // the index of the current reading
  43. double TOTAL = 0;                  // the running TOTAL
  44. double AVERAGE = 0;               //the final AVERAGE value
  45.  
  46. //-----------DEBOUNCE----------------------
  47. long LAST_DEBOUNCE = 0;                // the last time the output pin was toggled
  48. long DEBOUNCE_DELAY = 50;                 // the debounce time; increase if the output flickers
  49. byte BUTTON_STATE_A;
  50. byte BUTTON_STATE_B;                      // the current reading from the input pin
  51.  
  52. //-------------SCREEN NUMBER------------------
  53. byte SCREEN_SWITCH = 0;                      // switch case var. update via button A to switch display options
  54.  
  55. //-------HIGH/LOW WARNING THERSHOLDS-----------
  56. byte WARNING1 = 90;  //85-100?              // TEMPERATURE warning thershold in deg C.
  57. double WARNING2 = 14.5;                    // high VOLTS warning thershold in VOLTS
  58. byte WARNINGLOW1 = 12.1;                  //low voltage warning thershold
  59.  
  60. //----------EEPROM STUFF-----------------------
  61. unsigned long interval = 30000;//180000;          // (3 mins wait time we need to wait between checking max temp/VOLTS- to save writes to eeprom.
  62. unsigned long previousMillis = 0;        // millis() returns an unsigned long for timer checker
  63. byte OIL_ADDRS = 0;                        //address in EEPROM to store TEMPERATURE max.
  64. byte VOLTS_ADDRS = 1;                      //address in EEPROM to store VOLTS max.
  65.  
  66. // ---------------------------------------byte array for boot image screen in .xbm array format- search adafruit gfx xbitmap for details
  67. static const uint8_t  PROGMEM CAFE_XBM[] = {
  68.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  69.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  70.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x01, 0xf8, 0xf1,
  71.   0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xfc, 0xe0, 0x1f,
  72.   0xe0, 0x03, 0xfe, 0xf3, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0,
  73.   0x0f, 0xfe, 0xe1, 0x1f, 0xe0, 0x03, 0x0f, 0x72, 0x00, 0x00, 0x00, 0x00,
  74.   0x00, 0x00, 0x00, 0xc0, 0x0e, 0xc6, 0xe3, 0x00, 0xf0, 0x07, 0x07, 0x70,
  75.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x82, 0xe3, 0x00,
  76.   0x70, 0x87, 0x03, 0x70, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00,
  77.   0x0e, 0x80, 0xe3, 0x0f, 0x30, 0x86, 0x03, 0xf0, 0x0f, 0x00, 0x00, 0x00,
  78.   0xfc, 0x00, 0x00, 0x00, 0x0e, 0x80, 0xe3, 0x1f, 0x38, 0x8e, 0x03, 0xf0,
  79.   0x0f, 0x00, 0x00, 0x00, 0xfe, 0x01, 0x00, 0x00, 0x0e, 0xc0, 0x21, 0x3c,
  80.   0xf8, 0x8f, 0x03, 0x70, 0x00, 0x00, 0x00, 0x00, 0xff, 0x01, 0x00, 0x00,
  81.   0x0e, 0xf0, 0x01, 0x38, 0xfc, 0x1f, 0x07, 0x70, 0x00, 0x00, 0x00, 0x00,
  82.   0xff, 0x01, 0x00, 0x00, 0x0e, 0xf8, 0x00, 0x38, 0x1c, 0x1c, 0x0f, 0x72,
  83.   0x00, 0x00, 0x00, 0xf0, 0xff, 0x01, 0x00, 0x00, 0x0e, 0x7c, 0x20, 0x3c,
  84.   0x1c, 0x1c, 0xfe, 0xf3, 0x1f, 0x00, 0x00, 0xfe, 0xff, 0x01, 0x00, 0xc0,
  85.   0x7f, 0xfe, 0xe3, 0x1f, 0x0e, 0x38, 0xf8, 0xf1, 0x1f, 0x00, 0x00, 0xff,
  86.   0xff, 0x01, 0x00, 0xc0, 0x7f, 0xfe, 0xc3, 0x0f, 0x00, 0x00, 0x00, 0x00,
  87.   0x00, 0x00, 0x80, 0xff, 0xff, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  88.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0xff, 0xff, 0x00, 0x00, 0x00,
  89.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0xff,
  90.   0x7f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  91.   0x00, 0x00, 0xf0, 0xff, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  92.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0xff, 0x0f, 0x00, 0x00, 0x00,
  93.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0xff,
  94.   0x1f, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  95.   0x00, 0x00, 0xfe, 0xff, 0x7f, 0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  96.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xfd, 0x00, 0x00,
  97.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x3f,
  98.   0xff, 0xff, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  99.   0x00, 0x80, 0xff, 0x0f, 0xfe, 0xff, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00,
  100.   0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0xff, 0xcf, 0xff, 0xff, 0x1f, 0x00,
  101.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0xff, 0xff,
  102.   0xff, 0xff, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  103.   0x80, 0xdf, 0xff, 0xff, 0xff, 0xff, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00,
  104.   0x00, 0x00, 0x00, 0x00, 0xe0, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0f, 0x00,
  105.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0xff, 0xff, 0xff,
  106.   0xff, 0xff, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  107.   0xf0, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  108.   0x00, 0x00, 0x00, 0x00, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00,
  109.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0xff, 0xff,
  110.   0xff, 0xf3, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  111.   0xe0, 0xff, 0xff, 0xff, 0xbf, 0xc3, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00,
  112.   0x00, 0x00, 0x00, 0x00, 0xf8, 0xff, 0xff, 0xff, 0xff, 0xc1, 0xff, 0x07,
  113.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0xff, 0xe1, 0xff,
  114.   0xff, 0xc1, 0xff, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  115.   0xff, 0xff, 0x83, 0xff, 0xff, 0xc1, 0xff, 0x1f, 0x00, 0x00, 0x00, 0x00,
  116.   0x00, 0x00, 0x00, 0x80, 0x0f, 0xfe, 0x87, 0xff, 0xff, 0xe1, 0x0f, 0x3e,
  117.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x07, 0xfe, 0xe7, 0xff,
  118.   0xff, 0xf3, 0x0f, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0,
  119.   0x03, 0xcf, 0xff, 0xff, 0xff, 0xfb, 0x1e, 0x78, 0x00, 0x00, 0x00, 0x00,
  120.   0x00, 0x00, 0x00, 0xe0, 0x87, 0xcf, 0xff, 0xff, 0xff, 0x7b, 0x3c, 0xf0,
  121.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0xff, 0xff, 0xff, 0xff,
  122.   0xff, 0x39, 0xfc, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0,
  123.   0xfe, 0xff, 0xff, 0xff, 0xff, 0x3d, 0xfc, 0xe1, 0x00, 0x00, 0x00, 0x00,
  124.   0x00, 0x00, 0x00, 0xe0, 0xfc, 0xff, 0xff, 0xff, 0xff, 0x3d, 0xfe, 0xe1,
  125.   0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0xf8, 0x9f, 0xff, 0xff,
  126.   0xff, 0x3c, 0xfe, 0xe1, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0,
  127.   0xf0, 0xff, 0xff, 0xff, 0xff, 0x3c, 0xfe, 0xe1, 0x01, 0x00, 0x00, 0x00,
  128.   0x00, 0x00, 0x00, 0xe0, 0xf0, 0xff, 0xff, 0xff, 0x73, 0x3c, 0xfc, 0xe1,
  129.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0xe1, 0xff, 0xff, 0xff,
  130.   0x7f, 0x38, 0xf8, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0,
  131.   0x81, 0xc3, 0xff, 0xff, 0x3f, 0x78, 0x00, 0xf0, 0x00, 0x00, 0x00, 0x00,
  132.   0x00, 0x00, 0x00, 0xc0, 0x03, 0xe0, 0xff, 0xff, 0x1f, 0xf8, 0x00, 0x78,
  133.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x07, 0xf0, 0x01, 0x00,
  134.   0x00, 0xf0, 0x01, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80,
  135.   0x1f, 0xfc, 0x00, 0x00, 0x00, 0xe0, 0x07, 0x3f, 0x00, 0x00, 0x00, 0x00,
  136.   0x00, 0x00, 0x00, 0x00, 0xff, 0x7f, 0x00, 0x00, 0x00, 0xc0, 0xff, 0x1f,
  137.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0x3f, 0x00, 0x00,
  138.   0x00, 0x80, 0xff, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  139.   0xf8, 0x1f, 0x00, 0x00, 0x00, 0x00, 0xff, 0x03, 0x00, 0x00, 0x00, 0x00,
  140.   0x00, 0x00, 0x00, 0x00, 0xc0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x70, 0x00,
  141.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  142.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  143.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  144.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  145.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  146.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  147.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  148.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  149.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  150.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  151.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  152.   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  153.   0x00, 0x00, 0x00, 0x00
  154. };
  155.  
  156. void setup()  {
  157.   for (byte thisReading = 0; thisReading < NUM_READINGS; thisReading++) {
  158.     READINGS[thisReading] = 0;
  159.   }
  160.   display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3C -use i2c scanner sketch to find your screen location.
  161.   display.clearDisplay();
  162.   display.drawXBitmap(0, 0,  CAFE_XBM, 128, 64, 1); // start screen logo
  163.   display.display();
  164.   delay(3000);
  165.   display. fillRect( 0,  0, 128,  96, BLACK);
  166.   display.display();
  167.   display.setTextSize(2);
  168.   display.setTextColor(WHITE);
  169.   display.setCursor(0, 0);
  170.   TEMP_MAX = EEPROM.read(0);
  171.   VOLTS_MAX = EEPROM.read(1);
  172.   delay(100);  //sanity delay on startup to make sure EEPROM is read
  173.   display.clearDisplay();
  174. }
  175.  
  176. void loop() {
  177.   //---------read the analog volts and temperature in----
  178.   TEMPERATURE = TEMP.getTemp(); // read the thermistor value using the libary.
  179.   VOLTS = analogRead(VOLTS_PIN); // read in VOLTS 0-1023 increments for 0-50 volt range.
  180.   VOLTS = map(VOLTS, 0, 1023, 0, 500); // remap the VOLTS to a better range
  181.   VOLTS = VOLTS / 10;   // divide by ten to get 0 to 50 VOLTS again
  182.   VOLTS = VOLTS * 1; // correction factor for the resistor divider voltage diferance
  183.   TEMP_SENSOR = TEMPERATURE - 3; //this is the variable for oil TEMPERATURE from eeprom
  184.   VOLTS_SENSOR = AVERAGE; // this is the VOLTS from EEPROM
  185.  
  186.   //---------------------averaging out the volts to stop jitter--
  187.   // subtract the last reading:
  188.   TOTAL = TOTAL - READINGS[READ_INDEX];
  189.   // read from the sensor:
  190.   READINGS[READ_INDEX] = VOLTS;
  191.   // add the reading to the TOTAL:
  192.   TOTAL = TOTAL + READINGS[READ_INDEX];
  193.   // advance to the next position in the array:
  194.   READ_INDEX = READ_INDEX + 1;
  195.  
  196.   // if we're at the end of the array...
  197.   if (READ_INDEX >= NUM_READINGS) {
  198.     // ...wrap around to the beginning:
  199.     READ_INDEX = 0;
  200.   }
  201.   // calculate the AVERAGE VOLTS reading to stop jitter
  202.   AVERAGE = TOTAL / NUM_READINGS;
  203.   delay(1);        // delay in between reads for stability
  204.  
  205.   //--------------------check for button presses and apply debounce---
  206.   READING_B = digitalRead(BUTTON_PIN_B); // READ THE INPUT PIN
  207.   READING_A = digitalRead(BUTTON_PIN_A);
  208.   if (READING_A != LAST_STATE_A) {
  209.     // reset the debouncing timer
  210.     LAST_DEBOUNCE = millis();
  211.   }
  212.   if (READING_B != LAST_STATE_B) {
  213.     // reset the debouncing timer
  214.     LAST_DEBOUNCE = millis();
  215.   }
  216.   //-------create a time delay to stop the EEPROM getting written to all the time----
  217.   if ((unsigned long)(millis() - previousMillis) >= interval) {
  218.     previousMillis = millis();
  219.     //---------these routines check and store the maximum values to EEPROM
  220.     readMax();                                          // void to set new TEMPERATURE data and update eeprom
  221.     readMaxV();                                         // void to set new max VOLTS if higher and save to eeprom
  222.   }
  223.   //------------------
  224.  
  225.   resetMax();// check if button B pressed and reset max values in eeprom
  226.   SELECT_A(); // button A, change screens
  227.   LAST_STATE_A = READING_A; // set button reaing A
  228.   LAST_STATE_B = READING_B; // SET THE STATE OF THE BUTTON B FOR CHECKING IT LATER
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.   //----------------------------------------------
  236.   //--- case switching here for different display optins.
  237.   SELECT_A();
  238.   switch (SCREEN_SWITCH) {
  239.     case 0 :
  240.       display.setTextSize(2);
  241.       VOLTSB(0, 0, 56, 0); // display VOLTS text
  242.       oilB(0, 48, 72, 48); // display oil text
  243.       //display.fillRect( 0,  30,  128,  4,  WHITE);
  244.       display.drawCircle( 120,  48, 3,  WHITE);
  245.       break;
  246.  
  247.     case 1:  // main display screen 1
  248.       display.setTextSize(1);
  249.       VOLTSA(); // display VOLTS needle
  250.       VOLTSB(0, 0, 0, 8); // display VOLTS text
  251.       oilA(60, 32);  // display oil needle
  252.       oilB(0, 45, 0, 55); // display oil text
  253.       drawMAX(100, 55);
  254.       drawMAXv(94, 0);
  255.       MAX();//write the word max for main screen
  256.       DRAW_FACE();//draw the dial face circles last main screen
  257.       break;
  258.  
  259.     case 2 :
  260.       //oil bar graph here
  261.       display.fillRect( 0,  24,  (128 / 120)*TEMPERATURE,  20,  WHITE);
  262.       for (byte i = 0; i < 7; i++) {
  263.         display.drawLine(20 * i, 24, 20 * i, 48, BLACK);
  264.       }
  265.       oilB(0, 0, 72, 0); // display oil text
  266.       break;
  267.  
  268.     case 3:
  269.       display.fillRect( 0,  24,  (128 / 24)*AVERAGE,  20,  WHITE);
  270.       for (byte i = 0; i < 7; i++) {
  271.         display.drawLine(20 * i, 24, 20 * i, 48, BLACK);
  272.       }
  273.       VOLTSB(0, 0, 56, 0); // display VOLTS text
  274.       break;
  275.  
  276.     case 4:
  277.       display.fillRect( 0,  12,  (128 / 120)*TEMPERATURE,  20,  WHITE);
  278.       for (byte i = 0; i < 7; i++) {
  279.         display.drawLine(20 * i, 12, 20 * i, 36, BLACK);
  280.       }
  281.       display.fillRect( 0,  44,  (128 / 24)*AVERAGE,  20,  WHITE);
  282.       for (byte i = 0; i < 7; i++) {
  283.         display.drawLine(20 * i, 44, 20 * i, 64, BLACK);
  284.       }
  285.       display.setTextSize(1);
  286.       oilB(0, 0, 72, 0); // display oil text
  287.       VOLTSB(0, 34, 56, 34); // display VOLTS text
  288.       break;
  289.  
  290.     case 5: // single bottom sweep temp gauge
  291.       display.setTextSize(3);
  292.       display.setTextColor( 1 );
  293.       display.setCursor(46, 0);
  294.       display.println(TEMPERATURE - 3);
  295.       oilA(64, 60);  // display oil needle
  296.       display.drawRoundRect( 24,  24,  80,  46, 6 , WHITE);
  297.  
  298.       break;
  299.     case 6:
  300.       display.fillRect( 0,  0,  (128 / 24)*AVERAGE,  128,  WHITE);
  301.       for (byte i = 0; i < 7; i++) {
  302.         display.drawLine(20 * i, 0, 20 * i, 128, BLACK);
  303.  
  304.       }
  305.       display.fillTriangle(0, 0, 0, 46, 200, 0, BLACK  );
  306.       display.setTextSize(2);
  307.       display.setCursor(4, 4);
  308.       VOLTSB(0, 0, 60, 0); // display oil text
  309.       break;
  310.  
  311.     case 7:
  312.       display.fillRect( 0,  0,  (128 / 120)*TEMPERATURE - 3,  128,  WHITE);
  313.       for (byte i = 0; i < 7; i++) {
  314.         display.drawLine(20 * i, 0, 20 * i, 128, BLACK);
  315.  
  316.       }
  317.       display.fillTriangle(0, 0, 0, 46, 200, 0, BLACK  );
  318.       display.setTextSize(2);
  319.       display.setCursor(4, 4);
  320.       oilB(0, 0, 60, 0); // display oil text
  321.       break;
  322.  
  323.     case 8 :
  324.       display.setTextSize(1);
  325.       display.setCursor(10, 0);
  326.       display.setTextColor(0, 1 );
  327.       display.println("MAX STORED VALUES");
  328.       display.setTextColor(1 );
  329.       display.setTextSize(2);
  330.       VOLTSB(0, 20, 56, 20); // display VOLTS text
  331.       oilB(0, 48, 72, 48); // display oil text
  332.       display.fillRect( 50,  20,  82,  45,  BLACK);
  333.       drawMAX(72, 48);
  334.       drawMAXv(56, 20);
  335.       break;
  336.  
  337.     case 9:
  338.       // display. fillRect( 0,  0, 128,  96, BLACK);
  339.       display.drawXBitmap(0, 0,  CAFE_XBM, 128, 64, 1);
  340.       break;
  341.  
  342.     case 10:
  343.       display. fillRect( 0,  0, 128,  96, BLACK);
  344.  
  345.  
  346.  
  347.   }
  348.  
  349.   //---------------------------------------------
  350.   if (TEMPERATURE - 3 > WARNING1) {
  351.     display.setTextSize(2);
  352.     display.setTextColor(0, 1 );
  353.     display.setCursor(8, 24);
  354.     display.println("TEMP HIGH");
  355.  
  356.   }
  357.   //---------------------------------------------
  358.   if (VOLTS  < WARNINGLOW1) {
  359.     display.setTextSize(2);
  360.     display.setTextColor(0, 1 );
  361.     display.setCursor(8, 24);
  362.     display.println("VOLTS LOW");
  363.  
  364.   }
  365.   if (VOLTS > WARNING2) {
  366.     display.setTextSize(2);
  367.     display.setTextColor(0, 1 );
  368.     display.setCursor(8, 24);
  369.     display.println("VOLT HIGH");
  370.  
  371.   }
  372.   display.display();
  373.   display.clearDisplay();
  374.   delay(2);
  375. }
  376. //----end main void loop
  377.  
  378. //---------------start of function loops
  379.  
  380. void DRAW_FACE() {
  381.   //----center of the dial and the ring
  382.   display.drawCircle(x0, y0, r, WHITE);
  383.   display.fillCircle( x0,  y0,  6,  WHITE);
  384.   display.fillCircle( x0,  y0,  3,  BLACK  );
  385. }
  386.  
  387. void VOLTSA() { // voltage gauge needle
  388.   display.drawLine(x0, y0, x0 + r * sin(-135 + 2 * (VOLTS_SENSOR + 90) * 2 * 3.14 / 18), y0 - r * cos(-135 + 2 * (VOLTS_SENSOR + 90) * 2 * 3.14 / 18), WHITE);
  389.   display.fillCircle( x0,  y0,  20,  BLACK);
  390. }
  391. void VOLTSB(byte x, byte y, byte x1, byte y1) { // voltage text main screen
  392.  
  393.   display.setTextColor(WHITE);
  394.   display.setCursor(0 + x, 0 + y);
  395.   display.println("VOLT");
  396.   display.setCursor(0 + x1, 0 + y1);
  397.   display.println(VOLTS_SENSOR); //set voltage var to here later
  398. }
  399. void oilA(byte x1, byte y1) { //oil gauge needle
  400.  
  401.   display.drawLine(x1, y1, x1 + r * sin(293 + 2 * (TEMP_SENSOR) * 2 * 3.14 / 360), y1 - r * cos(293 + 2 * (TEMP_SENSOR) * 2 * 3.14 / 360), WHITE);
  402.   display.drawLine(x1 + 2, y1, x1 + r * sin(293 + 2 * (TEMP_SENSOR) * 2 * 3.14 / 360), y1 - r * cos(293 + 2 * (TEMP_SENSOR) * 2 * 3.14 / 360), WHITE);
  403.   display.drawLine(x1 - 2, y1, x1 + r * sin(293 + 2 * (TEMP_SENSOR) * 2 * 3.14 / 360), y1 - r * cos(293 + 2 * (TEMP_SENSOR) * 2 * 3.14 / 360), WHITE);
  404.   display.drawLine(x1, y1 + 2, x1 + r * sin(293 + 2 * (TEMP_SENSOR) * 2 * 3.14 / 360), y1 - r * cos(293 + 2 * (TEMP_SENSOR) * 2 * 3.14 / 360), WHITE);
  405.   display.drawLine(x1, y1 - 2, x1 + r * sin(293 + 2 * (TEMP_SENSOR) * 2 * 3.14 / 360), y1 - r * cos(293 + 2 * (TEMP_SENSOR) * 2 * 3.14 / 360), WHITE);
  406.  
  407. }
  408.  
  409. void oilB(byte x, byte y, byte x1, byte y1) { // oil temp text main screen
  410.  
  411.   display.setTextColor(WHITE);
  412.   display.setCursor(x, y);
  413.   display.println("TEMP");
  414.   display.setCursor(x1, y1);
  415.   display.println(TEMP_SENSOR); //display TEMPERATURE
  416. }
  417.  
  418.  
  419. void drawMAX(byte x, byte y) {
  420.   display.setCursor(x, y);
  421.   display.println(TEMP_MAX);
  422. }
  423.  
  424.  
  425. void MAX() {    //write the word MAX on screen
  426.   display.setTextColor(WHITE);
  427.   display.setCursor(105, 28);
  428.   display.println("MAX");
  429. }
  430.  
  431. void drawMAXv(byte x, byte y) { //draw max VOLTS on screen
  432.   display.setCursor(x, y);
  433.   display.println(VOLTS_MAX);
  434. }
  435.  
  436. void readMax() {                       // Save max temperature to eeprom
  437.   if (TEMP_SENSOR > TEMP_MAX) {
  438.     TEMP_MAX = TEMP_SENSOR;
  439.     EEPROM.update(OIL_ADDRS, TEMP_MAX);
  440.     display.setCursor(50, 0);
  441.     display.println("SAVED");
  442.     delay(30);
  443.   }
  444. }
  445.  
  446. void readMaxV() {
  447.   if (VOLTS > VOLTS_MAX) {
  448.     VOLTS_MAX = VOLTS;
  449.     EEPROM.update(VOLTS_ADDRS, VOLTS_MAX);
  450.     display.setCursor(50, 0);
  451.     display.println("SAVED");
  452.     delay(300);
  453.   }
  454. }
  455.  
  456.  
  457. int SELECT_A() { // Debounce and variable setting for button A
  458.   if ((millis() - LAST_DEBOUNCE) > DEBOUNCE_DELAY) {
  459.     if (READING_A != BUTTON_STATE_A) {
  460.       BUTTON_STATE_A = READING_A;
  461.       if (BUTTON_STATE_A == HIGH) {
  462.         if (SCREEN_SWITCH < 10) {
  463.           SCREEN_SWITCH = SCREEN_SWITCH + 1;
  464.         }
  465.         else {
  466.           SCREEN_SWITCH = 0;
  467.         }
  468.       }
  469.     }
  470.   }
  471. }
  472.  
  473.  
  474. void resetMax() { //Reset the stored max values to zero
  475.   if ((millis() - LAST_DEBOUNCE) > DEBOUNCE_DELAY) {
  476.     if (READING_B != BUTTON_STATE_B) {
  477.       BUTTON_STATE_B = READING_B;
  478.       if (BUTTON_STATE_B == HIGH) {
  479.         //ledState = !ledState;
  480.         TEMP_MAX = 0;
  481.         VOLTS_MAX = 0;
  482.         EEPROM.update(VOLTS_ADDRS, VOLTS_MAX);
  483.         EEPROM.update(OIL_ADDRS, TEMP_MAX);
  484.       }
  485.     }
  486.   }
  487. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement