#include <LiquidCrystal.h> // LCD ¸ð´ÏÅÍ Çì´õ
#include <SoftwareSerial.h> // ½Ã¸®¾ó Åë½Å Çì´õ
#include <DHT11.h> // ¿Â½Àµµ ¼¾¼
#include <Wire.h> // RCT
#include "RTClib.h" // RCT
#include <Adafruit_NeoPixel.h>
Adafruit_NeoPixel strip = Adafruit_NeoPixel(16, 5, NEO_GRBW + NEO_KHZ800);
LiquidCrystal lcd(12, 11, 9, 8, 7, 6);
SoftwareSerial Blue(2, 3);
DHT11 dht11(4);
const int sensorPin = A0;
RTC_DS1307 RTC;
int lightLevel;
int brightness;
void setup() {
Serial.begin(9600); //½Ã¸®¾ó¸ð´ÏÅÍ
Blue.begin(9600); //ºí·çÅõ½º ½Ã¸®¾ó
Wire.begin();
RTC.begin();
#if defined (__AVR_ATtiny85__)
if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
strip.begin();
strip.show(); // Initialize all pixels to 'off'
lcd.begin(16, 2); // LCDÀÇ Å©±â¸¦ ¼³Á¤ÇÑ´Ù.
analogWrite(10, 120);
lcd.print("hello, world!");
delay(500);
lcd.clear();
}
int command[10];
char resend[30] = {'\0'};
int aram_h;
int aram_m;
//////////////////////////////////////////////////////////////////////////////
void loop() {
get_command();
//1: ºû »ö±ò ºñ²Ù±â
if (command[0] == 1) {
colorWipe(strip.Color(command[1], command[2], command[3]), 0);
initial_command();
}
// 2: »óÅ¿¡ ´ëÇÑ ÀÀ´ä
else if (command[0] == 2) {
resend_state();
initial_command();
}
//3: ¾Ë¶÷¼³Á¤
else if (command[0] == 3) {
set_aram();
resend_aram();
initial_command();
}
//4: ½Ã°£µ¿±âÈ
else if (command[0] == 4) {
set_time();
resend_time();
initial_command();
}
//5 : ºû ¹à±â Á¶Àý
else if (command[0] == 5) {
if (command[1] == 0)
command[1] = 500;
strip.setBrightness(command[1]);
initial_command();
}
DateTime now = RTC.now();
if (aram_h == now.hour() && aram_m == now.minute()) {
Serial.print("wef");
rainbow(20);
}
}
//////////////////////////////////////////////////////////////////////////////
void get_command() {
char arr[25] = {'\0'};
char *tmp;
int i = 0;
int j = 0;
while (Blue.available()) {
char myChar = (char)Blue.read();
arr[i++] += myChar;
delay(10);
}
if (arr[0]) {
tmp = strtok(arr, "/");
while (tmp != NULL) {
command[j++] = atoi(tmp);
tmp = strtok(NULL, "/");
}
}
for (int k = 0; k < j; k++) {
Serial.println(command[k]);
}
}
void initial_command() {
for (int i = 0; i < 10; i++)
command[i] = 0;
}
uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if (WheelPos < 85) {
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
if (WheelPos < 170) {
WheelPos -= 85;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
WheelPos -= 170;
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
void rainbow(uint8_t wait) {
uint16_t i, j;
for (j = 0; j < 256; j++) {
for (i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel((i + j) & 255));
}
strip.show();
delay(wait);
}
}
void colorWipe(uint32_t c, uint8_t wait) {
for (uint16_t i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}
void set_aram(){
aram_h=command[1];
aram_m=command[2];
}
void resend_aram(){
char tmp[5] = {0};
strcpy(resend, "3/");
itoa(aram_h, tmp, 10);
strcat(resend, tmp);
strcat(resend, "/");
itoa(aram_m, tmp, 10);
strcat(resend, tmp);
strcat(resend, "\n");
Blue.write(resend);
}
char *char_month[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
void set_time() {
char date_data[11] = {'\0'};
char time_data[8] = {'\0'};
char tmp[5] = {0};
strcpy(date_data, char_month[command[2] - 1]);
strcat(date_data, " ");
itoa(command[3], tmp, 10);
strcat(date_data, tmp);
strcat(date_data, " ");
itoa(command[1], tmp, 10);
strcat(date_data, tmp);
itoa(command[4], tmp, 10);
strcpy(time_data, tmp);
strcat(time_data, ":");
itoa(command[5], tmp, 10);
strcat(time_data, tmp);
strcat(time_data, ":");
itoa(command[6], tmp, 10);
strcat(time_data, tmp);
RTC.adjust(DateTime(date_data, time_data));
}
void resend_time() {
DateTime now = RTC.now();
char tmp[5] = {0};
strcpy(resend, "4/");
itoa(now.year(), tmp, 10);
strcat(resend, tmp);
strcat(resend, "/");
itoa(now.month(), tmp, 10);
strcat(resend, tmp);
strcat(resend, "/");
itoa(now.day(), tmp, 10);
strcat(resend, tmp);
strcat(resend, "/");
itoa(now.hour(), tmp, 10);
strcat(resend, tmp);
strcat(resend, "/");
itoa(now.minute(), tmp, 10);
strcat(resend, tmp);
strcat(resend, "/");
itoa(now.second(), tmp, 10);
strcat(resend, tmp);
strcat(resend, "\n");
Blue.write(resend);
}
void resend_state() {
// ½Ã°£ºÎºÐ
DateTime now = RTC.now();
char tmp[5] = {0};
strcpy(resend, "2/");
itoa(now.year(), tmp, 10);
strcat(resend, tmp);
strcat(resend, "/");
itoa(now.month(), tmp, 10);
strcat(resend, tmp);
strcat(resend, "/");
itoa(now.day(), tmp, 10);
strcat(resend, tmp);
strcat(resend, "/");
itoa(now.hour(), tmp, 10);
strcat(resend, tmp);
strcat(resend, "/");
itoa(now.minute(), tmp, 10);
strcat(resend, tmp);
strcat(resend, "/");
itoa(now.second(), tmp, 10);
strcat(resend, tmp);
// ¹à±â ºÎºÐ
lightLevel = analogRead(sensorPin);
lightLevel = map(lightLevel, 500, 1023, 0, 255);
lightLevel = constrain(lightLevel, 0, 255);
strcat(resend, "/");
itoa(lightLevel, tmp, 10);
strcat(resend, tmp);
//¿Â½Àµµ ºÎºÐ
float temp, humi;
dht11.read(humi, temp);
strcat(resend, "/");
itoa((int)temp, tmp, 10);
strcat(resend, tmp);
strcat(resend, "/");
itoa((int)humi, tmp, 10);
strcat(resend, tmp);
strcat(resend, "\n");
Blue.write(resend);
}
´ÜÀÚ»çÁø
»çÁø¿¡ º¸½Ã´Ù½ÃÇÇ LED°¡ 6°³¸¸ µé¾î¿É´Ï´Ù.