bangnaga

Arduino Timbangan Seleksi Ukuran

Oct 25th, 2020 (edited)
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.74 KB | None | 0 0
  1. // UCOK --------- 25 Oktober 2020
  2. //
  3. int maxval = 0;
  4. uint32_t lastSample = 0;
  5.  
  6. int period = 1000;
  7. unsigned long time_now = 0;
  8. boolean kecil_selesai;
  9. boolean sedang_selesai;
  10. boolean besar_selesai;
  11.  
  12. #include <Servo.h>
  13. Servo myservo;
  14. Servo myservo2;
  15. float databaru;
  16. float dataMax;
  17. float dataMin;
  18. #include <HX711_ADC.h>
  19. #include <EEPROM.h>
  20.  
  21. int statusalat =0; //0 = Kosong, 1 = Kecil, 2 = Sedang, 3= Besar
  22. const int HX711_dout = A4; //mcu > HX711 dout pin
  23. const int HX711_sck = A5; //mcu > HX711 sck pin
  24.  
  25. //HX711 constructor:
  26. HX711_ADC LoadCell(HX711_dout, HX711_sck);
  27.  
  28. const int calVal_eepromAdress = 0;
  29. long t;
  30.  
  31. boolean bol_terseleksi;
  32. float berat_terakhir =0;
  33. float berat_sekarang =0;
  34.  
  35. /*define the sensor's pins*/
  36. uint8_t trigPin = 3;
  37. uint8_t echoPin = 2;
  38.  
  39. //pinMode(12, INPUT);
  40. //pinMode(11, INPUT);
  41.  
  42. unsigned long timerStart = 0;
  43. int TIMER_TRIGGER_HIGH = 10;
  44. int TIMER_LOW_HIGH = 2;
  45.  
  46. float timeDuration, distance;
  47.  
  48. float tinggipermukaan = 10.50;
  49. /*The states of an ultrasonic sensor*/
  50. enum SensorStates {
  51.   TRIG_LOW,
  52.   TRIG_HIGH,
  53.   ECHO_HIGH
  54. };
  55.  
  56. SensorStates _sensorState = TRIG_LOW;
  57.  
  58. void startTimer() {
  59.   timerStart = millis();
  60. }
  61.  
  62. bool isTimerReady(int mSec) {
  63.   return (millis() - timerStart) < mSec;
  64. }
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72. void setup() {
  73.   sedang_selesai==true;
  74.   besar_selesai==true;
  75.   Serial.begin(57600); delay(10);
  76.   Serial.println();
  77.   Serial.println("Starting...");
  78.  
  79.   myservo.attach(10);
  80.   myservo2.attach(9);
  81.   pinMode(11, INPUT);// set pin as input
  82.   pinMode(12, INPUT);// set pin as input
  83.   pinMode(trigPin, OUTPUT);
  84.   pinMode(echoPin, INPUT);
  85.  
  86.   tutupservo1();
  87.   tutupservo2();
  88.   delay (1000);
  89.  
  90.   bukaservo1();
  91.   bukaservo2();
  92.   delay(1000);
  93.  
  94.  
  95.   LoadCell.begin();
  96.   float calibrationValue; // calibration value (see example file "Calibration.ino")
  97.   calibrationValue = 262.64; // uncomment this if you want to set the calibration value in the sketch
  98. #if defined(ESP8266)|| defined(ESP32)
  99.   //EEPROM.begin(512); // uncomment this if you use ESP8266/ESP32 and want to fetch the calibration value from eeprom
  100. #endif
  101.   EEPROM.get(calVal_eepromAdress, calibrationValue); // uncomment this if you want to fetch the calibration value from eeprom
  102.  
  103.   long stabilizingtime = 500; // preciscion right after power-up can be improved by adding a few seconds of stabilizing time
  104.   boolean _tare = false; //set this to false if you don't want tare to be performed in the next step
  105.   LoadCell.start(stabilizingtime, _tare);
  106.   if (LoadCell.getTareTimeoutFlag()) {
  107.     Serial.println("Timeout, check MCU>HX711 wiring and pin designations");
  108.     while (1);
  109.   }
  110.   else {
  111.     LoadCell.setCalFactor(calibrationValue); // set calibration value (float)
  112.     Serial.println("Startup is complete");
  113.   }
  114.   LoadCell.tareNoDelay();
  115. }
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123. void loop() {
  124.  
  125.  
  126.  
  127.  
  128.  
  129.   static boolean newDataReady = 0;
  130.   const int serialPrintInterval = 500; //increase value to slow down serial print activity
  131.  
  132.   // check for new data/start next conversion:
  133.   if (LoadCell.update()) newDataReady = true;
  134.  
  135.   // get smoothed value from the dataset:
  136.   if (newDataReady) {
  137.     if (millis() > t + serialPrintInterval) {
  138.       float i = LoadCell.getData();
  139.      // Serial.print("Berat: ");
  140.     //  Serial.println(i);
  141.       newDataReady = 0;
  142.       berat_sekarang = i;
  143.       Serial.print("Berat Sekarang: ");
  144.       Serial.println(i);
  145.       t = millis();
  146.  
  147. //============================================================================
  148.    int detect1 = digitalRead(11);
  149.    if(detect1 == LOW){
  150.       bukaservo1();
  151.       bukaservo2();
  152.       Serial.print("Mulai Menimbang ");
  153.       dataMax = berat_sekarang;
  154.       Serial.print("Berat Awal: ");
  155.       Serial.print(berat_sekarang);
  156.    }
  157.    
  158.    int detect2 = digitalRead(12);
  159.    if(detect2 == LOW){
  160.      
  161.      
  162.       if (berat_sekarang > dataMax) {
  163.           dataMax = berat_sekarang;
  164.       }else{
  165.           dataMax = dataMax;
  166.       }
  167.       Serial.println("Selesai Menimbang");
  168.       Serial.print("Berat Max :");
  169.       Serial.println(dataMax);
  170.  
  171.    
  172.  
  173.    
  174.        
  175.      
  176.           if ((dataMax > 40) && (dataMax< 100)){
  177.             Serial.print ("Sedang: ");
  178.             tutupservo2();
  179.             dataMax = 0;
  180.           }
  181.           if (dataMax > 100){
  182.             Serial.print ("Besar ");
  183.             tutupservo1();
  184.             dataMax = 0;
  185.           }
  186.        
  187.        
  188.  }
  189.  
  190.    
  191.    
  192.        
  193.  
  194.      
  195.    
  196.      
  197.    }  
  198.   // Akhir bagian timbangan ====================================================
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.    
  208.  
  209.    
  210.    
  211.  
  212. // Akhir sensor Infra Red =======================================================
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.       // Cek Serial Command
  226.       if (Serial.available() > 0) {
  227.         float i;
  228.         char inByte = Serial.read();
  229.         if (inByte == 't') LoadCell.tareNoDelay();
  230.         if (inByte == 'a') bukaservo1();
  231.         if (inByte == 'b') tutupservo1();
  232.         if (inByte == 'c') bukaservo2();
  233.         if (inByte == 'd') tutupservo1();
  234.       }
  235.    
  236.      
  237.       if (LoadCell.getTareStatus() == true) {
  238.         Serial.println("Proses Kalibrasi Selesai");
  239.       }
  240.  
  241.   } // Akhir Pembacaan Timbangan
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250. }
  251. // Akhir Void Loop =============================
  252.  
  253.  
  254.  
  255.  
  256. void bukaservo1(){
  257.   myservo.write(100);
  258.   Serial.println ("Buka Servo_1");
  259. }
  260.  
  261.  
  262. void tutupservo1(){
  263.   myservo.write(180);
  264.   Serial.println ("Tutup Servo_1");
  265.  // delay(3000);
  266.  // myservo.write(100);
  267.   //besar_selesai= true;
  268.   //sedang_selesai= true;
  269. }
  270.  
  271. void bukaservo2(){
  272.   myservo2.write(100);
  273.   Serial.println ("Buka Servo_2");
  274. }
  275.  
  276. void tutupservo2(){
  277.   myservo2.write(180);
  278.   Serial.println ("Tutup Servo_2");
  279.  // delay(3000);
  280.  // myservo2.write(100);
  281.  // sedang_selesai= true;
  282.  //besar_selesai= true;
  283. }
Advertisement
Add Comment
Please, Sign In to add comment