Berikut adalah pautan kepada produk :

>> Sensor Jarak Ultrasonik HC-SR04 <<

Fungsi penguji sensor jarak ultrasonik HC-SR04 adalah untuk menguji sensor sama ada ianya berfugsi dengan baik atau rosak.

Penguji tanpa sensor jarak

Penguji dengan sensor

Di atas adalah gambar penguji sensor jarak ultrasonik HC-SR04, sensor akan disambungkan kepada penguji untuk diuji fungsinya.

Penguji ini mempunyai kemampuan untuk :
+ Mengimbas jarak objek di hadapan sensor jarak ultrasonik HC-SR04
+ Paparkan output pada LCD di atas penguji

Video di bawah menunjukkan bagaimana sensor diuji :

Cara Sambungan Litar :

Kod Punguji :

///////////////////////////////////////
// Title : Ultrasonic Sensor Test Jig
//
// Main programmer : Muhammad Ilyasaa @ Nadi Eleczone Solutions
//
// Description : Ultrasonic Sensor Test Jig
//
///////////////////////////////////////

///////////////////////////////////////
// Define
///////////////////////////////////////
#include <NewPing.h>
#include <LiquidCrystal.h>
///////////////////////////////////////

///////////////////////////////////////
// Constant
///////////////////////////////////////
#define SONAR_NUM     2 // Number or sensors.
#define MAX_DISTANCE 50 // Maximum distance (in cm) to ping.
#define PING_INTERVAL 33 // Milliseconds between sensor pings (29ms is about the min to avoid cross-sensor echo).
///////////////////////////////////////

///////////////////////////////////////
// Constructor
///////////////////////////////////////
LiquidCrystal lcd(A4, A5, A0, A1, A2, A3);
NewPing sonar[SONAR_NUM] = {     // Sensor object array, Each sensor's trigger pin, echo pin, and max distance to ping.
  NewPing(5, 4, MAX_DISTANCE),  //Sensor number 3
  NewPing(9, 8, MAX_DISTANCE)  //Sensor number 4
};
///////////////////////////////////////

///////////////////////////////////////
// Variable
///////////////////////////////////////
unsigned long pingTimer[SONAR_NUM]; // Holds the times when the next ping should happen for each sensor.
unsigned int cm[SONAR_NUM];         // Where the ping distances are stored.
uint8_t currentSensor = 0;          // Keeps track of which sensor is active.
///////////////////////////////////////

void setup() 
{
  Serial.begin(9600);
  lcd.begin(20, 4);
  lcd.setCursor(0, 1);
//lcd.print("                    ");
  lcd.print(" TestJig Start - up ");
  lcd.setCursor(0, 2);
//lcd.print("                    ");
  lcd.print("    Please Wait     ");
  
  pingTimer[0] = millis() + 75;           // First ping starts at 75ms, gives time for the Arduino to chill before starting.
  for (uint8_t i = 1; i < SONAR_NUM; i++) // Set the starting time for each sensor.
    pingTimer[i] = pingTimer[i - 1] + PING_INTERVAL;
    
  lcd.clear();
  lcd.home();
  lcd.setCursor(0, 0);
//lcd.print("                    ");
  lcd.print("Ultrasonic number 1 ");
  lcd.setCursor(0, 1);
//lcd.print("                    ");
  lcd.print("           cm      ");
  lcd.setCursor(0, 2);
//lcd.print("                    ");
  lcd.print("Ultrasonic number 2 ");
  lcd.setCursor(0, 3);
//lcd.print("                    ");
  lcd.print("           cm       ");
}

void loop() 
{
  for (uint8_t i = 0; i < SONAR_NUM; i++) 
  { // Loop through all the sensors.
    if (millis() >= pingTimer[i]) 
    {         // Is it this sensor's time to ping?
      pingTimer[i] += PING_INTERVAL * SONAR_NUM;  // Set next time this sensor will be pinged.
      if (i == 0 && currentSensor == SONAR_NUM - 1) oneSensorCycle(); // Sensor ping cycle complete, do something with the results.
      sonar[currentSensor].timer_stop();          // Make sure previous timer is canceled before starting a new ping (insurance).
      currentSensor = i;                          // Sensor being accessed.
      cm[currentSensor] = 0;                      // Make distance zero in case there's no ping echo for this sensor.
      sonar[currentSensor].ping_timer(echoCheck); // Do the ping (processing continues, interrupt will call echoCheck to look for echo).
    }
  }
  // The rest of your code would go here.
}

void echoCheck() 
{ // If ping received, set the sensor distance to array.
  if (sonar[currentSensor].check_timer())
    cm[currentSensor] = sonar[currentSensor].ping_result / US_ROUNDTRIP_CM;
}

void oneSensorCycle() 
{ // Sensor ping cycle complete, do something with the results.
  for (uint8_t i = 0; i < SONAR_NUM; i++) {
    //Serial.print(i);
    //Serial.print("=");
    //Serial.print(cm[i]);
    //Serial.print("cm ");
    lcd.home();
    lcd.setCursor(0, 0);
    if(i==0)
    {
    //lcd.print("                    ");
      lcd.setCursor(7, 1);
      lcd.print("   ");
      lcd.setCursor(7, 1);
      lcd.print(cm[i]);
    }
    else if(i==1)
    {
      lcd.setCursor(7, 3);
      lcd.print("   ");
      lcd.setCursor(7, 3);
      lcd.print(cm[i]);
    } 
  }
  Serial.println();
}

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here