Today took out the home Arduino again out to play a bit, also looked at some recent online information. Feel the current online article still stay in this how to use, that how to use the stage. There are few examples of the combination. So, I will write the code today, for everyone to refer to the brick.
Arduino, after all, is the basis of C language. So the structure of the program is still very important. In the loop function, it still holds the distinction between the acquisition and the specific processing. This should be important for subsequent maintenance of the program.
The other is the C function returns an array. This problem has been solved in the traditional C, but it seems to be slightly different in Arduino C. If the reader has a better way, please correct me.
The following nonsense less said, direct code posted.
/** * Desktop Work Companion * function: 1, feedback desktop temperature and humidity * 2, judge whether people sit down * 3, warn themselves not to keep a posture too long * version Number: 1.0 * Author: tianxin * Date: December 2015 1 8th/* PIN definition */#define DHT_PIN 2/IO temperature and humidity sensor data/#define TRG_PIN 3/O distance sensor start/#define EC H_pin/I distance sensor data input/#define ALM_PIN 4/* o buzzer Output/#define HMC_PIN 8/I body sensor data loss Enter/* global variable declaration * * * #define ALARM_COUNTER 180;
* * Monitoring the human body's alarm cycle, 15 minutes/void Setup () {serial.begin (9600);
Serial.println ("===setuped===");
The void Loop () {/* * delay every 5 seconds (5000);
/* Obtain temperature and humidity information */int *dht = Gettemperatureandhumidity ();
/* Get distance information */int distance = Getdistance ();
* * Monitor the human body and alarm/int humancheck = Gethumaninfo ();
* * Human sensors feel too long no activity, start the alarm */if (humancheck <= 0) {digitalwrite (Alm_pin, high);
}else{Digitalwrite (Alm_pin, low);
/* Parameter output//* Distance//Serial.print ("Distance:");
Serial.print (distance);
Serial.println ("cm.");
/* Humidity * * Serial.print ("humidity:"); Serial.print (*DHT);
Serial.println ("%.");
/* Temperature * * Serial.print ("Temperature:");
Serial.print (* (dht+1));
Serial.println ("C.");
/* Motion Countdown * * Serial.print ("Nobody Check Counter:");
Serial.println (Humancheck);
/* Clean memory */delete[] DHT; /** * Obtain the status of the sensor state * parameter: NO * return value: Whether the human body is currently in motion * return value = Predefined constant Alarm_counter is the motion * return value < Predefined constant is not in the motion * version This number: 1.0 */int gethumaninfo () {static int alarmcounter = Alarm_counter;
Set static parameter/* PIN definition/pinmode (Alm_pin, OUTPUT);
Pinmode (Hmc_pin, INPUT); * * If the human body is detected, think the human body is moving.
Alarm counter Restart */if (Digitalread (hmc_pin) = = high) {alarmcounter = Alarm_counter;
}else{/* Without detection of the human body, alarm counter Start countdown count * * * alarmcounter--;
/* Alarm counter lower bound set */if (alarmcounter <= 0) {alarmcounter = 0;
}//* Returns the value of the alarm counter */return alarmcounter;
/** * Obtain DHT11 humidity and problem information * parameter: NO * return value: Array [0] humidity, [1] Temperature * Version: 1.0 * Description: Learn to use, so no call library * * int* gettemperatureandhumidity () { int temperature; /* Temperature */INT Humidity; /* Humidity */int tokencheck; /* Parity bit */int chr[40] = {0}; /* Sensor return value array */unsigned long time; /* Sampling time record */int *dht = new Int[2];
/* Return Array Object *//////////////////* Continue to send sequential sequence to sensor until correct feedback is received (pushsingle4dht11) {};
/* Start receiving sensor Feedback (40 bytes)/for (int i = 0; i < i++) {/* monitor low level until it ends/while digitalread (dht_pin) = =
/* Record the high level of the initial time * * = Micros (); /* Monitor the high level until its end/while (Digitalread (dht_pin) = = high) {}/* If the duration of the higher level is greater than 50um, it is considered 1, otherwise 0 */if (Micros ()-Time & Gt
{Chr[i] = 1;
}else{Chr[i] = 0; }//* Humidity Specific value * * humidity = chr[0] * 128 + chr[1] * 4 + chr[2] * + chr[3] * + chr[4] * 8 + chr[5] * +
CHR[6] * 2 + chr[7]; /* Get the specific value of the temperature * * Temperature = chr[16] * 128 + chr[17] * 4 + chr[18] * * + chr[19] * + chr[20] * 8 + chr[21] * + CH
R[22] * 2 + chr[23]; /* Get the value of the check bit * * Tokencheck = chr[32] * 128 + CHR[33] * + chr[34] * + chr[35] * + chr[36] * 8 + chr[37] * 4 + chr[38] * 2 + chr[39];
/* Check pass, the value of temperature and humidity into the return array/if (humidity + temperature = = Tokencheck) {dht[0] = humidity;
DHT[1] = temperature;
}/* Return value * * /** * Send measurement request signal * parameter: NO * return value: True--Request failed re-request/Boolean pushsingle4dht11 () {int waitingcounter;
/* Wait counter////////////Delay (40) with the last send measurement signal interval 40 milliseconds.
/* Set the IO mode to send the request signal to the sensor/Pinmode (Dht_pin, OUTPUT);
/* 1, a low level of 20 milliseconds/digitalwrite (Dht_pin, lower);
Delay (20);
/* 2, a continuous 40 microseconds high level * * Digitalwrite (Dht_pin, GAO);
Delaymicroseconds (40);
/* 3, restore the low level, and wait for sensor feedback * * Digitalwrite (Dht_pin, lower);
Pinmode (Dht_pin, INPUT);
/* Wait for the sensor to return a high two signal////* When the high-low level signal any one can not find, return true, will restart this function * * * wait for the signal/* Waitingcounter = 10000;
while (Digitalread (Dht_pin)!= high) {if (waitingcounter--= = 0) {return true;
}/* Wait for low level signal/waitingcounter = 30000; while (Digitalread (dht_pin)!= low) {if (waitingcounter--= = 0) {return true; /** * FROM distance sensor distance * Parameter: no * return value: Distance value, unit cm, accurate to two decimal/float getdistance () {/* Set pins/Pinmode (Trg_pin, OUTP
UT);
Pinmode (Ech_pin, INPUT);
/* Set two microseconds low level/Digitalwrite (Trg_pin, lower);
Delaymicroseconds (2);
/* Send 10 microseconds High level/digitalwrite (Trg_pin);
Delaymicroseconds (10);
/* Return to High Level */Digitalwrite (trg_pin, low);
/* Get the value of the sensor and calculate the distance * * Pulsein (Ech_pin, high)/58.00;
}