/************************************************************************************************* PROGRAMMINFO ************************************************************************************************** Funktion: Analogwertanzeige auf OLED 0,96" mit Poti Poti A3 Shield=IO34 Bei 3,3V Anzeige nur bis 90MHP! ************************************************************************************************** Version: 226.12.022 ************************************************************************************************** Board: UNO OLED 1.3" oder 0,96" ************************************************************************************************** Libraries: https://github.com/espressif/arduino-esp32/tree/master/libraries C:\Users\User\Documents\Arduino D:\gittemp\Arduino II\A156_Wetterdaten_V3 ************************************************************************************************** C++ Arduino IDE V1.8.19 ************************************************************************************************** Einstellungen: https://dl.espressif.com/dl/package_esp32_index.json http://dan.drown.org/stm32duino/package_STM32duino_index.json http://arduino.esp8266.com/stable/package_esp8266com_index.json **************************************************************************************************/ #include "U8glib.h" U8GLIB_SH1106_128X64 u8g(U8G_I2C_OPT_DEV_0 | U8G_I2C_OPT_NO_ACK | U8G_I2C_OPT_FAST); //OLED 1,3" //U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_DEV_0 | U8G_I2C_OPT_NO_ACK | U8G_I2C_OPT_FAST); // OLED 0,96" int xmax=128; // max length x-axis int ymax=62; // max length y-axis int xcenter=xmax/2; // center of x-axis int ycenter=ymax/2+10; // center of y-axis int arc=ymax/2; int angle=0; char* label[] = {"LOAD","Temperatur","INTAKE", "VOLT"}; // some custom gauge labels int labelXpos[] = {53, 39, 49, 53}; // predefined x-position of a gauge label #define potmeterPin A3 // simulate analogue value with potentiometer int p, w, m; u8g_uint_t xx=0; // ------------------------------------------------- void gauge() ------------------------------------------ void gauge(uint8_t angle) { // draw border of the gauge u8g.drawCircle(xcenter,ycenter,arc+6, U8G_DRAW_UPPER_RIGHT); u8g.drawCircle(xcenter,ycenter,arc+4, U8G_DRAW_UPPER_RIGHT); u8g.drawCircle(xcenter,ycenter,arc+6, U8G_DRAW_UPPER_LEFT); u8g.drawCircle(xcenter,ycenter,arc+4, U8G_DRAW_UPPER_LEFT); // draw the needle float x1=sin(2*angle*2*3.14/360); // needle position float y1=cos(2*angle*2*3.14/360); u8g.drawLine(xcenter, ycenter, xcenter+arc*x1, ycenter-arc*y1); u8g.drawDisc(xcenter, ycenter, 5, U8G_DRAW_UPPER_LEFT); u8g.drawDisc(xcenter, ycenter, 5, U8G_DRAW_UPPER_RIGHT); u8g.setFont(u8g_font_chikita); // show scale labels u8g.drawStr( 20, 42, "0"); u8g.drawStr( 25, 18, "15"); u8g.drawStr( 60, 14, "30"); u8g.drawStr( 95, 18, "45"); u8g.drawStr( 105, 42, "60"); // show gauge label u8g.setPrintPos(labelXpos[1],32); u8g.print(label[1]); // show digital value and align its position u8g.setFont(u8g_font_profont22); u8g.setPrintPos(54,60); if (w<10){ // leading 0 when value less than 10 u8g.print("0"); } if (w>99) { // position at 100% u8g.setPrintPos(47,60); } u8g.print(w); } // ------------------------------------------------- void setup() ------------------------------------------ void setup(void) { pinMode(potmeterPin, INPUT); u8g.setFont(u8g_font_chikita); u8g.setColorIndex(1); // Instructs the display to draw with a pixel on. // assign default color value if ( u8g.getMode() == U8G_MODE_R3G3B2 ) { u8g.setColorIndex(255); // white } else if ( u8g.getMode() == U8G_MODE_GRAY2BIT ) { u8g.setColorIndex(3); // max intensity } else if ( u8g.getMode() == U8G_MODE_BW ) { u8g.setColorIndex(1); // pixel on } else if ( u8g.getMode() == U8G_MODE_HICOLOR ) { u8g.setHiColorByRGB(255,255,255); } } // ------------------------------------------------- void loop() ------------------------------------------ void loop(void) { p = analogRead(potmeterPin); // get value from potmeter w = map(p,0,1023,0,60); // map it between 0 and 100 m = map(p,0,1023,0,90); // map needle movement // show needle and dial xx = m; // 135 = zero position, 180 = just before middle, 0 = middle, 45 = max if (xx<45){ // positie correctie xx=xx+135; } else { xx=xx-45; } // picture loop { u8g.firstPage(); do { gauge(xx); } while( u8g.nextPage() ); } }