#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)
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
//Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
Adafruit_SSD1306 display(OLED_RESET);
#define charw 16
#define charh 16
static const unsigned char PROGMEM ju[]={0x00,0x00,0x7D,0xFE,0x45,0x00,0x45,0x00,0x45,0x00,0x7D,0xFC,0x11,0x04,0x11,0x04,
0x5D,0x04,0x51,0x04,0x51,0xFC,0x51,0x00,0x5D,0x00,0xE1,0x00,0x01,0xFE,0x00,0x00};
static const unsigned char PROGMEM li[]={0x02,0x00,0x01,0x00,0xFF,0xFE,0x00,0x00,0x14,0x50,0x13,0x90,0x14,0x50,0x1F,0xF0,
0x01,0x00,0x7F,0xFC,0x42,0x04,0x44,0x44,0x4F,0xE4,0x44,0x24,0x40,0x14,0x40,0x08};
void setup() {
Serial.begin(9600);
initChaoShengBo();
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
// Serial.println(F("SSD1306 allocation failed"));
for (;;); // Don't proceed, loop forever
}
}
#define Echo 6
#define Trig 5
long getTime(){
digitalWrite(Trig, HIGH);
delayMicroseconds(10);
digitalWrite(Trig, LOW);
return pulseIn(Echo,HIGH);
}
void initChaoShengBo(){
pinMode(Echo, INPUT);
pinMode(Trig, OUTPUT);
}
void loop()
{
display.clearDisplay(); //清屏—清除缓存
display.drawBitmap(0,16,ju,charw,charh,WHITE);
display.drawBitmap(64,16,li,charw,charh,WHITE);
float dis;
dis=getTime()/58;
/*
Serial.print(dis);
Serial.println("cm");
delay(50);
*/
}
Loading
ssd1306
ssd1306