OLED Display

From Public Wiki
Jump to navigation Jump to search

Synopsis

Modified Demo of SSD1306

Code

  1 //Arduino UNO
  2 //SSD1306
  3 ////Pin A4 SDA
  4 ////Pin A5 SCL
  5 #include <SPI.h>
  6 #include <Wire.h>
  7 #include <Adafruit_GFX.h>
  8 #include <Adafruit_SSD1306.h>
  9 
 10 #define SCREEN_WIDTH 128 // OLED display width, in pixels
 11 #define SCREEN_HEIGHT 64 // OLED display height, in pixels
 12 
 13 // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
 14 #define OLED_RESET     4 // Reset pin # (or -1 if sharing Arduino reset pin)
 15 Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
 16 
 17 void setup()
 18 {
 19   Serial.begin(115200);
 20 
 21   // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
 22   if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C))
 23   { // Address 0x3D for 128x64
 24     Serial.println(F("SSD1306 allocation failed"));
 25     for(;;); // Don't proceed, loop forever
 26   }
 27   display.clearDisplay();
 28 
 29 
 30   testdrawchar();      // Draw characters of the default font
 31 
 32 //  testdrawstyles();    // Draw 'stylized' characters
 33 
 34 //  testscrolltext();    // Draw scrolling text
 35 
 36   // Invert and restore display, pausing in-between
 37   display.invertDisplay(true);
 38   delay(1000);
 39   display.invertDisplay(false);
 40   delay(1000);
 41 
 42 }
 43 
 44 
 45 void loop() {
 46 }
 47 
 48 
 49 void testdrawchar(void) {
 50   display.clearDisplay();
 51 
 52   display.setTextSize(1);      // Normal 1:1 pixel scale
 53   display.setTextColor(SSD1306_WHITE); // Draw white text
 54   display.setCursor(0, 0);     // Start at top-left corner
 55   display.cp437(true);         // Use full 256 char 'Code Page 437' font
 56 
 57 while(1){
 58   display.setCursor(0,0);             // Start at top-left corner
 59   display.println(F("    CoolShit (TM)    "));
 60   display.println(F("   Battery Monitor   "));
 61   display.write(201);
 62 for(int i=0;i<19;i++)
 63   display.write(205);
 64   display.write(187);
 65   display.println(F(""));
 66   display.write(186);
 67   display.print(F(" A1=3.176 A2=3.175 "));
 68   display.write(186);
 69   display.println(F(""));
 70   display.write(186);
 71   display.print(F(" A3=3.176 A4=3.176 "));
 72   display.write(186);
 73   display.println(F(""));
 74   display.write(186);
 75   display.print(F(" A5=3.177 A6=3.177 "));
 76   display.write(186);
 77   display.println(F(""));
 78   display.write(186);
 79   display.print(F(" A7=3.177 A8=3.178 "));
 80   display.write(186);
 81   display.println(F(""));
 82   display.write(200);
 83 for(int i=0;i<19;i++)
 84   display.write(205);
 85   display.write(188);
 86 
 87   display.display();
 88   delay(4000);
 89   display.invertDisplay(true);
 90   delay(500);
 91   display.invertDisplay(false);
 92   display.clearDisplay();
 93   delay(500);
 94 
 95   display.setCursor(0,0);             // Start at top-left corner
 96   display.println(F("    CoolShit (TM)    "));
 97   display.println(F("   Battery Monitor   "));
 98   display.write(201);
 99 for(int i=0;i<19;i++)
100   display.write(205);
101   display.write(187);
102   display.println(F(""));
103   display.write(186);
104   display.print(F(" B1=3.231 B2=3.232 "));
105   display.write(186);
106   display.println(F(""));
107   display.write(186);
108   display.print(F(" B3=3.231 B4=3.222 "));
109   display.write(186);
110   display.println(F(""));
111   display.write(186);
112   display.print(F(" B5=3.222 B6=3.223 "));
113   display.write(186);
114   display.println(F(""));
115   display.write(186);
116   display.print(F(" B7=3.234 B8=3.245 "));
117   display.write(186);
118   display.println(F(""));
119   display.write(200);
120 for(int i=0;i<19;i++)
121   display.write(205);
122   display.write(188);
123 
124 
125   display.display();
126   delay(4000);
127   display.invertDisplay(true);
128   delay(500);
129   display.invertDisplay(false);
130   display.clearDisplay();
131   delay(500);
132 
133 
134   display.setCursor(0,0);             // Start at top-left corner
135   display.println(F("    CoolShit (TM)    "));
136   display.println(F("   Battery Monitor   "));
137   display.write(201);
138 for(int i=0;i<19;i++)
139   display.write(205);
140   display.write(187);
141   display.println(F(""));
142   display.write(186);
143   display.print(F(" C1=3.166 C2=3.165 "));
144   display.write(186);
145   display.println(F(""));
146   display.write(186);
147   display.print(F(" C3=3.165 C4=3.165 "));
148   display.write(186);
149   display.println(F(""));
150   display.write(186);
151   display.print(F(" C5=3.164 C6=3.153 "));
152   display.write(186);
153   display.println(F(""));
154   display.write(186);
155   display.print(F(" C7=3.164 C8=3.163 "));
156   display.write(186);
157   display.println(F(""));
158   display.write(200);
159 for(int i=0;i<19;i++)
160   display.write(205);
161   display.write(188);
162 
163 
164   display.display();
165   delay(4000);
166   display.invertDisplay(true);
167   delay(500);
168   display.invertDisplay(false);
169   display.clearDisplay();
170   delay(500);
171 
172 
173   display.setCursor(0,0);             // Start at top-left corner
174   display.println(F("    CoolShit (TM)    "));
175   display.println(F("   Battery Monitor   "));
176   display.write(201);
177 for(int i=0;i<19;i++)
178   display.write(205);
179   display.write(187);
180   display.println(F(""));
181   display.write(186);
182   display.print(F(" D1=3.185 D2=3.176 "));
183   display.write(186);
184   display.println(F(""));
185   display.write(186);
186   display.print(F(" D3=3.176 D4=3.175 "));
187   display.write(186);
188   display.println(F(""));
189   display.write(186);
190   display.print(F(" D5=3.174 D6=3.173 "));
191   display.write(186);
192   display.println(F(""));
193   display.write(186);
194   display.print(F(" D7=3.183 D8=3.188 "));
195   display.write(186);
196   display.println(F(""));
197   display.write(200);
198 for(int i=0;i<19;i++)
199   display.write(205);
200   display.write(188);
201 
202 
203   display.display();
204   delay(4000);
205   display.invertDisplay(true);
206   delay(500);
207   display.invertDisplay(false);
208   display.clearDisplay();
209   delay(500);
210 
211 
212   display.setCursor(0,0);             // Start at top-left corner
213   display.println(F("    CoolShit (TM)    "));
214   display.println(F("   Battery Monitor   "));
215   display.write(201);
216 for(int i=0;i<19;i++)
217   display.write(205);
218   display.write(187);
219   display.println(F(""));
220   display.write(186);
221   display.print(F(" E1=3.222 E2=3.221 "));
222   display.write(186);
223   display.println(F(""));
224   display.write(186);
225   display.print(F(" E3=3.224 E4=3.214 "));
226   display.write(186);
227   display.println(F(""));
228   display.write(186);
229   display.print(F(" E5=3.223 E6=3.224 "));
230   display.write(186);
231   display.println(F(""));
232   display.write(186);
233   display.print(F(" E7=3.233 E8=3.223 "));
234   display.write(186);
235   display.println(F(""));
236   display.write(200);
237 for(int i=0;i<19;i++)
238   display.write(205);
239   display.write(188);
240 
241 
242   display.display();
243   delay(4000);
244   display.invertDisplay(true);
245   delay(500);
246   display.invertDisplay(false);
247   display.clearDisplay();
248   delay(500);
249 
250 
251 }
252 }