bangnaga

Pengendalian Kadar PH Suhu dan Level Air Tambak

Jun 6th, 2013
108
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // PIN I/O
  2. int pinPH = 0;    
  3. int PinLM35 = 1;
  4. int PinPing = 7;
  5. int PinPWM = 9;        
  6. int PinValve = 13;        
  7. int PinMotor = 12;
  8.  
  9. int PinMotor1PH = 5;    
  10. int PinMotor2PH = 6;    
  11.  
  12.  
  13. // Deklarasi Variabel sensor PH
  14. float phSense = 0;
  15. float refvoltage = 3.5;
  16. boolean jampagi = false;
  17.  
  18. //Deklarasi  sensor temperature
  19. float temperature;
  20.  
  21.  
  22. int sensorSuhu = 0;         // the sensor value
  23. int sensorMin = 1023;       // minimum sensor value
  24. int sensorMax = 0;          // maximum sensor value
  25. int kalibrasiTemp = 70;
  26.  
  27.  
  28. // Deklarasi Sensor PING
  29. const int hmax = 5; //5 cm
  30. const int tinggisensor = 150;
  31. const int batasketinggian = 70;
  32. int ketinggian;
  33. int buttonState = 11;  
  34.  
  35.  
  36. void setup()
  37. {
  38.   Serial.begin(9600);
  39.  
  40. // Setup Sensor temperature & PWM
  41.   while (millis() < 5000) {
  42.     sensorSuhu = analogRead(PinLM35);
  43.     sensorSuhu = sensorSuhu;
  44.     if (sensorSuhu > sensorMax) {
  45.       sensorMax = sensorSuhu;
  46.     }
  47.     if (sensorSuhu < sensorMin) {
  48.       sensorMin = sensorSuhu;
  49.     }
  50. // Akhir Setup Sensor temperature & PWM
  51.   }
  52. }
  53.  
  54. void loop(){
  55.   buttonState = digitalRead(11);
  56.   if (buttonState == HIGH) {    
  57.     jampagi = true;
  58.     Serial.println("Pagi Jam 8 ");
  59.   }
  60.   else {
  61.     jampagi = false;
  62.     Serial.println("Bukan Pagi ");    
  63.   }  
  64.  
  65.  
  66.   //Baca PH
  67.   float nilaisensorph;
  68.   nilaisensorph = bacaph();
  69.   Serial.print("PH: ");
  70.   Serial.println(nilaisensorph);
  71.  
  72.    if (nilaisensorph < 6) {
  73.       digitalWrite(PinMotor1PH, HIGH); //Nyalakan Motor1
  74.       Serial.println("Nyalakan Motor1-PH");      
  75.    }
  76.    if (nilaisensorph >= 7) {
  77.       digitalWrite(PinMotor1PH, LOW); //Nyalakan Motor1
  78.       Serial.println("Matikan Motor1-PH");      
  79.    }
  80.  
  81.    if (nilaisensorph > 8) {
  82.       digitalWrite(PinMotor2PH, HIGH); //Nyalakan Motor2
  83.       Serial.println("Nyalakan Motor2-PH");      
  84.    }
  85.    if (nilaisensorph <= 7) {
  86.       digitalWrite(PinMotor2PH, LOW); //Nyalakan Motor2
  87.       Serial.println("Matikan Motor2-PH");      
  88.    }
  89.  
  90.  
  91.  
  92.   //Baca temperature
  93.   float nilaitemperature;
  94.   nilaitemperature = BacatemperaturePWM();
  95.  
  96.   //Baca Ketinggian Air Asusmsi ketinggian sensor ke permukaan 100 cm
  97.   int jarak;
  98.   jarak = bacajarak();
  99.   Serial.print("Ketinggian: ");
  100.   Serial.println(jarak);
  101.   //delay (500);
  102.  
  103.   //Cek Jarak jika kurang dari batas ketinggian  
  104.   if (jarak < batasketinggian) {
  105.       digitalWrite(PinMotor, HIGH); //Nyalakan Motor
  106.       Serial.println("Nyalakan Motor");      
  107.  
  108.     }
  109. //Akhir Cek Jarak jika kurang dari batas ketinggian      
  110.  
  111. //If Jarak
  112.   if (jarak > 100) {
  113.       digitalWrite(PinMotor, LOW); //Matikan Motor
  114.       Serial.println("Matikan Motor");
  115.    }
  116.  
  117. //Cek Jam
  118.   if (jampagi == true) {  // Jika Jam 8
  119.     digitalWrite(PinValve,HIGH);  //Buka Valve
  120.     Serial.println("Buka Valve");
  121.   }
  122.    if (jarak < batasketinggian) {
  123.       digitalWrite(PinValve, LOW); //Tutup Valve
  124.       Serial.println("Tutup Valve");      
  125.      // jampagi == false;
  126.   }
  127.  // Akhir Cek Jam
  128. Serial.println("============================================");    
  129.  delay(1000);
  130. }
  131.  
  132. float bacaph()
  133.  {
  134.   float result;
  135.   int samples = 20;
  136.   int aRead = 0;  
  137.   for (int i = 0; i < samples ; i++)
  138.   {
  139.     aRead += analogRead(pinPH);
  140.   }
  141.   float voltage = 5.0 * aRead/ (1023 * samples);  // assuming 5V reference
  142.   phSense = 14 - voltage/0.25;
  143.   result = phSense;
  144.   return result;
  145.   }
  146.  
  147. int bacajarak(){
  148.   int result;
  149.   long duration, cm;
  150.   pinMode(PinPing, OUTPUT);
  151.   digitalWrite(PinPing, LOW);
  152.   delayMicroseconds(2);
  153.   digitalWrite(PinPing, HIGH);
  154.   delayMicroseconds(5);
  155.   digitalWrite(PinPing, LOW);
  156.   pinMode(PinPing, INPUT);
  157.   duration = pulseIn(PinPing, HIGH);
  158.   cm = mmtocm(duration);
  159.   ketinggian = tinggisensor - cm;
  160.   result = ketinggian;
  161. }  
  162.  
  163. long mmtocm(long microseconds)
  164. {
  165.   return microseconds / 29 / 2;
  166. }
  167.  
  168.  
  169.  
  170.  
  171.  
  172. float BacatemperaturePWM(){
  173.   float temperature;
  174.   temperature = analogRead(PinLM35); //read the value from the sensor
  175.   temperature = (5.0 * temperature * 100.0)/1024.0; //convert the analog data to temperature in Celcius
  176.   sensorSuhu = analogRead(PinLM35);
  177.   sensorSuhu = map(sensorSuhu, sensorMin, kalibrasiTemp, 0, 255);
  178.   sensorSuhu = constrain(sensorSuhu, 0, 255);
  179.   analogWrite(PinPWM, sensorSuhu);
  180.  
  181.   Serial.print("Suhu: " );  
  182.   Serial.println(temperature);    
  183.   Serial.print("PWM: " );    
  184.   Serial.println(sensorSuhu);  
  185.  return temperature;  
  186. }
  187.  
  188.  
  189.  
  190.  
  191. //MOTOR ke-1: Jika jam 08:00 maka nyalakan Motor1 untuk membuang air tambak sebanyak sampai ketinggian air mencapai 70 cm (Asumsi ketinggian max air 100 cm)
  192. //            Motor ke-1 akan mati secara otomatis jika ketinggian air mencapai 70 cm
  193. //MOTOR ke-2: Jika ketinggian air <70 untuk mengisi tambak dengan air baru
  194. //            Motor ke-2 akan mati ketika air mencapai ketinggian 100 cm
RAW Paste Data