arduino sht4X+oled例程

//需要安装这三个库:

#include <Adafruit_GFX.h>

#include <Adafruit_SSD1306.h>

#include "Adafruit_SHT4x.h"

以下为真正代码:

/**************************************************************************
 This is an example for our Monochrome OLEDs based on SSD1306 drivers
 sht4x \ oled 供电电压均为3.3v   
 注意:sht4x 需要调用begin(),否则程序编译正常,运行会异常(屏幕花,循环调用setup()).
 SCL   A5 
 SDA   A4
 点阵文字生成:https://www.zhetao.com/fontarray.html
 使用说明:
 1、需要点击“生成16X16”的按钮生成的文字才有效,同时display.drawBitmap(0, 0, humidityMark, 16, 16, 1); 
 指定为16X16对应。
 2、static const unsigned char PROGMEM注意添加PROGMEM才有效。
 3、虚化 填150,可有效解决字体过粗问题。
 **************************************************************************/

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "Adafruit_SHT4x.h"

#define WORD_SIZE 16 //字体像素宽高 16X16
#define SCREEN_WIDTH 128  // OLED display width, in pixels
#define SCREEN_HEIGHT 32  // OLED display height, in pixels

#define OLED_RESET -1        // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C  ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
Adafruit_SHT4x sht4 = Adafruit_SHT4x();
sensors_event_t humidity, temp;
int lineHight1 = 0;//第一行高度
int lineHight2 = WORD_SIZE + 0;  // 第二行其实高度 = 第一行文字高度 + 0(因屏幕像素高度仅32,所以没办法加行间距)
int valueStart = WORD_SIZE * 2 + 10;// 具体数值的起始坐标
int valueEnd = WORD_SIZE * 2 + 60; // 符号的起始坐标
//虚化50,符号:%
static const unsigned char PROGMEM humidityMark[] = {
    0x00, 0x00, 0x00, 0x00, 0x3c, 0x30, 0x7c, 0x60, 0x66, 0x60, 0x66, 0xc0, 0x66, 0xc0, 0x3d, 0x9c, 
    0x39, 0xbc, 0x03, 0x66, 0x03, 0x66, 0x06, 0x66, 0x06, 0x3e, 0x0c, 0x3c, 0x00, 0x00, 0x00, 0x00
};
//虚化50,符号:摄氏度
static const unsigned char PROGMEM tempMark[] = {
    0x70, 0x00, 0xf0, 0x00, 0xdb, 0xf0, 0xf7, 0xf8, 0x2e, 0x1c, 0x0c, 0x0c, 0x0c, 0x00, 0x1c, 0x00, 
    0x1c, 0x00, 0x0c, 0x04, 0x0c, 0x0c, 0x0e, 0x1c, 0x07, 0xf8, 0x03, 0xf0, 0x00, 0x00, 0x00, 0x00  
};
//温
static const unsigned char PROGMEM wenMark[] = {
    0x00, 0x00, 0x23, 0xfc, 0x12, 0x0c, 0x02, 0x0c, 0x03, 0xfc, 0x62, 0x04, 0x33, 0xfc, 0x10, 0x00, 
    0x00, 0x00, 0x17, 0xfc, 0x16, 0x94, 0x36, 0x94, 0x26, 0x94, 0x26, 0x94, 0x6f, 0xfe, 0x00, 0x00
};
//度
static const unsigned char PROGMEM duMark[] = {
    0x00, 0x00, 0x00, 0x80, 0x3f, 0xfe, 0x22, 0x00, 0x32, 0x18, 0x3f, 0xfe, 0x22, 0x10, 0x23, 0xf0, 
    0x20, 0x00, 0x20, 0x00, 0x27, 0xf8, 0x23, 0x10, 0x61, 0xe0, 0x41, 0xe0, 0x5f, 0x1e, 0x00, 0x00
};
//湿
static const unsigned char PROGMEM shiMark[] = {
    0x00, 0x00, 0x63, 0xfc, 0x32, 0x04, 0x12, 0x04, 0x03, 0xfc, 0x62, 0x04, 0x32, 0x04, 0x13, 0xfc,
    0x00, 0x80, 0x04, 0x92, 0x24, 0x94, 0x22, 0x94, 0x22, 0x90, 0x60, 0x90, 0x4f, 0xfe, 0x00, 0x00
};

void setup() {
  Serial.begin(9600);
  initSht4X();
  initOLED();
}

void loop() {
  display.clearDisplay();

  readSHT4X();
  displayTemperature();
  displayHumidity();

  display.display();
  delay(10 * 1000);//每10秒读一次
}

void initSht4X() {
  Serial.println("start init Sht4X!");
  if (!sht4.begin()) {
    Serial.println("Couldn't find SHT4x");
    while (1) delay(10000000);
  }
  sht4.setPrecision(SHT4X_HIGH_PRECISION);
  sht4.setHeater(SHT4X_NO_HEATER);
  Serial.println("SHT4X init success!");
}

void initOLED() {
  Serial.println("start init OLED!");
   // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println("SSD1306 allocation failed");
    for (;;)
      ;  // Don't proceed, loop forever
  }
  Serial.println("OLED init success!");
  display.setTextSize(1); //设置字体大小
  display.setCursor(35, 5);//设置显示位置
}

void readSHT4X() {
  sht4.getEvent(&humidity, &temp);
}

void displayTemperature() {

  display.drawBitmap(0, lineHight1, wenMark, WORD_SIZE, WORD_SIZE, SSD1306_WHITE); 
  display.drawBitmap(WORD_SIZE, lineHight1, duMark, WORD_SIZE, WORD_SIZE, SSD1306_WHITE); 

  display.setTextSize(2);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(valueStart, lineHight1);

  display.print(temp.temperature, 1);

  display.drawBitmap(valueEnd, lineHight1, tempMark, WORD_SIZE, WORD_SIZE, SSD1306_WHITE); 

}

void displayHumidity() {
  display.drawBitmap(0, lineHight2, shiMark, WORD_SIZE, WORD_SIZE, SSD1306_WHITE); 
  display.drawBitmap(WORD_SIZE, lineHight2, duMark, WORD_SIZE, WORD_SIZE, SSD1306_WHITE); 

  display.setCursor(valueStart, lineHight2);
  display.print(humidity.relative_humidity, 1);
  display.drawBitmap(valueEnd, lineHight2, humidityMark, WORD_SIZE, WORD_SIZE, SSD1306_WHITE); 
}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值