Created
December 16, 2018 02:41
-
-
Save pinglunliao/0e04d3d66d01ea2ca38ef3110968ae6a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <NewPing.h> | |
#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); | |
#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[] = | |
{ B00000000, B11000000, | |
B00000001, B11000000, | |
B00000001, B11000000, | |
B00000011, B11100000, | |
B11110011, B11100000, | |
B11111110, B11111000, | |
B01111110, B11111111, | |
B00110011, B10011111, | |
B00011111, B11111100, | |
B00001101, B01110000, | |
B00011011, B10100000, | |
B00111111, B11100000, | |
B00111111, B11110000, | |
B01111100, B11110000, | |
B01110000, B01110000, | |
B00000000, B00110000 }; | |
#define PING_PIN 12 // Arduino pin for both trig and echo | |
NewPing sonar(PING_PIN, PING_PIN); | |
void setup() { | |
Serial.begin(9600); | |
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); | |
display.clearDisplay(); | |
display.setTextColor(WHITE); | |
display.setCursor(0,30); | |
display.setTextSize(4); | |
display.println("Hello World!"); | |
display.display(); | |
} | |
void loop() { | |
delay(500); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay | |
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS). | |
Serial.print("Ping: "); | |
unsigned int distanceCM = uS / US_ROUNDTRIP_CM; | |
Serial.print(distanceCM); // convert time into distance | |
Serial.println("cm"); | |
display.clearDisplay(); | |
display.setTextColor(WHITE); | |
display.setCursor(0,30); | |
display.print(distanceCM); | |
display.println("cm"); | |
display.display(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment