bangnaga icon

Arduino Timbangan Seleksi Ukuran

bangnaga | PRO | 06/23/25 06:35:01 AM UTC (Edited) | 0 ⭐ | 172 👁️ | Never ⏰ | []
C |

5.74 KB

|

None

|

0 👍

/

0 👎

// UCOK --------- 25 Oktober 2020
//
int maxval = 0;
uint32_t lastSample = 0;
 
int period = 1000;
unsigned long time_now = 0;
boolean kecil_selesai;
boolean sedang_selesai;
boolean besar_selesai;
 
#include <Servo.h>
Servo myservo;
Servo myservo2;
float databaru;
float dataMax;
float dataMin;
#include <HX711_ADC.h>
#include <EEPROM.h>
 
int statusalat =0; //0 = Kosong, 1 = Kecil, 2 = Sedang, 3= Besar
const int HX711_dout = A4; //mcu > HX711 dout pin
const int HX711_sck = A5; //mcu > HX711 sck pin
 
//HX711 constructor:
HX711_ADC LoadCell(HX711_dout, HX711_sck);
 
const int calVal_eepromAdress = 0;
long t;
 
boolean bol_terseleksi;
float berat_terakhir =0;
float berat_sekarang =0;
 
/*define the sensor's pins*/
uint8_t trigPin = 3;
uint8_t echoPin = 2;
 
//pinMode(12, INPUT);
//pinMode(11, INPUT);
 
unsigned long timerStart = 0;
int TIMER_TRIGGER_HIGH = 10;
int TIMER_LOW_HIGH = 2;
 
float timeDuration, distance;
 
float tinggipermukaan = 10.50;
/*The states of an ultrasonic sensor*/
enum SensorStates {
  TRIG_LOW,
  TRIG_HIGH,
  ECHO_HIGH
};
 
SensorStates _sensorState = TRIG_LOW;
 
void startTimer() {
  timerStart = millis();
}
 
bool isTimerReady(int mSec) {
  return (millis() - timerStart) < mSec;
}
 
 
 
 
 
 
 
void setup() {
  sedang_selesai==true;
  besar_selesai==true;
  Serial.begin(57600); delay(10);
  Serial.println();
  Serial.println("Starting...");
  
  myservo.attach(10);
  myservo2.attach(9);
  pinMode(11, INPUT);// set pin as input
  pinMode(12, INPUT);// set pin as input
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
 
  tutupservo1();
  tutupservo2();
  delay (1000);
  
  bukaservo1();
  bukaservo2();
  delay(1000);
 
  
  LoadCell.begin();
  float calibrationValue; // calibration value (see example file "Calibration.ino")
  calibrationValue = 262.64; // uncomment this if you want to set the calibration value in the sketch
#if defined(ESP8266)|| defined(ESP32)
  //EEPROM.begin(512); // uncomment this if you use ESP8266/ESP32 and want to fetch the calibration value from eeprom
#endif
  EEPROM.get(calVal_eepromAdress, calibrationValue); // uncomment this if you want to fetch the calibration value from eeprom
 
  long stabilizingtime = 500; // preciscion right after power-up can be improved by adding a few seconds of stabilizing time
  boolean _tare = false; //set this to false if you don't want tare to be performed in the next step
  LoadCell.start(stabilizingtime, _tare);
  if (LoadCell.getTareTimeoutFlag()) {
    Serial.println("Timeout, check MCU>HX711 wiring and pin designations");
    while (1);
  }
  else {
    LoadCell.setCalFactor(calibrationValue); // set calibration value (float)
    Serial.println("Startup is complete");
  }
  LoadCell.tareNoDelay();
}
 
 
 
 
 
 
 
void loop() {
 
 
 
 
  
  static boolean newDataReady = 0;
  const int serialPrintInterval = 500; //increase value to slow down serial print activity
 
  // check for new data/start next conversion:
  if (LoadCell.update()) newDataReady = true;
 
  // get smoothed value from the dataset:
  if (newDataReady) {
    if (millis() > t + serialPrintInterval) {
      float i = LoadCell.getData();
     // Serial.print("Berat: ");
    //  Serial.println(i);
      newDataReady = 0;
      berat_sekarang = i;
      Serial.print("Berat Sekarang: "); 
      Serial.println(i);
      t = millis();
 
//============================================================================
   int detect1 = digitalRead(11);
   if(detect1 == LOW){
      bukaservo1();
      bukaservo2();
      Serial.print("Mulai Menimbang "); 
      dataMax = berat_sekarang; 
      Serial.print("Berat Awal: "); 
      Serial.print(berat_sekarang); 
   }
   
   int detect2 = digitalRead(12); 
   if(detect2 == LOW){
     
      
      if (berat_sekarang > dataMax) {
          dataMax = berat_sekarang;
      }else{
          dataMax = dataMax;
      }
      Serial.println("Selesai Menimbang"); 
      Serial.print("Berat Max :"); 
      Serial.println(dataMax); 
  
    
 
   
       
      
          if ((dataMax > 40) && (dataMax< 100)){
            Serial.print ("Sedang: ");
            tutupservo2();
            dataMax = 0;
          }
          if (dataMax > 100){
            Serial.print ("Besar ");
            tutupservo1();
            dataMax = 0;
          }
       
        
 }
 
   
   
        
 
     
    
     
   }   
  // Akhir bagian timbangan ====================================================
 
 
  
 
 
 
  
 
   
 
   
    
 
// Akhir sensor Infra Red =======================================================
 
 
 
 
 
 
 
 
 
 
  
 
      // Cek Serial Command
      if (Serial.available() > 0) {
        float i;
        char inByte = Serial.read();
        if (inByte == 't') LoadCell.tareNoDelay();
        if (inByte == 'a') bukaservo1();
        if (inByte == 'b') tutupservo1();
        if (inByte == 'c') bukaservo2();
        if (inByte == 'd') tutupservo1();
      }
    
      
      if (LoadCell.getTareStatus() == true) {
        Serial.println("Proses Kalibrasi Selesai");
      }
 
  } // Akhir Pembacaan Timbangan
 
 
 
 
 
 
  
  
}
// Akhir Void Loop =============================
 
 
 
 
void bukaservo1(){
  myservo.write(100);
  Serial.println ("Buka Servo_1");
}
 
 
void tutupservo1(){
  myservo.write(180);
  Serial.println ("Tutup Servo_1");
 // delay(3000);
 // myservo.write(100);
  //besar_selesai= true;
  //sedang_selesai= true;
}
 
void bukaservo2(){
  myservo2.write(100);
  Serial.println ("Buka Servo_2");
}
 
void tutupservo2(){
  myservo2.write(180);
  Serial.println ("Tutup Servo_2");
 // delay(3000);
 // myservo2.write(100);
 // sedang_selesai= true;
 //besar_selesai= true;
}

Comments