//-----显示屏
#include
#include
#include
#define OLED_RESET 4
Adafruit_SSD1306 oled(OLED_RESET);
#include
SR04 ultrasound_0(3, 2);
#define RELAY 8//继电器
#define ROTATOR A0//旋钮
#define SENSOR A1//土壤湿度传感器
#define DISSENSOR A2//距离传感器
#define LIGHTSENSOR A3//光传感器
static unsigned char PROGMEM const moon[] =
{
B00100000,
B01000000,
B11000000,
B11000000,
B11100010,
B11111100,
B01111000,
B00000000
};
static unsigned char PROGMEM const sun[] =
{
B00010000,
B01000100,
B00111000,
B10111010,
B00111000,
B01000100,
B00010000,
B00000000
};
static unsigned char PROGMEM const rain_bitmap0[] =
{
B00111100,
B01111110,
B01111111,
B11111111,
B00000000,
B01000010,
B01001000,
B00001010
};
static unsigned char PROGMEM const rain_bitmap1[] =
{
B00111100,
B01111110,
B01111111,
B11111111,
B00001000,
B00000000,
B01000010,
B01001000
};
static unsigned char PROGMEM const rain_bitmap2[] =
{
B00111100,
B01111110,
B01111111,
B11111111,
B01001000,
B00001000,
B00000000,
B01000010
};
//浇水模式
byte water_mode = 0;
int water_param[] = {2000, 5000, 7000, 550, 500, 450};//浇水量,湿度下限---最干569 最湿300
byte mode_num = 3;//浇水模式数量
//更改模式
void changeMode()
{
water_mode = analogRead(ROTATOR)/342;
for(byte i = 0; i <= 2; i += 1)
{
oled.drawCircle(6, 10 + i * 10, 2, WHITE);
}
oled.fillCircle(6, 10 + water_mode * 10, 2, WHITE);
}
float circle_size = 0;
void draw3Circle()
{
oled.fillCircle(64, 32 + 3 * sin(circle_size + 0), 2, WHITE);
oled.fillCircle(76, 32 + 3 * sin(circle_size + 2*PI/3), 2, WHITE);
oled.fillCircle(52, 32 + 3 * sin(circle_size + 4*PI/3), 2, WHITE);
circle_size += 0.1;
}
void drawRainIcon()
{
oled.clearDisplay();
oled.drawBitmap(60, 28, rain_bitmap0, 8, 8, 1);
oled.display();
delay(80);
oled.clearDisplay();
oled.drawBitmap(60, 28, rain_bitmap1, 8, 8, 1);
oled.display();
delay(80);
oled.clearDisplay();
oled.drawBitmap(60, 28, rain_bitmap2, 8, 8, 1);
oled.display();
delay(80);
}
static String PROGMEM const greet_list[]=
{
"Hello",
"Bye bye",
"Nice to meet you",
"See you tomorrow"
};
void centerDrawText(String text, byte cursor_y, byte f_size)
{
oled.setTextSize(f_size);
oled.setCursor(65 - text.length() * 6 * f_size / 2, cursor_y);
oled.print(text);
}
uint32_t last_greet = -10000;
bool greet = 0;
String greet_word = "";
void greeting()
{
if (ultrasound_0.Distance() <= 30)//有人
{
greet = 1;
if(millis() - last_greet >= 5000)//超时
{
last_greet = millis();
greet_word = greet_list[random(4)];
}
}
else
{
greet = 0;
}
if (greet)
{
centerDrawText(greet_word, 32, 1);
}
}
//------------------------------------
bool pump_flag = 0;
uint32_t last_water = 0;//上次浇水时间
void watering()//浇水
{
if(analogRead(SENSOR) < water_param[mode_num + water_mode])
{
pump_flag = 1;
digitalWrite(RELAY, HIGH);
digitalWrite(13, HIGH);
}
if(pump_flag)//如果水泵打开
{
//draw3Circle();
if(millis() - last_water >= water_param[water_mode])//灌溉完毕
{
pump_flag = 0;
last_water = millis();
for(byte i = 0; i <= 10; i ++)
{
digitalWrite(RELAY, LOW);
delay(5);
}
Serial.println("stop");
}
}
}
void show_lumi()
{
//最亮20, 最暗800
// 5 1
byte lumi = max(9 - analogRead(LIGHTSENSOR)/40, 0);
oled.setCursor(116, 13);
oled.print(lumi);
if(lumi > 5)
oled.drawBitmap(115, 4, sun, 8, 8, 1);
else
oled.drawBitmap(115, 4, moon, 8, 8, 1);
}
//=====================================================
void setup()
{
Serial.begin(9600);
pinMode(RELAY, OUTPUT);
pinMode(13, OUTPUT);
pinMode(ROTATOR, INPUT);
pinMode(SENSOR, INPUT);
pinMode(LIGHTSENSOR, INPUT);
oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
oled.clearDisplay();
oled.display();
oled.setTextColor(WHITE);
}
void loop()
{
oled.clearDisplay();
//drawRainIcon();
show_lumi();
changeMode();
greeting();
watering();
oled.display();
}