How to make Car Gear Indicator with Hall and OLED Display using Arduino












CODE

#include <Wire.h>

#include <Adafruit_GFX.h>

#include <Adafruit_SSD1306.h>



#define SCREEN_WIDTH 128

#define SCREEN_HEIGHT 64



Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);



// Hall Sensors

const byte gearPins[6] = {2,3,4,5,6,7};



int currentGear = 0;

int lastGear = -1;



//-----------------------------



void setup()

{

  for(int i=0;i<6;i++)

    pinMode(gearPins[i], INPUT_PULLUP);



  display.begin(SSD1306_SWITCHCAPVCC,0x3C);



  display.clearDisplay();

  display.display();

}



//-----------------------------



void loop()

{

  currentGear = readGear();



  if(currentGear != lastGear)

  {

    lastGear = currentGear;

    drawDisplay(currentGear);

  }

}



//-----------------------------



int readGear()

{

  for(int i=0;i<6;i++)

  {

    if(digitalRead(gearPins[i])==LOW)

      return i+1;

  }



  return 0; // Neutral

}



//-----------------------------



void drawDisplay(int gear)

{

  display.clearDisplay();



  display.setTextColor(SSD1306_WHITE);



  display.setTextSize(1);

  display.setCursor(18,0);

  display.print("GEAR INDICATOR");



  drawPattern();



  highlightGear(gear);



  display.display();

}



//-----------------------------



void drawPattern()

{

  // H Pattern



  display.drawLine(20,21,20,48,WHITE);

  display.drawLine(64,21,64,48,WHITE);

  display.drawLine(108,21,108,48,WHITE);



  display.drawLine(20,35,64,35,WHITE);

  display.drawLine(64,35,108,35,WHITE);



  display.setTextSize(2);



  display.setCursor(14,9);

  display.print("1");



  display.setCursor(58,9);

  display.print("3");



  display.setCursor(102,9);

  display.print("5");



  display.setCursor(14,48);

  display.print("2");



  display.setCursor(58,48);

  display.print("4");



  display.setCursor(102,48);

  display.print("R");

}



//-----------------------------



void highlightGear(int gear)

{

  switch(gear)

  {

    case 1:

      display.drawRect(10,6,20,20,WHITE);

      break;



    case 2:

      display.drawRect(10,44,20,20,WHITE);

      break;



    case 3:

      display.drawRect(54,6,20,20,WHITE);

      break;



    case 4:

      display.drawRect(54,44,20,20,WHITE);

      break;



    case 5:

      display.drawRect(97,6,20,20,WHITE);

      break;



    case 6:

      display.drawRect(98,44,20,20,WHITE);

      break;



    default:

      display.fillRoundRect(52,25,24,21,2,WHITE);

      display.setTextColor(BLACK);

      display.setTextSize(2);

      display.setCursor(59,29);

      display.print("N");

      display.setTextColor(WHITE);

      break;

  }

}

Post a Comment

Previous Post Next Post

Contact Form