Advertisement
Guest User

Moisture_LED

a guest
Sep 30th, 2014
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.91 KB | None | 0 0
  1.  
  2. #include <LiquidCrystal.h>
  3. #include <math.h>
  4.  
  5. /* initialize the library with the numbers of the interface pins
  6. changed interface 13 ->12 and 12->11 */
  7.  
  8. LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
  9.  
  10. // define Ohm character
  11. byte ohm[8] = {
  12. B00000,
  13. B01110,
  14. B10001,
  15. B10001,
  16. B10001,
  17. B01010,
  18. B11011,
  19. B00000,
  20. };
  21.  
  22. // These constants won't change. They're used to give names
  23. // to the pins used:
  24. const int analogInPin1 = A0; // Analog input pin that the gypsum sensor is attached to
  25. const int analogInPin2 = A1; // Analog input pin that the gypsum sensor is attached to
  26.  
  27. // NO A1 FOR SENSOR?
  28.  
  29.  
  30. int NUM_READS = 10;
  31. int sensorvalue3 = 0; // value read from gypsum sensor
  32. int sensorvalue4 = 0; // value read from sensor supply
  33. int sensorvoltage2 = 0;
  34. int supplyvoltage2 = 0;
  35. int unsigned long Rsense1 = 0;
  36. long Rsense2 = 0;
  37. int unsigned long avsensorvalue3 = 0;
  38. int unsigned long avsensorvalue4 = 0;
  39. int B = 0;
  40. int unsigned long moisture_pct = 0;
  41.  
  42. void setup() {
  43. // initialize serial communications at 9600 bps:
  44. Serial.begin(9600);
  45.  
  46. // set up the LCD's number of columns and rows:
  47. lcd.createChar(1, ohm);
  48. lcd.begin(16, 2);
  49. lcd.print ("Amanduino!");
  50.  
  51.  
  52. // initialize the digital pin as an output.
  53. // Pin 6 is sense resistor voltage supply 1
  54. pinMode(6, OUTPUT);
  55.  
  56. // initialize the digital pin as an output.
  57. // Pin 7 is sense resistor voltage supply 2
  58. pinMode(7, OUTPUT);
  59.  
  60. // initialize the digital pin as an output.
  61. // Pin 8 is sense resistor voltage supply 2
  62. pinMode(8, OUTPUT);
  63.  
  64. // initialize the digital pin as an output.
  65. // Pin 9 is sense resistor voltage supply 2
  66. pinMode(9, OUTPUT);
  67.  
  68. delay(500);
  69. }
  70.  
  71. void loop() {
  72.  
  73. // read sensor, average, and calculate resistance value
  74.  
  75. // Noise filter
  76. // averaging filter
  77.  
  78. avsensorvalue3 = 0;
  79. avsensorvalue4 = 0;
  80.  
  81. for (B=0; B< NUM_READS; B++) {
  82.  
  83. // Read input 1 values
  84.  
  85. digitalWrite(7, HIGH); // set the voltage supply on
  86.  
  87. delay(10);
  88.  
  89. // read the analog in value:
  90. sensorvalue4 = analogRead(analogInPin2);
  91.  
  92. //delay(100);
  93.  
  94. digitalWrite(7, LOW); // set the voltage supply off
  95.  
  96. delay(10);
  97.  
  98. digitalWrite(6, HIGH); // set the voltage supply on
  99.  
  100. delay(10);
  101.  
  102. // read the analog in value:
  103. sensorvalue3 = analogRead(analogInPin2);
  104.  
  105. digitalWrite(6, LOW); // set the voltage supply off
  106.  
  107.  
  108. avsensorvalue4 = avsensorvalue4 + sensorvalue4;
  109. avsensorvalue3 = avsensorvalue3 + sensorvalue3;
  110.  
  111. }
  112.  
  113. // end of multiple read loop
  114.  
  115. // average all samples out
  116.  
  117. avsensorvalue4 = avsensorvalue4 / NUM_READS;
  118. avsensorvalue3 = avsensorvalue3 / NUM_READS;
  119.  
  120.  
  121. float sensorvoltage2 = avsensorvalue3 * 4.88 / 1023.0;
  122.  
  123. float supplyvoltage2 = avsensorvalue4 * 4.88 / 1023.0;
  124. // if (sensorvalue3 < 10)
  125. // {
  126. // Rsense2= 0;
  127. // }
  128. // else
  129. // {
  130. Rsense2 = (1200 / sensorvoltage2) * (supplyvoltage2 - sensorvoltage2);
  131. // }
  132.  
  133. moisture_pct = pow(float(Rsense2/31.65),float (1/-1.695))*400;
  134. if (moisture_pct > 100)
  135. {
  136. moisture_pct =100;
  137. }
  138. else
  139. {moisture_pct = moisture_pct;
  140. }
  141.  
  142. Serial.print("sensor resistance = ");
  143. Serial.println(Rsense2);
  144.  
  145. // set the cursor to column 0, line 0
  146. lcd.setCursor(0, 0);
  147.  
  148. //Clear the LCD
  149. lcd.print("sensor = ");
  150. lcd.setCursor(0, 1);
  151. lcd.print("moisture: ");
  152.  
  153. // set the cursor to column 0, line 0
  154. lcd.setCursor(0, 0);
  155. lcd.print("Moisture: ");
  156. lcd.print(moisture_pct);
  157. lcd.print(" %");
  158.  
  159. // set the cursor to column 0, line 1
  160. lcd.setCursor(0, 1);
  161. // Print a message to the LCD.
  162. lcd.print("Sensor: " );
  163. lcd.print(Rsense2);
  164. lcd.print(" ");
  165. lcd.write(1);
  166.  
  167.  
  168. // delay until next measurement (msec)
  169.  
  170. delay(5000);
  171.  
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement