DS18b20获取到温度数值保存到变量中,然后和天气图标还有滚动字幕一起发送到OLED 屏幕上显示

需要用到的库均可在Arduino库管理器下载。

电路图:

图中屏幕接线已在代码中写出,温度传感器date口需要接一个4.7k欧的电阻;

最终效果如下图:

代码如下:

#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);//调用函数绘制最终界面
}

最新文章

  1. SQL Server:字符串函数
  2. 最稳定 性能最好 的 Linux 版本?
  3. 【tornado】系列项目(一)之基于领域驱动模型架构设计的京东用户管理后台
  4. AngularJS Best Practices: Directory Structure
  5. AngularJS 表单提交后显示验证信息与失焦后显示验证信息
  6. ID和Name的区别
  7. 智能指针(一):STL auto_ptr实现原理
  8. CCAN:C语言的模块仓库
  9. filter的两种使用方法
  10. 自我介绍&amp;软工实践博客点评
  11. Windows下安装Anaconda
  12. [Postman]捕获HTTP请求(14)
  13. MySQL 查询语句中自己定义的中文内容在Java Web 中显示为问号
  14. Nginx+Keeplived双机热备(主从模式)
  15. 浅尝 Vue 中的 computed 属性 与 watch
  16. odoo11新开发功能模块测试指南
  17. 第三个spring冲刺第2天
  18. RSA 加密算法 Java 公钥加密私钥解密 和 私钥加密公钥解密 的特点
  19. 洛谷P4069 [SDOI2016]游戏(李超线段树)
  20. 你可能不知道的shell、bash二三事(Centos 7)

热门文章

  1. FreeBSD 12.2 vmware 虚拟机镜像 bt 种子
  2. 攻防世界 reverse 进阶 9-re1-100
  3. NetCore的缓存使用详例
  4. 配置Java环境变量时的一个常见错误
  5. ionic3 清除navpush的堆栈 (android真机返回键bug)
  6. [Fundamental of Power Electronics]-PART I-5.不连续导电模式-5.3 Boost变换器实例
  7. 【MQ中间件】RabbitMQ -- SpringBoot整合RabbitMQ(3)
  8. Spring Boot 接口幂等插件使用
  9. 如何写好一个 Spring 组件
  10. 22. VUE 插槽-详解