답변 : 방수온도센서를 이용한 네오픽셀점등
페이지 정보
작성자 master 작성일18-12-20 21:39 조회6,835회 댓글1건본문
// MCU BASIC: https://www.basic4mcu.com
// DateTime : 2018-12-20 오후 9:46:30
// by Ok-Hyun Park
//
//방수온도센서를 이용해서 온도별로 네오픽셀 링타입 24구에 색을다르게 출력하고싶은데 잘 안되네요 도와주세요 ㅜㅜ
//
#include <OneWire.h>
int DS18S20_Pin=2,ledNum; //온도센서를 2번 핀으로 연결
OneWire ds(DS18S20_Pin); //2번 핀과 연결되 OneWire 객체 생성
//
#include <Adafruit_NeoPixel.h>
#define PIN 6
Adafruit_NeoPixel strip=Adafruit_NeoPixel(60,PIN,NEO_GRB+NEO_KHZ800);
//
//
void setup(){
Serial.begin(9600);
strip.begin();
strip.show();
}
//
void setBlue(int ledNum){
for(int i=0; i<ledNum; i++)strip.setPixelColor(i,strip.Color(0,0,200));
}
//
void setGreen(int ledNum){
for(int i=0; i<ledNum; i++)strip.setPixelColor(i,strip.Color(0,200,0));
}
//
void setRed(int ledNum){
for(int i=0; i<ledNum; i++)strip.setPixelColor(i,strip.Color(255,0,0));
}
//
void setPurple(int ledNum){
for(int i=0; i<ledNum; i++)strip.setPixelColor(i,strip.Color(102,0,102));
}
//
void loop(){
float temperature=getTemp(); //온도 측정 후 변수에 저장
Serial.println(temperature);
//
if (temperature<=12)setBlue(24); //온도가 12도 이하일 때,파란색 LED 점등
else if(temperature<=25)setGreen(24); //온도가 12도 이상 25미만일 때,초록색 LED 점등
else if(temperature<=30)setRed(24); //온도가 25도 이상 30미만일 때,빨강색 LED 점등
else if(temperature<=40)setPurple(24); //온도가 30도 이상일 때,보리색 LED 점등
strip.show();
delay(3000);
}
//
float getTemp(){ //온도 측정 후 반환하는 함수
byte data[12];
byte addr[8];
if(!ds.search(addr)){ ds.reset_search(); return-1000; }
if(OneWire::crc8(addr,7)!=addr[7]){ Serial.println("CRC is not valid!"); return-1000; }
if(addr[0]!=0x10&&addr[0]!=0x28){ Serial.print("Device is not recognized"); return-1000; }
ds.reset();
ds.select(addr);
ds.write(0x44,1);
byte present=ds.reset();
ds.select(addr);
ds.write(0xBE);
for(int i=0; i<9; i++){ data[i]=ds.read(); }
ds.reset_search();
byte MSB=data[1];
byte LSB=data[0];
float tempRead=((MSB<<8)|LSB);
float TemperatureSum=tempRead/16;
return TemperatureSum;
}
댓글 : 1
조회수 : 6,835
구퓨리님의 댓글
구퓨리 작성일몇시간이나 머리 싸매고 못 고치고 있었는데 너무 감사합니다ㅠㅜㅠㅜㅠ<3 ㅜㅠㅜㅠ