Das OLED Display, dass man überall verbauen kann. Es ist klein aber dennoch gut zu lesen. Diese Displays gibt es in den unterschiedlichsten Farben und Schnittstellen. Ich habe mit hier für die SPI Variante entschieden. Das Display U8G2_SSD1306_128X64 bekommt man bei eBay für einen schmalen Taler 🙂
Anbei ein kleiner Test mit Hilfe des Arduino Nanos.
Da ich mehre Displays testen wollte, habe ich noch eine weiter Schaltung aufgebaut.
Und zu guter letzt der Programmcode, es empfiehlt sich die Bibliothek U8g2lib von github zu laden.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
#include <Arduino.h> #include <U8g2lib.h> #include <SPI.h> #include <Wire.h> #include <SoftwareSerial.h> //U8G2_SSD1306_128X64_NONAME_F_4W_SW_SPI(rotation, clock, data, cs, dc [, reset]) //U8G2_SSD1306_128X64_NONAME_F_4W_SW_SPI u8g2(U8G2_R0, 2, 3, 9, 8, 6); U8G2_SSD1306_128X64_NONAME_F_4W_SW_SPI u8g2(U8G2_R0, 12, 11, 8, 9, 10); // U8G2_SSD1306_128X64_NONAME_F_3W_SW_SPI u8g2(U8G2_R0, 3, 4, 7, 5); const int irSense = A0; // Connect sensor to analog pin A0 int distance = 0; typedef u8g2_uint_t u8g_uint_t; #define SECONDS 10 uint8_t flip_color = 0; uint8_t draw_color = 1; void draw_set_screen(void) { // graphic commands to redraw the complete screen should be placed here u8g2.setColorIndex(flip_color); u8g2.drawBox( 0, 0, u8g2.getWidth(), u8g2.getHeight() ); } void draw_clip_test(void) { u8g_uint_t i, j, k; char buf[3] = "AB"; k = 0; u8g2.setColorIndex(draw_color); u8g2.setFont(u8g2_font_6x10_tf); for( i = 0; i < 6; i++ ) { for( j = 1; j < 8; j++ ) { u8g2.drawHLine(i-3, k, j); u8g2.drawHLine(i-3+10, k, j); u8g2.drawVLine(k+20, i-3, j); u8g2.drawVLine(k+20, i-3+10, j); k++; } } u8g2.setFontDirection(0); u8g2.drawStr(0-3, 50, buf); u8g2.setFontDirection(2); u8g2.drawStr(0+3, 50, buf); u8g2.setFontDirection(0); u8g2.drawStr(u8g2.getWidth()-3, 40, buf); u8g2.setFontDirection(2); u8g2.drawStr(u8g2.getWidth()+3, 40, buf); u8g2.setFontDirection(1); u8g2.drawStr(u8g2.getWidth()-10, 0-3, buf); u8g2.setFontDirection(3); u8g2.drawStr(u8g2.getWidth()-10, 3, buf); u8g2.setFontDirection(1); u8g2.drawStr(u8g2.getWidth()-20, u8g2.getHeight()-3, buf); u8g2.setFontDirection(3); u8g2.drawStr(u8g2.getWidth()-20, u8g2.getHeight()+3, buf); u8g2.setFontDirection(0); } void draw_char(void) { char buf[2] = "@"; u8g_uint_t i, j; // graphic commands to redraw the complete screen should be placed here u8g2.setColorIndex(draw_color); u8g2.setFont(u8g2_font_6x10_tf); j = 8; for(;;) { i = 0; for(;;) { u8g2.drawStr( i, j, buf); i += 8; if ( i > u8g2.getWidth() ) break; } j += 8; if ( j > u8g2.getHeight() ) break; } } void draw_pixel(void) { u8g_uint_t x, y, w2, h2; u8g2.setColorIndex(draw_color); w2 = u8g2.getWidth(); h2 = u8g2.getHeight(); w2 /= 2; h2 /= 2; for( y = 0; y < h2; y++ ) { for( x = 0; x < w2; x++ ) { if ( (x + y) & 1 ) { u8g2.drawPixel(x,y); u8g2.drawPixel(x,y+h2); u8g2.drawPixel(x+w2,y); u8g2.drawPixel(x+w2,y+h2); } } } } // returns unadjusted FPS uint16_t picture_loop_with_fps(void (*draw_fn)(void)) { uint16_t FPS10 = 0; uint32_t time; time = millis() + SECONDS*1000; // picture loop do { u8g2.firstPage(); do { draw_fn(); } while( u8g2.nextPage() ); FPS10++; flip_color = flip_color ^ 1; } while( millis() < time ); return FPS10; } const char *convert_FPS(uint16_t fps) { static char buf[6]; strcpy(buf, u8g2_u8toa( (uint8_t)(fps/10), 3)); buf[3] = '.'; buf[4] = (fps % 10) + '0'; buf[5] = '\0'; return buf; } void show_result(const char *s, uint16_t fps) { // assign default color value u8g2.setColorIndex(draw_color); u8g2.setFont(u8g2_font_8x13B_tf); u8g2.firstPage(); do { u8g2.drawStr(0,12, s); u8g2.drawStr(0,24, convert_FPS(fps)); } while( u8g2.nextPage() ); } void setup(void) { /* U8g2 Project: SSD1306 Test Board */ //pinMode(10, OUTPUT); //pinMode(9, OUTPUT); //digitalWrite(10, 0); //digitalWrite(9, 0); /* U8g2 Project: T6963 Test Board */ //pinMode(18, OUTPUT); //digitalWrite(18, 1); /* U8g2 Project: KS0108 Test Board */ //pinMode(16, OUTPUT); //digitalWrite(16, 0); /* U8g2 Project: LC7981 Test Board, connect RW to GND */ //pinMode(17, OUTPUT); //digitalWrite(17, 0); Serial.begin(9600); u8g2.begin(); // flip screen, if required // u8g2.setRot180(); // assign default color value draw_color = 1; // pixel on } void loop(void) { Serial.println("Hello"); uint16_t fps; fps = picture_loop_with_fps(draw_clip_test); show_result("draw clip test", fps); delay(5000); fps = picture_loop_with_fps(draw_set_screen); show_result("clear screen", fps); delay(5000); fps = picture_loop_with_fps(draw_char); show_result("draw @", fps); delay(5000); fps = picture_loop_with_fps(draw_pixel); show_result("draw pixel", fps); distance = analogRead(irSense); // Read the sensor Serial.println(distance, DEC); // Display value in Serial Monitor window delay(5000); } |