Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h>
- #include <Wire.h>
- #include <EEPROM.h>
- #include <Adafruit_GFX.h>
- #include <Adafruit_SSD1306.h>
- #define OLED_RESET 4
- Adafruit_SSD1306 display(OLED_RESET);
- #include <Thermistor.h>
- Thermistor temp(1);// A1- temperature probe
- int VOLTS_PIN = 0; // the A0 pin the voltage divider from the bike is on. (12 volts=1.2 volts)
- double volts=0; //setup volts (read pin in to this variable)
- const int buttonPinA = 7; // the number of the pushbutton pin A (select
- const int buttonPinB = 9; // the number of the pushbutton pin B (reset)
- int readingA = LOW;// button A
- int readingB = LOW;// button B
- int lastButtonStateA = LOW; // the previous reading from the input pin
- int lastButtonStateB = LOW; // the previous reading from the input pin
- long lastDebounceTime = 0; // the last time the output pin was toggled
- long debounceDelay = 100; // the debounce time; increase if the output flickers
- int buttonStateB; // the current reading from the input pin
- int buttonStateA;
- int SC1 = 0; // switch case var. update via button A to switch display options
- int TEMP_MAX=0; // variable to store the max read temp.
- float VOLTS_MAX=0; // variable to store the max read volts.
- int WARNING1=30; //85-100? // temperature warning thershold in deg C.
- float WARNING2=15.5; // temperature warning thershold in volts
- unsigned long interval=45000; // the time we need to wait to read max- to save writes to eeprom.
- unsigned long previousMillis=0; // millis() returns an unsigned long for timer checker
- int OIL_ADDRS = 0; //address in EEPROM to store temperature max.
- int VOLTS_ADDRS = 1; //address in EEPROM to store volts max.
- int temperature=0; //setp temperature
- //center of screen
- int x0=60;
- int y0=32;
- int a=2;
- // radius
- int r=30;
- int TEMP_SENSOR=0;
- double VOLTS_SENSOR=0;
- // byte array for boot image screen in .xbm array format- search adafruit gfx xbitmap for details
- static const uint8_t PROGMEM CAFE_XBM[] ={
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xcf, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0x01, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0xfe, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0x00, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0x0f, 0x00, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00, 0xfe, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
- 0x00, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0x7f, 0x00, 0x00, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0x00, 0x00, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0x00,
- 0x80, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0x0f, 0x00, 0xf0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x07, 0x00, 0xf0, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x03, 0x00,
- 0xe0, 0x9f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0x01, 0x00, 0x80, 0x87, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x02, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0xc0,
- 0x00, 0x00, 0xe0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0x7f, 0x00, 0xf0, 0x01, 0x00, 0xe0, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, 0x00, 0x30, 0x00, 0x00, 0xe0, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0x00, 0x00,
- 0x00, 0x00, 0xe0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0x7f, 0x20, 0x00, 0x00, 0x00, 0x00, 0xe0, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0xfc, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x03, 0x00, 0x00,
- 0x00, 0x0c, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0x1f, 0x00, 0x00, 0x00, 0x40, 0x3c, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0x07, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x00, 0xf8,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00, 0x1e, 0x00,
- 0x00, 0x3e, 0x00, 0xf0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0x00, 0x00, 0x7c, 0x00, 0x00, 0x3e, 0x00, 0xe0, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0x7f, 0xf0, 0x01, 0x78, 0x00, 0x00, 0x1e, 0xf0, 0xc1,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xf8, 0x01, 0x18, 0x00,
- 0x00, 0x0c, 0xf0, 0x83, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f,
- 0xfc, 0x30, 0x00, 0x00, 0x00, 0x04, 0xe1, 0x87, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0x1f, 0x78, 0x30, 0x00, 0x00, 0x00, 0x84, 0xc3, 0x0f,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0xc6, 0x03, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f,
- 0x01, 0x00, 0x00, 0x00, 0x00, 0xc2, 0x03, 0x1e, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0x1f, 0x03, 0x00, 0x00, 0x00, 0x00, 0xc2, 0x01, 0x1e,
- 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0f, 0x07, 0x60, 0x00, 0x00,
- 0x00, 0xc3, 0x01, 0x1e, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f,
- 0x0f, 0x00, 0x00, 0x00, 0x00, 0xc3, 0x01, 0x1e, 0xfe, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0x1f, 0x0f, 0x00, 0x00, 0x00, 0x8c, 0xc3, 0x03, 0x1e,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0x1e, 0x00, 0x00, 0x00,
- 0x80, 0xc7, 0x07, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f,
- 0x7e, 0x3c, 0x00, 0x00, 0xc0, 0x87, 0xff, 0x0f, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0x3f, 0xfc, 0x1f, 0x00, 0x00, 0xe0, 0x07, 0xff, 0x87,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xf8, 0x0f, 0xfe, 0xff,
- 0xff, 0x0f, 0xfe, 0x83, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f,
- 0xe0, 0x03, 0xff, 0xff, 0xff, 0x1f, 0xf8, 0xc0, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0x00, 0x80, 0xff, 0xff, 0xff, 0x3f, 0x00, 0xe0,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0xc0, 0xff, 0xff,
- 0xff, 0x7f, 0x00, 0xf0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0x07, 0xe0, 0xff, 0xff, 0xff, 0xff, 0x00, 0xfc, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0x3f, 0xfe, 0xff, 0xff, 0xff, 0xff, 0x8f, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- 0xff, 0xff, 0xff, 0xff };
- void setup() {
- Serial.begin(9600);
- display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C -use i2c scanner sketch to find your screen location.
- display.stopscroll();
- display.clearDisplay();
- display.drawXBitmap(0, 0, CAFE_XBM, 128, 64, 1);
- display.invertDisplay(true);
- display.display();
- delay(3000);
- display.invertDisplay(false);
- display. fillRect( 0, 0, 128, 96, BLACK);
- display.display();
- display.setTextSize(2);
- display.setTextColor(WHITE);
- display.setCursor(0,0);
- TEMP_MAX = EEPROM.read(0);// turn on to use EEprom settings temp
- VOLTS_MAX = EEPROM.read(1);// turn on to use EEprom settings volts
- delay(100); //sanity delay on startup to make sure EEPROM is read
- display.clearDisplay();
- }
- void loop() {
- temperature = temp.getTemp(); // read the thermistor value using the libary.
- volts = analogRead(VOLTS_PIN); // read in volts 0-1023 increments for 0-50 volt range.
- volts = map(volts, 0, 1023,0,500); // remap the volts to a better range
- volts = volts/10; // divide by ten to get 0 to 50 volts again
- volts= volts*.99; // correction factor for the resistor divider voltage diferance
- //volts=random(11,14); // psuedo code to simulate volts during testing
- TEMP_SENSOR=temperature;//this is the variable for oil temperature from eeprom
- VOLTS_SENSOR=volts;// this is the volts from EEPROM
- readingB = digitalRead(buttonPinB); // READ THE INPUT PIN
- readingA = digitalRead(buttonPinA);
- if (readingA!= lastButtonStateA) {
- // reset the debouncing timer
- lastDebounceTime = millis();
- }
- if (readingB != lastButtonStateB) {
- // reset the debouncing timer
- lastDebounceTime = millis();
- }
- if ((unsigned long)(millis() - previousMillis) >= interval) {
- previousMillis = millis();
- readMax();// set new temperature data and update eeprom
- readMaxV();// set new max volts if higher and save to eeprom
- }
- //------------------
- resetMax();// check if button B pressed and reset max values in eeprom
- SELECT_A(); // button A, change screens
- lastButtonStateA = readingA; // set button reaing A
- lastButtonStateB = readingB; // SET THE STATE OF THE BUTTON B FOR CHECKING IT LATER
- //----------------------------------------------
- //--- case switching here for different display optins.
- SELECT_A();
- switch (SC1){
- case 0: // main display screen 1
- display.setTextSize(1);
- voltsA(); // display volts needle
- voltsB(0,0,0,8); // display volts text
- oilA(); // display oil needle
- oilB(0,45,0,55); // display oil text
- drawMAX(100,55);
- drawMAXv(94,0);
- MAX();//write the word max for main screen
- DRAW_FACE();//draw the dial face circles last main screen
- break;
- case 1 :
- display.setTextSize(2);
- voltsB(0,0,56,0); // display volts text
- oilB(0,48,72,48); // display oil text
- display.fillRect( 0, 30, 128, 4, WHITE);
- display.drawCircle( 120, 48, 3, WHITE);
- break;
- case 2 :
- display.setTextSize(1);
- display.setCursor(0,0);
- display.println("MAX STORED VALUES");
- display.setTextSize(2);
- voltsB(0,26,56,26); // display volts text
- oilB(0,48,72,48); // display oil text
- display.fillRect( 50, 25, 82, 40, BLACK);
- drawMAX(72,48);
- drawMAXv(56,26);
- break;
- }
- //---------------------------------------------
- if(temperature > WARNING1){ //flash screen if temp gets above thershold
- DRAW_INVERT();
- }
- display.display();
- display.clearDisplay();
- delay(250);
- }
- //----end main void loop
- //---------------start of function loops
- void DRAW_FACE(){
- //----center of the dial and the ring
- display.drawCircle(x0, y0, r, WHITE);
- display.fillCircle( x0, y0, 6, WHITE);
- display.fillCircle( x0, y0, 3, BLACK );
- }
- void voltsA(){ // voltage gauge needle
- 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);
- display.fillCircle( x0, y0, 20, BLACK);
- }
- void voltsB(int x, int y, int x1, int y1){ // voltage text main screen
- display.setTextColor(WHITE);
- display.setCursor(0+x,0+y);
- display.println("VOLT");
- display.setCursor(0+x1,0+y1);
- display.println(VOLTS_SENSOR); //set voltage var to here later
- }
- void oilA(){ //oil gauge needle
- display.drawLine(x0,y0, x0+r*sin(293+2*(TEMP_SENSOR)*2*3.14/360), y0-r*cos(293+2*(TEMP_SENSOR)*2*3.14/360), WHITE);
- display.drawLine(x0+2,y0, x0+r*sin(293+2*(TEMP_SENSOR)*2*3.14/360), y0-r*cos(293+2*(TEMP_SENSOR)*2*3.14/360), WHITE);
- display.drawLine(x0-2,y0, x0+r*sin(293+2*(TEMP_SENSOR)*2*3.14/360), y0-r*cos(293+2*(TEMP_SENSOR)*2*3.14/360), WHITE);
- display.drawLine(x0,y0+2, x0+r*sin(293+2*(TEMP_SENSOR)*2*3.14/360), y0-r*cos(293+2*(TEMP_SENSOR)*2*3.14/360), WHITE);
- display.drawLine(x0,y0-2, x0+r*sin(293+2*(TEMP_SENSOR)*2*3.14/360), y0-r*cos(293+2*(TEMP_SENSOR)*2*3.14/360), WHITE);
- }
- void oilB(int x, int y, int x1, int y1){ // oil temp text main screen
- display.setTextColor(WHITE);
- display.setCursor(x,y);
- display.println("OIL");
- display.setCursor(x1,y1);
- display.println(TEMP_SENSOR); //display temperature
- }
- void resetMax(){
- if ((millis() - lastDebounceTime) > debounceDelay) {
- if (readingB != buttonStateB) {
- buttonStateB = readingB;
- if (buttonStateB == HIGH) {
- //ledState = !ledState;
- TEMP_MAX=0;
- VOLTS_MAX=0;
- }
- }
- }
- }
- void drawMAX(int x,int y){
- display.setCursor(x,y);
- display.println(TEMP_MAX);
- }
- void MAX(){ //write the word MAX on screen
- display.setTextColor(WHITE);
- display.setCursor(105,28);
- display.println("MAX");
- }
- void drawMAXv(int x, int y){ //draw max volts on screen
- display.setCursor(x,y);
- display.println(VOLTS_MAX);
- }
- void readMax(){
- if(temperature > TEMP_MAX){
- TEMP_MAX=temperature;
- EEPROM.update(OIL_ADDRS, TEMP_MAX);// turn on to start storing EEPROM data
- display.setCursor(50,0);
- display.println("SAVED");
- delay(30);
- }
- }
- void readMaxV(){
- if(volts > VOLTS_MAX){ // need to add function that reads analouge pin A2 and assigns volts
- VOLTS_MAX=volts;
- EEPROM.update(VOLTS_ADDRS, VOLTS_MAX);// turn on to start storing EEPROM data
- display.setCursor(50,0);
- display.println("SAVED");
- delay(300);
- }
- }
- void DRAW_INVERT(){
- display.invertDisplay(true);
- delay(10);
- display.invertDisplay(false);
- delay(10);
- }
- int SELECT_A(){
- if ((millis() - lastDebounceTime) > debounceDelay) {
- if (readingA != buttonStateA) {
- buttonStateA = readingA;
- if (buttonStateA == HIGH) {
- if (SC1 < 2){
- SC1= SC1+1;
- }
- else { SC1 =0;}
- //set SC to plus one etc if SC< 3 sc++ else SC=0;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement