分享贴子
单片机吧
全部回复
仅看楼主
level 12
分享贴子
2018年05月23日 01点05分 1
level 12
[哈哈]
2018年09月28日 05点09分 3
level 12

2018年10月10日 13点10分 5
level 12

2018年10月14日 06点10分 6
level 12

2018年10月15日 10点10分 7
level 12

2018年10月21日 01点10分 9
level 12

2018年10月22日 06点10分 10
level 12

2018年10月31日 06点10分 11
level 12
👍
2018年11月08日 14点11分 12
level 12

2018年11月09日 07点11分 13
level 12
👍
2018年11月10日 07点11分 14
level 12
棒棒的
2018年11月12日 02点11分 15
level 12

2018年11月24日 01点11分 17
level 7
这个可以用arduino替代
2018年11月24日 01点11分 18
level 7
#include <stdio.h>
#include <string.h>
#include <dht11.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DS1302.h>
char sensor;
static long timer;
static byte session;
long previousMillis = 0;
long interval = 1000;
int sensorPin = A3;
int sensorValue = 0;
int sensorPin1 = A7;
int sensorValue1 = 0;
const int buzz=3;
const int alcoholic=A6;
static int alcoholic_value=200;
char buflcd[16];
dht11 DHT11;
#define DHT11PIN 2
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
namespace {
// Set the appropriate digital I/O pin connections. These are the pin
// assignments for the Arduino as well for as the DS1302 chip. See the DS1302
// datasheet:
//
// http://datasheets.maximintegrated.com/en/ds/DS1302.pdf
const int kCePin = A2; // Chip Enable
const int kIoPin = A1; // Input/Output
const int kSclkPin = A0; // Serial Clock
// Create a DS1302 object.
DS1302 rtc(kCePin, kIoPin, kSclkPin);
void ds1302_init()
{
rtc.writeProtect(false);
rtc.halt(false);
}
String dayAsString(const Time::Day day) {
switch (day) {
case Time::kSunday: return "Sunday";
case Time::kMonday: return "Monday";
case Time::kTuesday: return "Tuesday";
case Time::kWednesday: return "Wednesday";
case Time::kThursday: return "Thursday";
case Time::kFriday: return "Friday";
case Time::kSaturday: return "Saturday";
}
return "(unknown day)";
}
void printTime() {
// Get the current time and date from the chip.
Time t = rtc.time();
// Format the time and date and insert into the temporary buffer.
snprintf(buflcd, sizeof(buflcd), "%2d-%d-%d %d:%d:%d ",
t.yr-2000, t.mon, t.date,
t.hr, t.min, t.sec);
// Print the formatted string to serial so we can see the time.
lcd.setCursor(0,1);
lcd.print(buflcd);
}
void edit_time()
{
/* 串口数据缓存 */
String comdata = "";
int numdata[7] ={0}, j = 0, mark = 0;
while (Serial.available() > 0)
{
comdata += char(Serial.read());
delay(2);
mark = 1;
}
/* 以逗号分隔分解comdata的字符串,分解结果变成转换成数字到numdata[]数组 */
if(mark == 1)
{
Serial.print("You inputed : ");
Serial.println(comdata);
for(int i = 0; i < comdata.length() ; i++)
{
if(comdata[i] == ',' || comdata[i] == 0x10 || comdata[i] == 0x13)
{
j++;
}
else
{
numdata[j] = numdata[j] * 10 + (comdata[i] - '0');
}
}
if(numdata[0]>2000)
{
/* 将转换好的numdata凑成时间格式,写入DS1302 */
Time t(numdata[0], numdata[1], numdata[2], numdata[3], numdata[4], numdata[5], Time::kSaturday );
rtc.time(t);
}
else
{
if(numdata[0]>100)
{
alcoholic_value= numdata[0];
}
}
}
}
} // namespace
#if defined(ARDUINO) && ARDUINO >= 100
#define printByte(args) write(args);
#else
#define printByte(args) print(args,BYTE);
#endif
uint8_t bell[8] = {0x4,0xe,0xe,0xe,0x1f,0x0,0x4};
uint8_t note[8] = {0x2,0x3,0x2,0xe,0x1e,0xc,0x0};
uint8_t clock[8] = {0x0,0xe,0x15,0x17,0x11,0xe,0x0};
uint8_t heart[8] = {0x0,0xa,0x1f,0x1f,0xe,0x4,0x0};
uint8_t duck[8] = {0x0,0xc,0x1d,0xf,0xf,0x6,0x0};
uint8_t check[8] = {0x0,0x1,0x3,0x16,0x1c,0x8,0x0};
uint8_t cross[8] = {0x0,0x1b,0xe,0x4,0xe,0x1b,0x0};
uint8_t retarrow[8] = {0x1,0x1,0x5,0x9,0x1f,0x8,0x4};
void lcd_init()
{
lcd.init(); // initialize the lcd
lcd.backlight();
lcd.createChar(0, bell);
lcd.createChar(1, note);
lcd.createChar(2, clock);
lcd.createChar(3, heart);
lcd.createChar(4, duck);
lcd.createChar(5, check);
lcd.createChar(6, cross);
lcd.createChar(7, retarrow);
}
void dh11()
{
int chk = DHT11.read(DHT11PIN);
Serial.print("Humidity (%): ");
Serial.println((int)DHT11.humidity);
Serial.print("Temperature (oC): ");
Serial.println((int)DHT11.temperature);
sensorValue = analogRead(sensorPin);
Serial.print("Light=" );
Serial.println(sensorValue);
sensorValue1 = analogRead(sensorPin1);
Serial.print("alcoholic=" );
Serial.println(sensorValue1);
lcd.setCursor(0,0);
snprintf(buflcd, sizeof(buflcd), "%2dC %2d %3d %3d ", DHT11.temperature,DHT11.humidity,sensorValue,sensorValue1);
lcd.print(buflcd);
}
void pin_mode()
{
pinMode(buzz, OUTPUT);
digitalWrite(buzz, LOW);
pinMode(alcoholic, INPUT);
}
void alcoholic_buzz()
{
sensorValue1 = analogRead(sensorPin1);
if(sensorValue1>alcoholic_value)
{
digitalWrite(buzz, HIGH);
lcd.setCursor(15, 0);
lcd.printByte(0);
}
else
{
digitalWrite(buzz, LOW);
lcd.setCursor(15, 0);
lcd.print(" ");
}
}
void setup()
{
Serial.begin(115200);
pin_mode();
lcd_init();
ds1302_init();
}
void loop()
{
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval)
{
//display temperature and humidity
previousMillis = currentMillis;
dh11();
alcoholic_buzz();
printTime();
}
edit_time();
}
2018年11月24日 01点11分 19
1 2 尾页