개발자/Arduino

미세먼지 측정기 오늘 잘 돌아가는 코드

지구빵집 2021. 4. 30. 00:19
반응형

 

 

 

강력하고 정확한 것 두 가지를 만족시키기는 어렵다. 힘이 들어가면 빗나가거나 정확한 지점에 힘을 집중할 수 없기 때문이다. 무엇보다 힘을 빼야만 정확한 지점에 집중할 수 있다. 가끔은 강력하면서도 정확하게 할 때가 있다. 그런 시간을 넘기 직전에 있는지도 모른다.   

 

하~ 힘들다.

 


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

#include <SimpleDHT.h>
#include <pm2008_i2c.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
//#define OLED_RESET     4 // Reset pin # (or -1 if sharing Arduino reset pin)
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

#define NUMFLAKES     10 // Number of snowflakes in the animation example

#define LOGO_HEIGHT   16
#define LOGO_WIDTH    16

/*
static const unsigned char PROGMEM logo_bmp[] =
{ B00000000, B11000000,
  B00000001, B11000000,
  B00000001, B11000000,
  B00000011, B11100000,
  B11110011, B11100000,
  B11111110, B11111000,
  B01111110, B11111111,
  B00110011, B10011111,
  B00011111, B11111100,
  B00001101, B01110000,
  B00011011, B10100000,
  B00111111, B11100000,
  B00111111, B11110000,
  B01111100, B11110000,
  B01110000, B01110000,
  B00000000, B00110000 };
*/

// for DHT11, 
//      VCC: 5V or 3V
//      GND: GND
//      DATA: 9

int pinDHT11 = 9;
SimpleDHT11 dht11(pinDHT11);
PM2008_I2C pm2008_i2c;

int flag = 0; //for interrupt
#define debounceTime 200 // <<- Set debounce Time (unit ms) 

int redPin = 5;
int greenPin = 4;
int bluePin = 3;
int buttoninput = 2;

void setup()
{
    Serial.begin(9600);
    
    // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
    // 바로 여기다 3C 로 수정한다. 
    if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) 
    { // Address 0x3D for 128x64
        Serial.println(F("SSD1306 allocation failed"));
        for(;;); // Don't proceed, loop forever
    }

    pinMode(redPin, OUTPUT);
    pinMode(greenPin, OUTPUT);
    pinMode(bluePin, OUTPUT);
    pinMode(buttoninput, INPUT);
    attachInterrupt(0, ButtonInterrupt, FALLING);

    digitalWrite(redPin, LOW);
    digitalWrite(greenPin, LOW);
    digitalWrite(bluePin, LOW);


    pm2008_i2c.begin();
    pm2008_i2c.command();

    initial_oled();
    
    //delay(1000);

    // Show initial display buffer contents on the screen --
    // the library initializes this with an Adafruit splash screen.
    //display.display();
    //delay(2000); // Pause for 2 seconds

    // Clear the buffer
    //display.clearDisplay();

    // Draw a single pixel in white
    //display.drawPixel(10, 10, SSD1306_WHITE);

    // Show the display buffer on the screen. You MUST call display() after
    // drawing commands to make them visible on screen!
    //display.display();
    //delay(2000);
    // display.display() is NOT necessary after every single drawing command,
    // unless that's what you want...rather, you can batch up a bunch of
    // drawing operations and then update the screen all at once by calling
    // display.display(). These examples demonstrate both approaches...
}

int currentmode = 1;

void loop()
{
    //mode lection process
    if(currentmode == 1)
    {
        offlight();
        showtemphumi();
    }
        
    if(currentmode == 2)
    {
        offlight();
        showfinedust();
    }
        
    if(currentmode == 3)
    {
        showlight();        
    }
    Serial.println(currentmode);
    delay(500);

    //below Test Code 
    //rgbtest();
    //testdht11();
    //testpm2008m();
    //testbutton();
    
}

void showfinedust()
{
    uint8_t ret = pm2008_i2c.read();
    //Serial.print(ret);
    if (ret == 0) {
        Serial.print("PM 1.0 (TSI) : ");
        Serial.println(pm2008_i2c.pm1p0_tsi);
        Serial.print("PM 2.5 (TSI) : : ");
        Serial.println(pm2008_i2c.pm2p5_tsi);
        Serial.print("PM 10 (TSI) : : ");
        Serial.println(pm2008_i2c.pm10_tsi);
    }
    delay(1000);
}

void showlight()
{
    digitalWrite(redPin, HIGH);
    digitalWrite(greenPin, HIGH);
    digitalWrite(bluePin, HIGH);
}

void offlight()
{
    digitalWrite(redPin, LOW);
    digitalWrite(greenPin, LOW);
    digitalWrite(bluePin, LOW);
}


char _buffer[8];

