Setelah mengalami riset dan percobaan selama 3 bulan, akhirnya si kompas digital CMPS10 berhasil menunjukkan fungsinya. Cukup dicolok ke Arduino Uno, lalu ditampilin di LCD 16×2. Saya menggunakan template kode dari kang James Henderson di http://www.robot-electronics.co.uk/files/arduino_cmps10.ino. Saya mengubah kode untuk LCD nya dari LCD03 ke LCD 16×2 biasa. Disini ada beberapa mode, si kompas bisa menunjukkan arahnya berapa derajat gitu, bisa menunjukkan pitch dan rollnya juga. Saya menambahkan pembagian 8 arah mata anginnya juga. Hardware dirangkai oleh mas Muhammad Faqih Ulum.
/****************************************************************
*                 Arduino CMPS10 example code                 *
*                   CMPS10 running I2C mode                   *
*Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â by James Henderson, 2012Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â *
*     modified by M. Faqih Ulum and Talking Compass Team      *
*****************************************************************/
// Originally, this program is made with LCD03 display. But we only have LCD 16×2, so we modified it a little.
#include <Wire.h>
#include <LiquidCrystal.h>
#define ADDRESS 0x60Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â // Defines address of CMPS10
LiquidCrystal lcd = LiquidCrystal (7, 8, 9, 10, 11, 12);
void setup(){
Wire.begin();Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â // Conects I2C
lcd.begin(16, 2);
}
void loop(){
byte highByte, lowByte, fine;Â Â Â Â Â Â Â Â Â Â Â Â Â // highByte and lowByte store high and low bytes of the bearing and fine stores decimal place of bearing
char pitch, roll;Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â // Stores pitch and roll values of CMPS10, chars are used because they support signed value
int bearing;Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â // Stores full bearing
Wire.beginTransmission(ADDRESS);Â Â Â Â Â Â Â Â Â Â //starts communication with CMPS10
Wire.write(2);Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â //Sends the register we wish to start reading from
Wire.endTransmission();
Wire.requestFrom(ADDRESS, 4);Â Â Â Â Â Â Â Â Â Â Â Â Â // Request 4 bytes from CMPS10
while(Wire.available() < 4);Â Â Â Â Â Â Â Â Â Â Â Â Â Â // Wait for bytes to become available
highByte = Wire.read();
lowByte = Wire.read();
pitch = Wire.read();
roll = Wire.read();
bearing = ((highByte<<8)+lowByte)/10;Â Â Â Â Â // Calculate full bearing
fine = ((highByte<<8)+lowByte)%10;Â Â Â Â Â Â Â Â // Calculate decimal place of bearing
display_data(bearing, fine, pitch, roll);Â // Display data to the LCD
delay(100);
}
void display_data(int b, int f, int p, int r){Â // pitch and roll (p, r) are recieved as ints instead oif bytes so that they will display corectly as signed values.
char a1[15] = “Utara”;
char a2[15] = “Timur laut”;
char a3[15] = “Timur”;
char a4[15] = “Tenggara”;
char a5[15] = “Selatan”;
char a6[15] = “Barat daya”;
char a7[15] = “Barat”;
char a8[15] = “Barat laut”;
//lcd.setCursor(0, 0);Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â // Set the LCD cursor position
//lcd.print(“CMPS10 : “);
//lcd.print(soft_ver());Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â // Display software version of the CMPS10
delay(5);Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â // Delay to allow LCD to proscess data
//Â Â Â lcd.setCursor(0, 1);
//Â Â Â lcd.print(“Angle = “);Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â // Display the full bearing and fine bearing seperated by a decimal poin on the LCD, if you want to display it, jus remove the comment mark
//Â Â Â lcd.print(b);
//Â Â Â lcd.print(“.”);
//Â Â Â lcd.print(f);
lcd.setCursor(0, 1);
//lcd.print(“Arah “);Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â // Display the direction
lcd.clear();
if ((b >= 338) || (b <= 22))
{
lcd.print(a1);
}
else if ((b >= 23) && (b <= 67))
{
lcd.print(a2);
}
else if ((b >= 68) && (b <= 112))
{
lcd.print(a3);
}
else if ((b >= 113) && (b <= 157))
{
lcd.print(a4);
}
else if ((b >= 158) && (b <= 202))
{
lcd.print(a5);
}
else if ((b >= 203) && (b <= 247))
{
lcd.print(a6);
}
else if ((b >= 248) && (b <= 292))
{
lcd.print(a7);
}
else if ((b >= 293) && (b <= 337))
{
lcd.print(a8);
}
delay(5);
//Â lcd.setCursor(10, 1);Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â // Display the Pitch value to the LCD, if you want to diplay it, just remove the comment mark
//Â lcd.write(41);
//Â lcd.print(“Pitch = “);
//Â lcd.print(p);
//Â lcd.print(” “);
//Â delay(5);
//Â lcd.setCursor(14, 1);Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â // Display the roll value to the LCD, if you want to diplay it, just remove the comment mark
//Â lcd.write(61);
//Â lcd.print(“Roll = “);
//Â lcd.print(r);
//Â lcd.print(” “);
}
int soft_ver(){
int data;                                  // Software version of CMPS10 is read into data and then returned
Wire.beginTransmission(ADDRESS);
// Values of 0 being sent with write need to be masked as a byte so they are not misinterpreted as NULL this is a bug in arduino 1.0
Wire.write((byte)0);Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â // Sends the register we wish to start reading from
Wire.endTransmission();
Wire.requestFrom(ADDRESS, 1);Â Â Â Â Â Â Â Â Â Â Â Â // Request byte from CMPS10
while(Wire.available() < 1);
data = Wire.read();
return(data);
}
Untuk wiringnya, bisa lihat di http://www.robot-electronics.co.uk/htm/arduino_examples.htm#Tilt%20Compensated%20Magnetic%20Compass untuk CMPS10 ke Arduino dan http://www.ladyada.net/learn/lcd/index.html untuk Arduino ke LCD. Selamat mencoba!