Advertisement
Guest User

SSD1306 OLED 0.96" Display Driver Example (Arduino)

a guest
Apr 28th, 2017
400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.95 KB | None | 0 0
  1. /*
  2.   OLED 0.96" display test code for Arduino
  3.   Libraries used:
  4.     SPI
  5.     TWI/I2C (for temperature sensor)
  6.    
  7.   Hardware:
  8.     LM35 temperature sensor, connected to pin A0
  9.     Momentary push button connected to pin D2
  10.  
  11.   The debounce routine works but it can miss sometimes. I recommend
  12.   using the Bounce2 library for a foolproof debouncing routine.
  13. */
  14. #include <SPI.h>
  15. #include <Wire.h>
  16. #include <Adafruit_GFX.h>
  17. #include <Adafruit_SSD1306.h>
  18.  
  19. #define TEMP_PIN 0  // Pin A0 for LM35
  20. #define TEMP_CONVERSION 0.48828125
  21. #define TEMP_F(X) (X*9)/5 + 32
  22. #define OLED_RESET 4  // Reset line for OLED display
  23. #define BUTTON_PIN 2  // Pin D2 for button
  24.  
  25. float tempC = 0.00;   // Temperature value
  26. byte dispMode = 0;    // Current display mode
  27. int buttonState;         // variable for reading the pushbutton status
  28. int lastButtonState = LOW;
  29. unsigned int flipDisplay = 0;
  30.  
  31. // the following variables are long's because the time, measured in miliseconds,
  32. // will quickly become a bigger number than can be stored in an int.
  33. unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
  34. const long PROGMEM debounceDelay = 50;    // the debounce time; increase if the output flickers
  35.  
  36. unsigned long lastReadTime = 0;
  37. const long PROGMEM readDelay = 2000;    // Read delay amount for sensors
  38.  
  39. Adafruit_SSD1306 display(OLED_RESET);
  40.  
  41. void setup() {
  42.   dispMode = 0;
  43.   pinMode(BUTTON_PIN, INPUT);
  44.   // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
  45.   display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3C (for the 128x64)
  46.   // init done
  47.  
  48.   // Initialize the display
  49.   display.clearDisplay();
  50.   display.setTextSize(1);
  51.   display.setTextColor(WHITE);
  52.  
  53.   display.display();
  54.  
  55.   tempC = analogRead(TEMP_PIN) * TEMP_CONVERSION;
  56. }
  57.  
  58. void loop() {
  59.   // Need to only do this but once every second or so
  60.   if ( (millis() - lastReadTime) > readDelay) {
  61.     lastReadTime = millis();
  62.     flipDisplay = !flipDisplay;
  63.     /* Take readings from sensors */
  64.     tempC = analogRead(TEMP_PIN) * TEMP_CONVERSION;
  65.   }
  66.   int reading = digitalRead(BUTTON_PIN);
  67.  
  68.   // If the switch changed, due to noise or pressing:
  69.   if (reading != lastButtonState) {
  70.     // reset the debouncing timer
  71.     lastDebounceTime = millis();
  72.   }
  73.  
  74.   if ( (millis() - lastDebounceTime) > debounceDelay) {
  75.       if ( reading != buttonState) {
  76.         buttonState = reading;
  77.          if ( buttonState == HIGH ) {
  78.              dispMode++;
  79.              if ( dispMode > 4 ) {
  80.                dispMode = 0;
  81.              }
  82.          }
  83.       }
  84.   }
  85.  
  86.   lastButtonState = reading;
  87.  
  88.   switch (dispMode) {
  89.     case 0:
  90.       drawMode0();
  91.       break;
  92.     case 1:
  93.       drawMode1();
  94.       break;
  95.     case 2:
  96.       drawMode2();
  97.       break;
  98.     case 3:
  99.       drawMode3();
  100.       break;
  101.     case 4:
  102.       drawMode4();
  103.       break;
  104.   }
  105.  
  106.   display.display();
  107.  
  108. }
  109.  
  110. void drawHeader(boolean drawBeacon) {
  111.   display.clearDisplay();
  112.   display.setTextSize(1);
  113.   display.setCursor(20,0);
  114.   display.setTextColor(WHITE);
  115.   display.println( F("USCSS Nostromo") );
  116.   if ( drawBeacon ) {
  117.     display.setTextColor(BLACK, WHITE); // 'inverted' text
  118.     display.setCursor(9,8);
  119.     display.println( F(" Beacon Detected ") );
  120.   }
  121. }
  122.  
  123. void drawTempC() {
  124.   display.print(tempC);
  125.   display.print(" ");
  126.   display.print((char)248);
  127.   display.println(F("C"));  
  128. }
  129.  
  130. void drawTempF() {
  131.   display.print(TEMP_F(tempC));
  132.   display.print(" ");
  133.   display.print((char)248);
  134.   display.println(F("F"));
  135. }
  136.  
  137. void drawMode0() {
  138.   drawHeader(false);
  139.   display.setTextSize(1);
  140.   display.setTextColor(WHITE);
  141.   display.setCursor(0,16);
  142.   drawTempC();
  143.   drawTempF();
  144.   display.println(F("Distance: 500m"));
  145.   display.print(F("Time: "));
  146.   display.print(millis()/1000);
  147.   display.print(F("s"));
  148.   //display.println("Signal Strength: LOW");
  149. }
  150.  
  151. /*
  152.   Big temperature in F
  153. */
  154. void drawMode1() {
  155.   drawHeader(false);
  156.   display.setTextSize(2);
  157.   display.setTextColor(WHITE);
  158.   display.setCursor(18, 32);
  159.   drawTempF();
  160. }
  161.  
  162. /*
  163.   Big temperature in C
  164. */
  165. void drawMode2() {
  166.   drawHeader(false);
  167.   display.setTextSize(2);
  168.   display.setTextColor(WHITE);
  169.   display.setCursor(18, 32);
  170.   drawTempC();
  171. }
  172.  
  173. /*
  174.   Distance
  175. */
  176. void drawMode3() {
  177.   drawHeader(false);
  178.   display.setTextSize(2);
  179.   display.setTextColor(WHITE);
  180.   display.setCursor(16, 16);
  181.   display.println(F("Distance"));
  182.   if ( flipDisplay )
  183.   display.setTextColor(BLACK, WHITE);
  184.   display.setCursor(24, 36);
  185.   display.print(F(" 500m "));
  186. }
  187.  
  188. /*
  189.   Display signal bars
  190. */
  191. void drawMode4() {
  192.   drawHeader(true);
  193.   display.setCursor(16,18);
  194.   display.setTextSize(1);
  195.   display.setTextColor(WHITE);
  196.   display.println(F("Signal Strength"));
  197.   display.fillRect(48, 40, 2, 8, WHITE);
  198.   display.fillRect(52, 40, 2, 8, WHITE);
  199.   display.fillRect(56, 36, 2, 12, WHITE);
  200.   display.fillRect(60, 32, 2, 16, WHITE);
  201.   display.drawFastHLine(48, 50, 14, WHITE);
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement