基于arduino的空气检测器

基于arduino的空气检测器_第1张图片

此空气检测器用了:OLED,ISD1820,DS18B20,MQ135模块,电扇

#include 
#include 
#include "U8glib.h"
// 定义DS18B20数据口连接arduino的2号IO上
#define ONE_WIRE_BUS 2
/ 初始连接在单总线上的单总线设备
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
U8GLIB_SSD1306_128X64 u8g(13, 11, 10, 9);  // SW SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9
const int gasSensor =0;
int toneval;
int IN3 = 4;   // 
int IN4 = 5;   
int ENB = 3;  
int Play_pin = 7;
static unsigned char word1[] U8G_PROGMEM = {0x00,0x00,0x00,0x00,0xBF,0x7F,0x08,0x08,0x08,0x08,0x08,0x04,0x08,0x04,0x3E,0x16,
0x08,0x25,0x88,0x44,0x48,0x44,0x08,0x04,0x38,0x04,0x07,0x04,0x02,0x04,0x00,0x04};//汉字编码

static unsigned char word2[] U8G_PROGMEM = {0x04,0x01,0x04,0x02,0xC4,0x1F,0x84,0x08,0x1F,0x05,0xE4,0x7F,0x04,0x00,0xC4,0x1F,
0x44,0x10,0xC4,0x1F,0x44,0x10,0xDC,0x1F,0x87,0x04,0x82,0x44,0x40,0x44,0x30,0x78};/*"气",1*/
static unsigned char word3[] U8G_PROGMEM = {0x08,0x02,0x08,0x02,0x08,0x05,0x08,0x05,0xBF,0x08,0x48,0x10,0xAC,0x6F,0x1C,0x00,
0x2A,0x11,0x0A,0x12,0x49,0x12,0x88,0x0A,0x88,0x08,0x08,0x04,0xE8,0x7F,0x08,0x00};/*"检",2*/
static unsigned char word4[] U8G_PROGMEM = {0x00,0x20,0xE4,0x23,0x28,0x22,0x28,0x2A,0xA1,0x2A,0xA2,0x2A,0xA2,0x2A,0xA8,0x2A,
0xA8,0x2A,0xA4,0x2A,0xA7,0x2A,0x84,0x20,0x44,0x21,0x44,0x22,0x24,0x28,0x10,0x10};/*"测",3*/
static unsigned char word5[] U8G_PROGMEM = {0x00,0x00,0x7C,0x3E,0x44,0x22,0x44,0x22,0x7C,0x3E,0x80,0x04,0x80,0x08,0xFF,0x7F,
0x40,0x01,0x30,0x06,0x0C,0x18,0x03,0x60,0x7C,0x3E,0x44,0x22,0x44,0x22,0x7C,0x3E};/*"器",4*/

void draw(void) 
{
 
  u8g.setPrintPos(0,35); 
  u8g.print("Air :");
  u8g.setPrintPos(0,55);
  u8g.print("Temp:");
}

void setup(void) {
  Serial.begin(57600);//波特率
  pinMode(IN3, OUTPUT);
pinMode(Play_pin,OUTPUT);
    pinMode(IN4, OUTPUT);
    // 初始库
  sensors.begin();
}

void loop(void) {
  float  temp;
   sensors.requestTemperatures(); // 发送命令获取温度
  Serial.print("Temperature for the device 1 (index 0) is: ");
  temp=sensors.getTempCByIndex(0);
  Serial.println(temp);  //通过索引值来获取摄氏温度,输入参数为器件索引值,返回参数为摄氏温度。
  u8g.firstPage();  
  do 
  { u8g.setFont(u8g_font_unifont);//设置字体
    u8g.drawXBMP(0,0,16,16,word1);
   u8g.drawXBMP(22,0,16,16,word2);
   u8g.drawXBMP(44,0,16,16,word3);
   u8g.drawXBMP(66,0,16,16,word4);
   u8g.drawXBMP(88,0,16,16,word5);
    draw();
     float voltage;
  voltage = getVoltage(0);
  Serial.println(voltage);
   u8g.setPrintPos(50,35); 
   u8g.print(voltage);
     u8g.setPrintPos(50,55);
    u8g.print((float)sensors.getTempCByIndex(0));
  if(temp>30||temp ==30) 
   {
    digitalWrite(Play_pin,HIGH);
    delay(500);
    digitalWrite(Play_pin,HIGH);
    delay(500);
    digitalWrite(IN3, HIGH);
    digitalWrite(IN4, LOW);
     analogWrite(ENB, 128);
     delay(500);
   } 
   else
   {
    digitalWrite(Play_pin,LOW);
    digitalWrite(IN3, LOW);
    digitalWrite(IN4, LOW);
    }
  } while( u8g.nextPage() );
  delay(500);

}

float getVoltage(int pin)
{ 
  return (analogRead(pin) * 0.004882814); 
}

你可能感兴趣的:(基于arduino的空气检测器)