DS18b20獲取到溫度數值保存到變量中,而後和天氣圖標還有滾動字幕一塊兒發送到OLED 屏幕上顯示c++
須要用到的庫都可在Arduino庫管理器下載。
電路圖:
函數
圖中屏幕接線已在代碼中寫出,溫度傳感器date口須要接一個4.7k歐的電阻;
最終效果以下圖:
oop
代碼以下:ui
#include <Arduino.h> #include <U8g2lib.h> #include <OneWire.h> #include <DallasTemperature.h> // DS18b20數據輸出腳接開發板數字引腳 #define ONE_WIRE_BUS 8 OneWire oneWire(ONE_WIRE_BUS); DallasTemperature sensors(&oneWire); #ifdef U8X8_HAVE_HW_SPI #include <SPI.h> #endif #ifdef U8X8_HAVE_HW_I2C #include <Wire.h> #endif //選擇本身的屏幕驅動 U8G2_SSD1306_128X64_NONAME_F_4W_SW_SPI u8g2(U8G2_R0, /* clock=*/ 4, /* data=*/ 5, /* cs=*/ 3, /* dc=*/ 7, /* reset=*/ 6); #define SUN_CLOUD 1 //繪製天氣圖標 void drawWeatherSymbol(u8g2_uint_t x, u8g2_uint_t y, uint8_t symbol) { switch(symbol) { case SUN_CLOUD: u8g2.setFont(u8g2_font_open_iconic_weather_6x_t); u8g2.drawGlyph(x, y, 65); break; } } //繪製顯示界面 void drawWeather(uint8_t symbol, int val) { //繪製天氣符號 drawWeatherSymbol(0, 48, symbol); //繪製溫度 u8g2.setFont(u8g2_font_logisoso32_tf); u8g2.setCursor(48+3, 42); u8g2.print(val); u8g2.print("°C"); // requires enableUTF8Print() } //繪製滾動字幕 void drawScrollString(int16_t offset, const char *s) { static char buf[36]; // should for screen with up to 256 pixel width size_t len; size_t char_offset = 0; u8g2_uint_t dx = 0; size_t visible = 0; u8g2.setDrawColor(0); // clear the scrolling area u8g2.drawBox(0, 49, u8g2.getDisplayWidth()-1, u8g2.getDisplayHeight()-1); u8g2.setDrawColor(1); // set the color for the text len = strlen(s); if ( offset < 0 ) { char_offset = (-offset)/8; dx = offset + char_offset*8; if ( char_offset >= u8g2.getDisplayWidth()/8 ) return; visible = u8g2.getDisplayWidth()/8-char_offset+1; strncpy(buf, s, visible); buf[visible] = '\0'; u8g2.setFont(u8g2_font_8x13_mf); u8g2.drawStr(char_offset*8-dx, 62, buf); } else { char_offset = offset / 8; if ( char_offset >= len ) return; // nothing visible dx = offset - char_offset*8; visible = len - char_offset; if ( visible > u8g2.getDisplayWidth()/8+1 ) visible = u8g2.getDisplayWidth()/8+1; strncpy(buf, s+char_offset, visible); buf[visible] = '\0'; u8g2.setFont(u8g2_font_8x13_mf); u8g2.drawStr(-dx, 62, buf); } } void draw(const char *s, uint8_t symbol, int val) { int16_t offset = -(int16_t)u8g2.getDisplayWidth(); int16_t len = strlen(s); u8g2.clearBuffer(); // clear the internal memory drawWeather(symbol, val); // draw the icon and degree only once for(;;) // then do the scrolling { drawScrollString(offset, s); // no clearBuffer required, screen will be partially cleared here u8g2.sendBuffer(); // transfer internal memory to the display delay(20); offset+=2; if ( offset > len*8+1 ) break; } } void setup(void) { Serial.begin(9600); sensors.begin(); //初始化DS18b20 u8g2.begin(); //初始化OLED u8g2.enableUTF8Print();//打開UTF8輸出 } void loop(void) { sensors.requestTemperatures(); // 發送命令獲取溫度 int val = sensors.getTempCByIndex(0);//將獲取到的溫度數值保存在變量中 draw("What a beautiful day!", SUN_CLOUD, val);//調用函數繪製最終界面 }