Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <LiquidCrystal.h>
- LiquidCrystal lcd(8, 9, 4, 5, 6, 7); // the pins used on the LCD panel
- /************************Hardware Related Macros************************************/
- #define MG_PIN A1 //CO2 sensor Analog output to Arduino Analog Input 0
- #define DC_GAIN 8.5 //define the DC gain of amplifier
- #define SensorPin A0 //pH meter Analog output to Arduino Analog Input 0
- /***********************Software Related Macros************************************/
- #define READ_SAMPLE_INTERVAL 50 //define how many samples you are going to take in normal operation
- #define READ_SAMPLE_TIMES 5 //define the time interval(in milisecond) between each samples in
- //normal operation
- #define samplingInterval 20
- #define printInterval 60000 //print every 60000ms
- #define ArrayLenth 40 //times of collection
- /**********************Application Related Macros**********************************/
- //These two values differ from sensor to sensor. user should derermine this value.
- #define V400 (705) //measure the CO2 in the atmosphere(approximate 400ppm)
- #define V40000 (260) //measure 100% CO2
- #define Offset 0.07 //pH meter deviation compensate
- /*****************************Globals***********************************************/
- float slope=(V40000-V400)/(4.602-2.602); //calculate the correction line
- //log40000=4.602
- //log400=2.602
- int pHArray[ArrayLenth]; //Store the average value of the sensor feedback
- int pHArrayIndex=0;
- void setup()
- {
- Serial.begin(9600);
- lcd.begin(16, 2); // start the library
- lcd.setCursor(0,0); //set the cursor in the LCD to (0,0)
- }
- void loop()
- {
- int percentage;
- float analogValue;
- float volts;
- static unsigned long samplingTime = millis();
- static unsigned long printTime = millis();
- static float pHValue,voltage;
- if(millis()-samplingTime > samplingInterval)
- {
- pHArray[pHArrayIndex++]=analogRead(SensorPin);
- if(pHArrayIndex==ArrayLenth)pHArrayIndex=0;
- voltage = avergearray(pHArray, ArrayLenth)*5.0/1024;
- pHValue = 3.5*voltage+Offset;
- samplingTime=millis();
- }
- analogValue = MGRead(MG_PIN);
- volts=analogValue*5/1024;
- percentage = MGGetPercentage(analogValue);
- if ((millis() - printTime > printInterval)){
- /******print CO2 value in serial******/
- Serial.print(analogValue);
- Serial.print( "\t" );
- Serial.print(volts);
- Serial.print( "V" );
- Serial.print( "\t" );
- Serial.print("CO2:");
- Serial.print(percentage);
- Serial.print( "ppm" );
- Serial.print( "\t" );
- /******print CO2 value in LCD******/
- lcd.setCursor(0,0);
- lcd.print("CO2:");
- lcd.print(percentage);
- lcd.setCursor(10,0);
- lcd.print( "ppm" );
- /*****print pH value on serial******/
- Serial.print( "Time point:" );
- Serial.print(millis());
- Serial.print( "\t" );
- Serial.print(" pH value: ");
- Serial.println(pHValue,4);
- Serial.print("\n");
- /*****print pH value on lcd******/
- lcd.setCursor(0,1);
- lcd.print("pH value: ");
- lcd.println(pHValue,4);
- printTime=millis();
- }
- }
- double avergearray(int* arr, int number){
- int i;
- int max,min;
- double avg;
- long amount=0;
- if(number<=0){
- Serial.println("Error number for the array to avraging!/n");
- return 0;
- }
- if(number<5){ //less than 5, calculated directly statistics
- for(i=0;i<number;i++){
- amount+=arr[i];
- }
- avg = amount/number;
- return avg;
- }else{
- if(arr[0]<arr[1]){
- min = arr[0];max=arr[1];
- }
- else{
- min=arr[1];max=arr[0];
- }
- for(i=2;i<number;i++){
- if(arr[i]<min){
- amount+=min; //arr<min
- min=arr[i];
- }else {
- if(arr[i]>max){
- amount+=max; //arr>max
- max=arr[i];
- }else{
- amount+=arr[i]; //min<=arr<=max
- }
- }//if
- }//for
- avg = (double)amount/(number-2);
- }//if
- return avg;
- delay(1000);
- }
- /***************************** MGRead *********************************************
- Input: mg_pin - analog channel
- Output: output of SEN-000007
- Remarks: This function reads the output of SEN-000007
- ************************************************************************************/
- float MGRead(int mg_pin)
- {
- int i;
- float v=0;
- for (i=0;i<READ_SAMPLE_TIMES;i++) {
- v += analogRead(mg_pin);
- delay(READ_SAMPLE_INTERVAL);
- }
- v =v/READ_SAMPLE_TIMES ;
- return v;
- }
- /***************************** MQGetPercentage **********************************
- the function which turns the analog value to the CO2 concentration
- ************************************************************************************/
- int MGGetPercentage(float analogValue)
- {
- float logConc=2.602 +(analogValue-V400)/slope; //calculate the log of CO2 concentration
- return pow(10,logConc);
- }
Add Comment
Please, Sign In to add comment