Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // For a connection via I2C using the Arduino Wire include:
- #include <Wire.h> // Only needed for Arduino 1.6.5 and earlier
- #include "SSD1306Wire.h" // legacy: #include "SSD1306.h"
- // I2C OLED Display works with SSD1306 driver
- #define OLED_SDA 5
- #define OLED_SCL 4
- #define LED 16
- //#define OLED_RST 16
- // Initialize the OLED display using Arduino Wire:
- SSD1306Wire display(0x3C, OLED_SDA, OLED_SCL); // ADDRESS, SDA, SCL - SDA and SCL usually populate automatically based on your board's pins_arduino.h e.g. https://github.com/esp8266/Arduino/blob/master/variants/nodemcu/pins_arduino.h
- #define DEMO_DURATION 3000
- typedef void (*Demo)(void);
- int demoMode = 0;
- int counter = 1;
- void setup() {
- Serial.begin(115200);
- Serial.println();
- Serial.println();
- pinMode(LED, OUTPUT);
- blinky();
- // Initialising the UI will init the display too.
- display.init();
- display.flipScreenVertically();
- display.setFont(ArialMT_Plain_10);
- }
- void blinky() {
- for (int i = 0; i < 5; i++) {
- digitalWrite(16, HIGH); // turn the LED on (HIGH is the voltage level)
- delay(300); // wait for a second
- digitalWrite(16, LOW); // turn the LED off by making the voltage LOW
- delay(500);
- }
- }
- void drawFontFaceDemo() {
- // Font Demo1
- // create more fonts at http://oleddisplay.squix.ch/
- display.setTextAlignment(TEXT_ALIGN_LEFT);
- display.setFont(ArialMT_Plain_24);
- display.drawString(0, 0, "HwThinker");
- display.setFont(ArialMT_Plain_16);
- display.drawString(0, 26, "in Action");
- display.setFont(ArialMT_Plain_10);
- display.drawString(0, 45, "Nature");
- }
- void drawTextFlowDemo() {
- display.setFont(ArialMT_Plain_10);
- display.setTextAlignment(TEXT_ALIGN_LEFT);
- display.drawStringMaxWidth(0, 0, 128,
- "Lorem ipsum\n dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore." );
- }
- void drawTextAlignmentDemo() {
- // Text alignment demo
- display.setFont(ArialMT_Plain_10);
- // The coordinates define the left starting point of the text
- display.setTextAlignment(TEXT_ALIGN_LEFT);
- display.drawString(0, 10, "Left aligned (0,10)");
- // The coordinates define the center of the text
- display.setTextAlignment(TEXT_ALIGN_CENTER);
- display.drawString(64, 22, "Center aligned (64,22)");
- // The coordinates define the right end of the text
- display.setTextAlignment(TEXT_ALIGN_RIGHT);
- display.drawString(128, 33, "Right aligned (128,33)");
- }
- void drawRectDemo() {
- // Draw a pixel at given position
- for (int i = 0; i < 10; i++) {
- display.setPixel(i, i);
- display.setPixel(10 - i, i);
- }
- display.drawRect(12, 12, 20, 20);
- // Fill the rectangle
- display.fillRect(14, 14, 17, 17);
- // Draw a line horizontally
- display.drawHorizontalLine(0, 40, 20);
- // Draw a line horizontally
- display.drawVerticalLine(40, 0, 20);
- }
- void drawCircleDemo() {
- for (int i = 1; i < 8; i++) {
- display.setColor(WHITE);
- display.drawCircle(32, 32, i * 3);
- if (i % 2 == 0) {
- display.setColor(BLACK);
- }
- display.fillCircle(96, 32, 32 - i * 3);
- }
- }
- void drawProgressBarDemo() {
- int progress = (counter / 5) % 100;
- // draw the progress bar
- display.drawProgressBar(0, 32, 120, 10, progress);
- // draw the percentage as String
- display.setTextAlignment(TEXT_ALIGN_CENTER);
- display.drawString(64, 15, String(progress) + "%");
- }
- Demo demos[] = {drawFontFaceDemo, drawTextFlowDemo, drawTextAlignmentDemo, drawRectDemo, drawCircleDemo, drawProgressBarDemo};
- int demoLength = (sizeof(demos) / sizeof(Demo));
- long timeSinceLastModeSwitch = 0;
- void loop() {
- // clear the display
- display.clear();
- // draw the current demo method
- demos[demoMode]();
- display.setFont(ArialMT_Plain_10);
- display.setTextAlignment(TEXT_ALIGN_RIGHT);
- display.drawString(128, 54, String(millis()));
- // write the buffer to the display
- display.display();
- if (millis() - timeSinceLastModeSwitch > DEMO_DURATION) {
- demoMode = (demoMode + 1) % demoLength;
- timeSinceLastModeSwitch = millis();
- }
- counter++;
- delay(10);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement