Guest User

Untitled

a guest
Aug 20th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.21 KB | None | 0 0
  1. #include <LiquidCrystal.h>
  2.  
  3. LiquidCrystal lcd(8, 9, 4, 5, 6, 7); // the pins used on the LCD panel
  4.  
  5. /************************Hardware Related Macros************************************/
  6. #define MG_PIN A1 //CO2 sensor Analog output to Arduino Analog Input 0
  7. #define DC_GAIN 8.5 //define the DC gain of amplifier
  8. #define SensorPin A0 //pH meter Analog output to Arduino Analog Input 0
  9.  
  10.  
  11. /***********************Software Related Macros************************************/
  12. #define READ_SAMPLE_INTERVAL 50 //define how many samples you are going to take in normal operation
  13. #define READ_SAMPLE_TIMES 5 //define the time interval(in milisecond) between each samples in
  14. //normal operation
  15.  
  16. #define samplingInterval 20
  17. #define printInterval 60000 //print every 60000ms
  18. #define ArrayLenth 40 //times of collection
  19.  
  20.  
  21. /**********************Application Related Macros**********************************/
  22. //These two values differ from sensor to sensor. user should derermine this value.
  23. #define V400 (705) //measure the CO2 in the atmosphere(approximate 400ppm)
  24. #define V40000 (260) //measure 100% CO2
  25.  
  26. #define Offset 0.07 //pH meter deviation compensate
  27.  
  28. /*****************************Globals***********************************************/
  29. float slope=(V40000-V400)/(4.602-2.602); //calculate the correction line
  30. //log40000=4.602
  31. //log400=2.602
  32.  
  33. int pHArray[ArrayLenth]; //Store the average value of the sensor feedback
  34. int pHArrayIndex=0;
  35.  
  36. void setup()
  37. {
  38. Serial.begin(9600);
  39. lcd.begin(16, 2); // start the library
  40. lcd.setCursor(0,0); //set the cursor in the LCD to (0,0)
  41. }
  42.  
  43.  
  44. void loop()
  45. {
  46. int percentage;
  47. float analogValue;
  48. float volts;
  49.  
  50. static unsigned long samplingTime = millis();
  51. static unsigned long printTime = millis();
  52. static float pHValue,voltage;
  53. if(millis()-samplingTime > samplingInterval)
  54. {
  55. pHArray[pHArrayIndex++]=analogRead(SensorPin);
  56. if(pHArrayIndex==ArrayLenth)pHArrayIndex=0;
  57. voltage = avergearray(pHArray, ArrayLenth)*5.0/1024;
  58. pHValue = 3.5*voltage+Offset;
  59. samplingTime=millis();
  60. }
  61. analogValue = MGRead(MG_PIN);
  62. volts=analogValue*5/1024;
  63. percentage = MGGetPercentage(analogValue);
  64. if ((millis() - printTime > printInterval)){
  65.  
  66. /******print CO2 value in serial******/
  67. Serial.print(analogValue);
  68. Serial.print( "\t" );
  69. Serial.print(volts);
  70. Serial.print( "V" );
  71. Serial.print( "\t" );
  72. Serial.print("CO2:");
  73. Serial.print(percentage);
  74. Serial.print( "ppm" );
  75. Serial.print( "\t" );
  76.  
  77. /******print CO2 value in LCD******/
  78. lcd.setCursor(0,0);
  79. lcd.print("CO2:");
  80. lcd.print(percentage);
  81. lcd.setCursor(10,0);
  82. lcd.print( "ppm" );
  83.  
  84. /*****print pH value on serial******/
  85. Serial.print( "Time point:" );
  86. Serial.print(millis());
  87. Serial.print( "\t" );
  88. Serial.print(" pH value: ");
  89. Serial.println(pHValue,4);
  90. Serial.print("\n");
  91.  
  92. /*****print pH value on lcd******/
  93. lcd.setCursor(0,1);
  94. lcd.print("pH value: ");
  95. lcd.println(pHValue,4);
  96.  
  97. printTime=millis();
  98. }
  99. }
  100.  
  101. double avergearray(int* arr, int number){
  102. int i;
  103. int max,min;
  104. double avg;
  105. long amount=0;
  106. if(number<=0){
  107. Serial.println("Error number for the array to avraging!/n");
  108. return 0;
  109. }
  110. if(number<5){ //less than 5, calculated directly statistics
  111. for(i=0;i<number;i++){
  112. amount+=arr[i];
  113. }
  114. avg = amount/number;
  115. return avg;
  116. }else{
  117. if(arr[0]<arr[1]){
  118. min = arr[0];max=arr[1];
  119. }
  120. else{
  121. min=arr[1];max=arr[0];
  122. }
  123. for(i=2;i<number;i++){
  124. if(arr[i]<min){
  125. amount+=min; //arr<min
  126. min=arr[i];
  127. }else {
  128. if(arr[i]>max){
  129. amount+=max; //arr>max
  130. max=arr[i];
  131. }else{
  132. amount+=arr[i]; //min<=arr<=max
  133. }
  134. }//if
  135. }//for
  136. avg = (double)amount/(number-2);
  137. }//if
  138. return avg;
  139. delay(1000);
  140. }
  141.  
  142. /***************************** MGRead *********************************************
  143. Input: mg_pin - analog channel
  144. Output: output of SEN-000007
  145. Remarks: This function reads the output of SEN-000007
  146. ************************************************************************************/
  147. float MGRead(int mg_pin)
  148. {
  149. int i;
  150. float v=0;
  151.  
  152. for (i=0;i<READ_SAMPLE_TIMES;i++) {
  153. v += analogRead(mg_pin);
  154. delay(READ_SAMPLE_INTERVAL);
  155. }
  156. v =v/READ_SAMPLE_TIMES ;
  157. return v;
  158. }
  159.  
  160. /***************************** MQGetPercentage **********************************
  161. the function which turns the analog value to the CO2 concentration
  162. ************************************************************************************/
  163. int MGGetPercentage(float analogValue)
  164. {
  165. float logConc=2.602 +(analogValue-V400)/slope; //calculate the log of CO2 concentration
  166. return pow(10,logConc);
  167. }
Add Comment
Please, Sign In to add comment