Bacalah

Learning is forever

RSS Feed

Going where this year?

Kompas Digital + Arduino + LCD

0 Comments
Posted by Taufiq on April 14, 2012 at 12:37 pm

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!

You can leave a comment, or trackback from your own site.

0 Comments

You can be the first to comment!

Leave a Reply

Your email address will not be published. Required fields are marked *