/**************************************************************************
This is an example for our Monochrome OLEDs based on SSD1306 drivers
Pick one up today in the adafruit shop!
------> http://www.adafruit.com/category/63_98
This example is for a 128x32 pixel display using I2C to communicate
3 pins are required to interface (two I2C and one reset).
Adafruit invests time and resources providing this open
source code, please support Adafruit and open-source
hardware by purchasing products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries,
with contributions from the open source community.
BSD license, check license.txt for more information
All text above, and the splash screen below must be
included in any redistribution.
**************************************************************************/
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// The pins for I2C are defined by the Wire-library.
//On an arduino UNO: A4(SDA), A5(SCL)
// On an arduino MEGA 2560: 20(SDA), 21(SCL)
// On an arduino LEONARDO: 2(SDA), 3(SCL), ...
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define NUMFLAKES 10 // Number of snowflakes in the animation example
#define LOGO_HEIGHT 16
#define LOGO_WIDTH 16
static const unsigned char PROGMEM logo_bmp[] =
{ 0b00000000, 0b11000000,
0b00000001, 0b11000000,
0b00000001, 0b11000000,
0b00000011, 0b11100000,
0b11110011, 0b11100000,
0b11111110, 0b11111000,
0b01111110, 0b11111111,
0b00110011, 0b10011111,
0b00011111, 0b11111100,
0b00001101, 0b01110000,
0b00011011, 0b10100000,
0b00111111, 0b11100000,
0b00111111, 0b11110000,
0b01111100, 0b11110000,
0b01110000, 0b01110000,
0b00000000, 0b00110000 };
static const unsigned char PROGMEM num0[] =
{ 0b00011111, 0b11111000,
0b00111111, 0b11111100,
0b01111111, 0b11111110,
0b11111111, 0b11111111,
0b11111110, 0b01111111,
0b11111110, 0b01111111,
0b11111110, 0b01111111,
0b11111110, 0b01111111,
0b11111110, 0b01111111,
0b11111110, 0b01111111,
0b11111110, 0b01111111,
0b11111110, 0b01111111,
0b11111110, 0b01111111,
0b11111110, 0b01111111,
0b11111110, 0b01111111,
0b11111110, 0b01111111,
0b11111110, 0b01111111,
0b11111110, 0b01111111,
0b11111111, 0b11111111,
0b11111111, 0b11111111,
0b11111111, 0b11111111,
0b11111111, 0b11111111,
0b11111111, 0b11111111,
0b11111111, 0b11111111,
0b01111111, 0b11111110,
0b00111111, 0b11111100,
0b00011111, 0b11111000};
static const unsigned char PROGMEM num1[] =
{ 0b00000000, 0b11111100,
0b00000011, 0b11111110,
0b00001111, 0b11111110,
0b00111111, 0b11111110,
0b01111111, 0b11111110,
0b11111111, 0b11111110,
0b11111111, 0b11111110,
0b01111111, 0b11111110,
0b00000000, 0b11111110,
0b00000000, 0b11111110,
0b00000000, 0b11111110,
0b00000000, 0b11111110,
0b00000000, 0b11111110,
0b00000000, 0b11111110,
0b00000000, 0b11111110,
0b00000000, 0b11111110,
0b00000000, 0b11111110,
0b00000000, 0b11111110,
0b00000000, 0b11111110,
0b00000000, 0b11111110,
0b00000000, 0b11111110,
0b00000000, 0b11111110,
0b00000000, 0b11111110,
0b00000000, 0b11111110,
0b00000000, 0b11111110,
0b00000000, 0b11111110,
0b00000000, 0b01111100};
static const unsigned char PROGMEM num2[] =
{ 0b01111111, 0b11111110,
0b11111111, 0b11111111,
0b11111111, 0b11111111,
0b11111111, 0b00011111,
0b11111111, 0b00011111,
0b11111111, 0b00011111,
0b11111111, 0b11011111,
0b01111111, 0b11011111,
0b00000000, 0b00011111,
0b01111111, 0b11111111,
0b11111111, 0b11111111,
0b11111111, 0b11111110,
0b11111000, 0b00000000,
0b11111011, 0b11111110,
0b11111011, 0b11111111,
0b11111000, 0b11111111,
0b11111000, 0b11111111,
0b11111000, 0b11111111,
0b11111000, 0b11111111,
0b11111111, 0b11111111,
0b11111111, 0b11111111,
0b11111111, 0b11111111,
0b11111111, 0b11111111,
0b11111111, 0b11111111,
0b11111111, 0b11111111,
0b11111111, 0b11111111,
0b01111111, 0b11111110};
static const unsigned char PROGMEM num3[] =
{ 0b01111111, 0b11111110,
0b11111111, 0b11111111,
0b11111111, 0b11111111,
0b11111111, 0b00011111,
0b11111111, 0b00011111,
0b11111111, 0b00011111,
0b11111111, 0b11011111,
0b01111111, 0b11011111,
0b00000000, 0b00011111,
0b01111111, 0b11111111,
0b11111111, 0b11111111,
0b01111111, 0b11111111,
0b00000000, 0b00011111,
0b11111111, 0b11011111,
0b11111111, 0b11011111,
0b11111111, 0b11011111,
0b11111111, 0b00011111,
0b11111111, 0b00011111,
0b11111111, 0b00011111,
0b11111111, 0b00011111,
0b11111111, 0b11111111,
0b11111111, 0b11111111,
0b11111111, 0b11111111,
0b11111111, 0b11111111,
0b11111111, 0b11111111,
0b11111111, 0b11111111,
0b01111111, 0b11111110};
static const unsigned char PROGMEM num4[] =
{ 0b01111111, 0b11111100,
0b11111111, 0b11111110,
0b11111111, 0b11111110,
0b11111111, 0b11111110,
0b11111111, 0b01111110,
0b11111110, 0b01111110,
0b11111100, 0b01111110,
0b11111100, 0b01111110,
0b11111100, 0b01111110,
0b11111100, 0b00011110,
0b11111111, 0b11111111,
0b11111111, 0b11111111,
0b01111111, 0b11111111,
0b00000000, 0b11111110,
0b00000000, 0b11111110,
0b00000000, 0b11111110,
0b00000000, 0b11111110,
0b00000000, 0b11111110,
0b00000000, 0b11111110,
0b00000000, 0b11111110,
0b00000000, 0b11111110,
0b00000000, 0b11111110,
0b00000000, 0b11111110,
0b00000000, 0b11111110,
0b00000000, 0b11111110,
0b00000000, 0b11111110,
0b00000000, 0b01111100};
static const unsigned char PROGMEM num5[] =
{ 0b01111111, 0b11111110,
0b11111111, 0b11111111,
0b11111111, 0b11111111,
0b11111000, 0b11111111,
0b11111000, 0b11111111,
0b11111000, 0b11111111,
0b11111011, 0b11111111,
0b11111011, 0b11111110,
0b11111000, 0b00000000,
0b11111111, 0b11111110,
0b11111111, 0b11111111,
0b11111111, 0b11111111,
0b00000000, 0b00011111,
0b11111111, 0b11011111,
0b11111111, 0b11011111,
0b11111111, 0b00011111,
0b11111111, 0b00011111,
0b11111111, 0b00011111,
0b11111111, 0b00011111,
0b11111111, 0b11111111,
0b11111111, 0b11111111,
0b11111111, 0b11111111,
0b11111111, 0b11111111,
0b11111111, 0b11111111,
0b11111111, 0b11111111,
0b11111111, 0b11111111,
0b01111111, 0b11111110};
static const unsigned char PROGMEM num6[] =
{ 0b01111111, 0b11111110,
0b11111111, 0b11111111,
0b11111111, 0b11111111,
0b11111000, 0b11111111,
0b11111000, 0b11111111,
0b11111000, 0b11111111,
0b11111011, 0b11111111,
0b11111011, 0b11111110,
0b11111000, 0b00000000,
0b11111111, 0b11111110,
0b11111111, 0b11111111,
0b11111111, 0b11111111,
0b11110000, 0b11111111,
0b11111000, 0b11111111,
0b11111000, 0b11111111,
0b11111011, 0b11111111,
0b11111111, 0b11111111,
0b11111111, 0b11111111,
0b11111111, 0b11111111,
0b11111111, 0b11111111,
0b11111111, 0b11111111,
0b11111111, 0b11111111,
0b11111111, 0b11111111,
0b11111111, 0b11111111,
0b11111111, 0b11111111,
0b11111111, 0b11111111,
0b01111111, 0b11111110};
static const unsigned char PROGMEM num7[] =
{ 0b01111111, 0b11111110,
0b11111111, 0b11111111,
0b11111111, 0b11111111,
0b11111111, 0b11111111,
0b11111111, 0b11111111,
0b11111111, 0b11111111,
0b11111111, 0b11111111,
0b01111111, 0b11111111,
0b00000000, 0b11111111,
0b00000000, 0b11111111,
0b00000001, 0b11111110,
0b00000001, 0b11111110,
0b00000011, 0b11111100,
0b00000011, 0b11111100,
0b00000111, 0b11111000,
0b00000111, 0b11111000,
0b00001111, 0b11110000,
0b00001111, 0b11110000,
0b00011111, 0b11100000,
0b00011111, 0b11100000,
0b00111111, 0b11000000,
0b00111111, 0b11000000,
0b01111111, 0b10000000,
0b01111111, 0b10000000,
0b11111111, 0b00000000,
0b11111111, 0b00000000,
0b01111110, 0b00000000};
static const unsigned char PROGMEM num8[] =
{ 0b01111111, 0b11111110,
0b11111111, 0b11111111,
0b11111111, 0b11111111,
0b11111110, 0b01111111,
0b11111110, 0b01111111,
0b11111110, 0b01111111,
0b11111111, 0b11111111,
0b01111111, 0b11111110,
0b00111111, 0b11111100,
0b01111111, 0b11111110,
0b11111111, 0b11111111,
0b11111111, 0b11111111,
0b11111110, 0b01111111,
0b11111110, 0b01111111,
0b11111110, 0b01111111,
0b11111110, 0b01111111,
0b11111110, 0b01111111,
0b11111110, 0b01111111,
0b11111111, 0b11111111,
0b11111111, 0b11111111,
0b11111111, 0b11111111,
0b11111111, 0b11111111,
0b11111111, 0b11111111,
0b11111111, 0b11111111,
0b11111111, 0b11111111,
0b11111111, 0b11111111,
0b01111111, 0b11111110};
static const unsigned char PROGMEM num9[] =
{ 0b01111111, 0b11111110,
0b11111111, 0b11111111,
0b11111111, 0b11111111,
0b11111100, 0b00111111,
0b11111100, 0b00111111,
0b11111100, 0b00111111,
0b11111111, 0b10111111,
0b11111111, 0b11111111,
0b01111111, 0b11111111,
0b00000000, 0b11111111,
0b00000001, 0b11111110,
0b00000001, 0b11111110,
0b00000011, 0b11111100,
0b00000011, 0b11111100,
0b00000111, 0b11111000,
0b00000111, 0b11111000,
0b00001111, 0b11110000,
0b00001111, 0b11110000,
0b00011111, 0b11100000,
0b00011111, 0b11100000,
0b00111111, 0b11000000,
0b00111111, 0b11000000,
0b01111111, 0b10000000,
0b01111111, 0b10000000,
0b11111111, 0b00000000,
0b11111111, 0b00000000,
0b01111110, 0b00000000};
void setup() {
Serial.begin(9600);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
// Show initial display buffer contents on the screen --
// the library initializes this with an Adafruit splash screen.
//display.display();
//delay(2000); // Pause for 2 seconds
// Clear the buffer
//display.clearDisplay();
// Draw a single pixel in white
//display.drawPixel(10, 10, SSD1306_WHITE);
// Show the display buffer on the screen. You MUST call display() after
// drawing commands to make them visible on screen!
//display.display();
//delay(2000);
// display.display() is NOT necessary after every single drawing command,
// unless that's what you want...rather, you can batch up a bunch of
// drawing operations and then update the screen all at once by calling
// display.display(). These examples demonstrate both approaches...
display.invertDisplay(true);
delay(250);
display.invertDisplay(false);
delay(250);
testfillRect(); // Draw rounded rectangles (filled)
}
void loop() {
}
void testfillRect(void) {
display.clearDisplay();
int16_t boxwidth=100;
int16_t groworshrink=1;
int8_t metric=0;
long distance=0;
int spd[3]={0,0,0};
int y;
//letsbreak up the digits
for(int16_t i=0; i<20000; i+1) {
// The INVERSE color is used so round-rects alternate white/black
display.clearDisplay();
display.fillRect(1, 1, boxwidth, 10, //sortof speed bar graph, currently being used to feed speedometer display.
SSD1306_INVERSE);
if (groworshrink==0){
boxwidth=boxwidth + 1;
}
if (groworshrink==1){
boxwidth=boxwidth - 1;
}
if (boxwidth >=125) {
groworshrink=1;
}
if (boxwidth <=5) {
groworshrink=0;
}
//Speedometer display to three digits
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,15);
spd[2]=0;
int counter1 = 0;
int x = boxwidth;
while (x>0)
{
y=x/10;
spd[counter1]=x-(10*y);
x=y;
counter1++;
}
if (boxwidth>=100){ BigDigitDisplay(spd[2],66);} //First digit of the speedometer, if speeds at 100 or above
if (boxwidth>=10){ BigDigitDisplay(spd[1],83);} //Second digit of the speedometer, if speeds at 10 or above
BigDigitDisplay(spd[0],100); //Third digit of the speedometer, always display, even if 0
//mph or kph indicator
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(110,35);
// if (metric == 1){
// display.print("KPH");
// }
// else if (metric == 0){
// display.print("MPH");
// }
//odometer display
ODOMETER(distance); //sends the distance to the odometer display
if (metric == 1){
display.print("K");
}
else if (metric == 0){
display.print("M");
}
distance=(distance + boxwidth*10);//updates the distance variable
if(distance>999999){
distance=0;
}
// delay(100); //slows down the updates of the speedometer
display.display();
}
}
void ODOMETER (long distance)
{
display.setCursor(75,50);
if(distance>999999){
display.print(distance);
}
else if (distance>99999){
display.print(distance);
}
else if (distance>9999){
display.print("0");
display.print(distance);
}
else if (distance>999){
display.print("00");
display.print(distance);
}
else if (distance>99){
display.print("000");
display.print(distance);
}
else if (distance>9){
display.print("0000");
display.print(distance);
}
else if (distance<10){
display.print("00000");
display.print(distance);
}
}
void BigDigitDisplay (int x, int pos){
//Draws the big digits for the speedometer display, at the position of each number
switch (x) {
case 0:
display.drawBitmap(pos,16,num0,16,27,SSD1306_WHITE);
break;
case 1:
display.drawBitmap(pos,16,num1,16,27,SSD1306_WHITE);
break;
case 2:
display.drawBitmap(pos,16,num2,16,27,SSD1306_WHITE);
break;
case 3:
display.drawBitmap(pos,16,num3,16,27,SSD1306_WHITE);
break;
case 4:
display.drawBitmap(pos,16,num4,16,27,SSD1306_WHITE);
break;
case 5:
display.drawBitmap(pos,16,num5,16,27,SSD1306_WHITE);
break;
case 6:
display.drawBitmap(pos,16,num6,16,27,SSD1306_WHITE);
break;
case 7:
display.drawBitmap(pos,16,num7,16,27,SSD1306_WHITE);
break;
case 8:
display.drawBitmap(pos,16,num8,16,27,SSD1306_WHITE);
break;
case 9:
display.drawBitmap(pos,16,num9,16,27,SSD1306_WHITE);
break;
default:
break;
}
}