38
LAMPIRAN 1 Tampilan Alat
Gambar 1. Sistem Sensor didalam Kotak Hitam
Gambar 2. LASER
Universitas Sumatera Utara
39
Gambar 3. Tabung Volumetri dan Bola Besi didalam Sampel
Universitas Sumatera Utara
40
LAMPIRAN 2 Tampilan aplikasi pada Android
Gambar 1. Tampilan awal aplikasi
Gambar 2. Tampilan menu
Gambar 3. Tampilan menu setting
Gambar 4. Tampilan edit jarak sensor
Universitas Sumatera Utara
41
Gambar 5. Tampilan setelah jarak sensor diatur
Gambar 6. Perhitungan viskositas gliserin
Gambar 7. Perhitungan viskositas Oli Shell Advance
Gambar 8. Perhitungan viskositas Minyak Goreng
LAMPIRAN 3
Universitas Sumatera Utara
42
1
Program Untuk Modul Arduino #include <Usb.h> #include
//jenis paket yang akan dikirim #define perintah 0x1 #define targetTerima 0x2 #define comAtur 0x3 #define tarAtur 0x4 #define koma 0x5 AndroidAccessory acc("ajirdoank", "Vizcouz", "Vizcouz", "1.0", "http://www.brainhover.blogspot.co.vu", "123456789");
//pin analog yang digunakan #define ldr1 2 #define ldr2 0 byte msg[6]; int xAmbang = 10; int yAmbang = 100; int valLdr1; int valAkhirLdr1; int valLdr1Kirim; boolean valLdr1Picu; int valLdr2; int valAkhirLdr2; int valLdr2Kirim; boolean valLdr2Picu;
Universitas Sumatera Utara
43
float ubah1 = 0; float ubah2 = 0; int waktuMulai = 0; int val; void setup() { reset(); Serial.begin(9600); acc.powerOn(); msg[0] = perintah; msg[1] = targetTerima; } void reset() { valLdr1 = 0; valAkhirLdr1 = 0; valLdr1Kirim = 0; valLdr1Picu = false; valLdr2 = 0; valAkhirLdr2 = 0; valLdr2Kirim = 0; valLdr2Picu = false; waktuMulai = 0; val = 0; kirimData(); } void cekLdr1() { if(valLdr1Picu) { return; }
Universitas Sumatera Utara
44
//baca nilai ldr1; valLdr1 = analogRead(ldr1); //jika selisih prubahan ldr1 lebih besar dari nilai yAmbang; if(abs(valAkhirLdr1 - valLdr1) >= yAmbang) { //Serial.print("ldr1 = "); //Serial.print(valLdr1); valAkhirLdr1 = valLdr1; } //jika nilai ldr1 yg skrang besar dari nilai awal ldr1; if(valAkhirLdr1 > valLdr1) { //hitung persentase perubahan nilai; ubah1 = ((float)(valAkhirLdr1 - valLdr1) / (float)valLdr1) * 1000; //nah jika nilai presentase perubahan tersebut lebih besar dari nilai xAmbang; if(ubah1 > xAmbang) { //Serial.print(" | ubah1 = "); //Serial.print(ubah1); //mulai picu ldr1 dan mulai waktu timer; valLdr1Picu = true; waktuMulai = millis(); } } //nilai ldr1 yg sekarang dikembalikan dengan nilai ldr1 yg terbaca; valAkhirLdr1 = valLdr1; } void cekLdr2() { if(valLdr2Picu) { return;
Universitas Sumatera Utara
45
} //baca nilai ldr2; valLdr2 = analogRead(ldr2); //jika selisih prubahan ldr2 lebih besar dari nilai yAmbang; if(abs(valAkhirLdr2 - valLdr2) >= yAmbang) { //Serial.print("
ldr2 = ");
//Serial.print(valLdr2); valAkhirLdr2 = valLdr2; } //cek ldr2 hanya jika ldr1 udah terpicu; if(valLdr1Picu && valAkhirLdr2 > valLdr2) { //hitung persentase perubahan dari ldr2; ubah2 = ((float)(valAkhirLdr2 - valLdr2) / (float)valLdr2) * 2000; //jika nilai ubah2 lebih besar daripada nilai xAmbang; if(ubah2 > xAmbang) { valLdr2Picu = true; //Serial.print(" | ubah2 = "); //Serial.print(ubah2); //Serial.print(" | "); //Serial.print("waktu = "); val = millis() - waktuMulai; kirimData(); //Serial.print(val); } } valAkhirLdr2 = valLdr2; } void kirimData(){ if (acc.isConnected()){ msg[0] = perintah; msg[1] = targetTerima;
Universitas Sumatera Utara
46
msg[2] = (byte) (val >> 24); msg[3] = (byte) (val >> 16); msg[4] = (byte) (val >> 8); msg[5] = (byte) (val); acc.write(msg, 6); delay(2); } } void atur(){ if (acc.isConnected()){ int len = acc.read(msg, sizeof(msg), 1); if(len > 0){ if(msg[0] == comAtur){ if(msg[1] == tarAtur){ byte titik = msg[2]; if(titik == koma){ reset(); } } } } } delay(2); } void loop() { if(acc.isConnected()); cekLdr1(); cekLdr2(); atur(); delay(1); }
2 2.1
Program Untuk Aplikasi Android Untuk Tampilan Aplikasi dan menu pilihan
Universitas Sumatera Utara
47
2.1.1 main.xml <Button android:id="@+id/button1" style="@style/btnStyleShakespeare"
Universitas Sumatera Utara
48
android:width="150dp" android:height="20dp" android:layout_column="6" android:layout_gravity="center_horizontal|bottom" android:layout_marginBottom="15dp" android:layout_row="0" android:text="@string/rstButton" android:textSize="7pt" /> <Space android:layout_column="3" android:layout_gravity="fill_vertical" android:layout_row="3" />
Universitas Sumatera Utara
49
android:id="@+id/UnitTime" android:layout_column="2" android:layout_gravity="center_horizontal|right" android:layout_marginRight="20dp" android:layout_row="1" android:text="@string/timeU" android:textSize="8pt" />
Universitas Sumatera Utara
50
android:layout_column="2" android:layout_gravity="center_horizontal|left" android:layout_marginLeft="20dp" android:layout_row="1" android:text="@string/jarakPisah" android:textAppearance="?android:attr/textAppearanceMedium" android:textColorHint="@color/black" android:textSize="8pt" /> <Space android:layout_column="3" android:layout_gravity="fill_vertical" android:layout_row="3" />
2.1.2 veloz.xml
Universitas Sumatera Utara
51
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
2.2
Untuk Java Source Code
2.2.1
Ajirdoank/src/Veloz.java
package com.ajirdoank.velozity; //script untuk menerima dan mengirimkan byte ke Arduino //ditulis oleh achmad muhajir //@ajirdoank import java.io.IOException; import java.io.FileDescriptor; import java.io.FileInputStream; import java.io.FileOutputStream; import java.text.DecimalFormat; import com.ajirdoank.velozity.Prefs; import android.os.Bundle; import android.os.ParcelFileDescriptor; import android.preference.PreferenceManager; import android.app.Activity;
Universitas Sumatera Utara
52
import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Intent; import android.content.IntentFilter; import android.content.Context; import android.content.SharedPreferences; import android.util.Log; import android.widget.Button; import android.widget.TextView; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import com.android.future.usb.UsbAccessory; import com.android.future.usb.UsbManager;
public class Veloz extends Activity { private static final String tag = Veloz.class.getSimpleName(); private PendingIntent mPermissionIntent; private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION"; private boolean mPermissionRequestPending; private UsbManager mUsbManager; private UsbAccessory mAccessory; private ParcelFileDescriptor mFileDescriptor; private FileInputStream mInputStream; private FileOutputStream mOutputStream; private static final byte perintah = 0x1; private static final byte targetTerima = 0x2; private static final byte comAtur = 0x3; private static final byte tarAtur = 0x4; private static final byte koma = 0x5; public TextView SenSD, time, vizcouz;
Universitas Sumatera Utara
53
public double
waktux, var1, BDy, BMy, LDy, SDy, TDy, GRy,
visc, DB, CF;
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mUsbManager = UsbManager.getInstance(this); mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent( ACTION_USB_PERMISSION), 0); IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION); filter.addAction(UsbManager.ACTION_USB_ACCESSORY_DETACHED); registerReceiver(mUsbReceiver, filter); setContentView(R.layout.main); SharedPreferences pengaturan = PreferenceManager.getDefaultSharedPreferences(getBaseContext()); //ini bagian untuk setting variable //bagian di preference yg uda dibuat dan dishare ke mainActivity String BDx = pengaturan.getString("BD", "0"); String BMx = pengaturan.getString("BM", "0"); String LDx = pengaturan.getString("LD", "0"); String SDx = pengaturan.getString("SD", "0"); String TDx = pengaturan.getString("TD", "0"); String GRx = pengaturan.getString("GRV", "0"); //konversi BDy = Double.valueOf(BDx); BMy = Double.valueOf(BMx); LDy = Double.valueOf(LDx); SDy = Double.valueOf(SDx); TDy = Double.valueOf(TDx); GRy = Double.valueOf(GRx); //massa jenis bola atau Density of Ball
Universitas Sumatera Utara
54
DB = BMy/( (4.0 / 3.0 )*Math.PI*Math.pow(BDy/2.0, 3)); //variable tetap sebelum perhitungan viskositas var1 = ( 2.0 / 9.0 )* GRy * Math.pow(BDy/2.0, 2)*(DB LDy)/SDy; //untuk faktor koreksi CF = 1.0 - (2.104 * (BDy/TDy)) + (2.09 * (Math.pow(BDy/TDy, 3))) - (0.95 * (Math.pow(BDy/TDy, 5))); //pengaturan teks yang muncul pada mainActivity SenSD = (TextView)findViewById(R.id.SensSD); SenSD.setText(String.valueOf(SDy)); vizcouz = (TextView)findViewById(R.id.viscText); time = (TextView)findViewById(R.id.timeVal); Button reset = (Button)findViewById(R.id.button1); reset.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub time.setText("0"); vizcouz.setText("0"); byte[] buffer = new byte[3]; buffer[0] = comAtur; buffer[1] = tarAtur; buffer[2] = koma; try{ mOutputStream.write(buffer); }catch(IOException e){ Log.e(tag, "pesan tidak dikenal", e); } } });
}
Universitas Sumatera Utara
55
//cek broadcast data streaming yang masuk private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (ACTION_USB_PERMISSION.equals(action)) { synchronized (this) { //sinkron dan baca apa usb tersamsubung atau tidak UsbAccessory accessory = UsbManager.getAccessory(intent); if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) { openAccessory(accessory); } else { Log.d(tag, "sambungan ditolak" + accessory); } mPermissionRequestPending = false; } } else if (UsbManager.ACTION_USB_ACCESSORY_DETACHED.equals(action)) { //jika sambungan Usb dicabut UsbAccessory accessory = UsbManager.getAccessory(intent); if (accessory != null && accessory.equals(mAccessory)) { closeAccessory(); } } } }; //ketika meresume aplikasi @Override public void onResume(){ super.onResume(); if (mInputStream != null && mOutputStream != null) { return; } UsbAccessory[] accessories = mUsbManager.getAccessoryList();
Universitas Sumatera Utara
56
UsbAccessory accessory = (accessories == null ? null : accessories[0]); if (accessory != null) { if (mUsbManager.hasPermission(accessory)) { openAccessory(accessory); } else { synchronized (mUsbReceiver) { if (!mPermissionRequestPending) { mUsbManager.requestPermission(accessory, mPermissionIntent); mPermissionRequestPending = true; } } } } else { Log.d(tag, "android tak tersambung"); } } //ketika aplikasi dipause @Override public void onPause(){ super.onPause(); closeAccessory(); } //ketika data streaming tidak ada @Override public void onDestroy(){ super.onDestroy(); unregisterReceiver(mUsbReceiver); } //untuk menutup aplikasi private void closeAccessory() { // TODO Auto-generated method stub try{ if(mFileDescriptor != null){ mFileDescriptor.close(); }
Universitas Sumatera Utara
57
}catch(IOException e){ }finally{ mFileDescriptor = null; mAccessory = null; } } //inisialisasi ketika USB di insert private void openAccessory(UsbAccessory accessory) { // TODO Auto-generated method stub mFileDescriptor = mUsbManager.openAccessory(accessory); if (mFileDescriptor != null){ mAccessory = accessory; FileDescriptor fd = mFileDescriptor.getFileDescriptor(); mInputStream = new FileInputStream(fd); mOutputStream = new FileOutputStream(fd); Thread thread = new Thread(null, jalankan, tag); thread.start(); Log.d(tag, "handphone tersambung"); }else{ Log.d(tag, "sambungan gagal"); } } //proses penerimaan data straming via USB Runnable jalankan = new Runnable() { @Override public void run() { // TODO Auto-generated method stub int ret = 0; final byte buffer[] = new byte[6]; while (ret >= 0){ try{ ret = mInputStream.read(buffer); }catch(IOException e){ break; } switch(buffer[0]){
Universitas Sumatera Utara
58
case perintah: if(buffer[1] == targetTerima){ final int waktu = ((buffer[2] & 0xFF) << 24) +((buffer[3] & 0xFF) << 16) + ((buffer[4] & 0xFF) << 8) + (buffer[5] & 0xFF); if (waktu > 0){ runOnUiThread(new Runnable() { @Override public void run() { //perhitungan viskositas time.setText(String.valueOf(waktu)); waktux = Double.valueOf(waktu); //viskositas akhir double x = var1 * waktux / 1000.0; //hasil akhir dari perhitungan vizcouz.setText(new DecimalFormat("###.####").format(x)); } }); } } break; default: Log.d(tag, "no data found" + buffer[0]); break; } } } }; //script untuk menu option @Override public boolean onCreateOptionsMenu(android.view.Menu menu) { // TODO Auto-generated method stub super.onCreateOptionsMenu(menu); MenuInflater muncul = getMenuInflater(); muncul.inflate(R.menu.veloz, menu); return true;
Universitas Sumatera Utara
59
} //script ketika item dari menu option dipilih @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub switch(item.getItemId()){ case R.id.aboutMe: Intent a = new Intent("com.ajirdoank.velozity.ABOUT"); startActivity(a); return true; case R.id.setting: Intent s = new Intent("com.ajirdoank.velozity.PREFS"); startActivity(s); return true; case R.id.instruct: Intent i = new Intent("com.ajirdoank.velozity.INSTR"); startActivity(i); return true; case R.id.exit: finish(); } return false; } // the end of this application from @ajirdoank }
2.2.2
Velozity/src/Prefs.java
package com.ajirdoank.velozity; import android.os.Bundle; import android.preference.PreferenceActivity; public class Prefs extends PreferenceActivity{ @Override public void onCreate(Bundle savedInstanceState) {
Universitas Sumatera Utara
60
// TODO Auto-generated method stub super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.preferences); } }
2.3
Untuk accessory_filter.xml
<usb-accessory manufacturer="ajirdoank" model="Vizcouz" version="1.0" />
2.4
Untuk preferences.xml
<EditTextPreference android:title="Ball Diameters (m)" android:key="BD" android:inputType="numberDecimal" android:summary="input Ball diameter"> <EditTextPreference android:title="Ball Mass (kg)" android:key="BM" android:inputType="numberDecimal" android:summary="input Ball Mass"> <EditTextPreference android:title="Liquid Density (kg/m3)" android:key="LD"
Universitas Sumatera Utara
61
android:inputType="numberDecimal" android:summary="input Liquid Density"> <EditTextPreference android:title="Both Sensors Distance (m)" android:key="SD" android:inputType="numberDecimal" android:summary="input what distance both of sensor placed"> <EditTextPreference android:title="Tube Diameters (m)" android:key="TD" android:inputType="numberDecimal" android:summary="input Tube Diameters"> <EditTextPreference android:title="Gravitation (m/s2)" android:key="GRV" android:inputType="numberDecimal" android:summary="input Gravitation value">
Universitas Sumatera Utara