void showtemphumi()
{
    byte temperature = 0;
    byte humidity = 0;
  
    if (dht11.read(pinDHT11, &temperature, &humidity, NULL)) {
        Serial.print("Read DHT11 failed.");
        return;
    }

    display.setCursor(23, 10); 
    sprintf(_buffer, "%d ", (int)temperature);
    display.print(_buffer);
    display.print((char)248); // degree symbol 
    display.println("C");
  
    display.setCursor(23, 30); 
    sprintf(_buffer, "%d ", (int)humidity);
    display.print(_buffer);
    display.println("%");
  
    //display.setCursor(23, 50);
    //printf(_buffer, "On/Off");
    //display.print("On/Off");
  
    //display.drawCircle(88, 12, 2, WHITE); 
  
    // update the display 
    display.display();
}

void initial_oled()
{
    //initialize the SSD1306 OLED display with I2C address = 0x3D
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);

  // clear the display buffer.
  display.clearDisplay();
 
  display.setTextSize(1);   // text size = 1
  display.setTextColor(WHITE, BLACK);  // set text color to white and black background
  display.setCursor(15, 0);            // move cursor to position (15, 0) pixel
  display.print("Today Weather");
  display.display();        // update the display
  display.setTextSize(2);   // text size = 2
}


void testbutton()
{
    if(flag == 1)
    {
        digitalWrite(bluePin, HIGH);
    }
    else 
    {
        digitalWrite(bluePin, LOW);
    }

    Serial.println(flag);
    delay(100);
}

void ButtonInterrupt() 
{
    //flag = !flag;
    
    Serial.println("interrupt");
    static unsigned long lastTime = 0;
    unsigned long now = millis();
    if((now-lastTime) > debounceTime)
    {
        flag=!flag;
    }
    lastTime = now;
} 

void testpm2008m()
{
    uint8_t ret = pm2008_i2c.read();
    //Serial.print(ret);
  if (ret == 0) {
    //Serial.print("PM 1.0 (GRIMM) : ");
    //Serial.println(pm2008_i2c.pm1p0_grimm);
    //Serial.print("PM 2.5 (GRIMM) : : ");
    //Serial.println(pm2008_i2c.pm2p5_grimm);
    //Serial.print("PM 10 (GRIMM) : : ");
    //Serial.println(pm2008_i2c.pm10_grimm);
    Serial.print("PM 1.0 (TSI) : ");
    Serial.println(pm2008_i2c.pm1p0_tsi);
    Serial.print("PM 2.5 (TSI) : : ");
    Serial.println(pm2008_i2c.pm2p5_tsi);
    Serial.print("PM 10 (TSI) : : ");
    Serial.println(pm2008_i2c.pm10_tsi);
    //Serial.print("Number of 0.3 um : ");
    //Serial.println(pm2008_i2c.number_of_0p3_um);
    //Serial.print("Number of 0.5 um : ");
    //Serial.println(pm2008_i2c.number_of_0p5_um);
    //Serial.print("1 um : ");
    //Serial.println(pm2008_i2c.number_of_1_um);
    //Serial.print("2.5 um : ");
    //Serial.println(pm2008_i2c.number_of_2p5_um);
    //Serial.print("Number of 5 um : ");
    //Serial.println(pm2008_i2c.number_of_5_um);
    //Serial.print("10 um : ");
    //Serial.println(pm2008_i2c.number_of_10_um);
  }
  delay(1000);
}

void testdht11() {
  // start working...
  Serial.println("=================================");
  Serial.println("Sample DHT11...");
  
  // read without samples.
  byte temperature = 0;
  byte humidity = 0;
  int err = SimpleDHTErrSuccess;
  if ((err = dht11.read(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
    Serial.print("Read DHT11 failed, err="); Serial.print(SimpleDHTErrCode(err));
    Serial.print(","); Serial.println(SimpleDHTErrDuration(err)); delay(1000);
    return;
  }
  
  Serial.print("Sample OK: ");
  Serial.print((int)temperature); Serial.print(" *C, "); 
  Serial.print((int)humidity); Serial.println(" H");
  
  // DHT11 sampling rate is 1HZ.
  delay(1500);
}

    
void rgbtest()
{
    setColor(255, 0, 0); // red
    delay(1000);
    /*setColor(0, 255, 0); // green
    delay(1000);
    setColor(0, 0, 255); // blue
    delay(1000);
    setColor(255, 255, 0); // yellow
    delay(1000); 
    setColor(80, 0, 80); // purple
    delay(1000);
    setColor(0, 255, 255); // aqua
    delay(1000);*/
}

void setColor(int red, int green, int blue)
{
  analogWrite(redPin, red);
  analogWrite(greenPin, green);
  analogWrite(bluePin, blue); 
}

void swInterrupt() 
{
    //flag = !flag;   
    
    static unsigned long lastTime = 0;
    unsigned long now = millis();
    if((now-lastTime) > debounceTime)
    {
        Serial.println("int0");
        flag=!flag;
    }
    lastTime = now;
} 

 

미세먼지 센서 테스트

 

 

반응형