read a 16 bit data from serial.
in
Integration and Hardware
•
3 years ago
Hi, my name is luca and i'm italian new member.
I've just load this sketch in my arduino for read a position from a compass like HMC6352
my question is how can i read and recostruction this 16 bit in processig.
I try to use example "SimpleRead" but with bad result
Excuse me for my bad english,
Thanks, luca
I've just load this sketch in my arduino for read a position from a compass like HMC6352
- #include <LiquidCrystal.h>
#include <Wire.h>
int HMC6352Address = 0x42;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// This is calculated in the setup() function
int slaveAddress;
int ledPin = 13;
boolean ledState = false;
byte headingData[2];
int i, headingValue;
void setup()
{
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
// Shift the device's documented slave address (0x42) 1 bit right
// This compensates for how the TWI library only wants the
// 7 most significant bits (with the high bit padded with 0)
slaveAddress = HMC6352Address >> 1; // This results in 0x21 as the address to pass to TWI
Serial.begin(9600);
pinMode(ledPin, OUTPUT); // Set the LED pin as output
Wire.begin();
}
void loop()
{
lcd.clear();
lcd.print("Pos. Attuale:");
lcd.setCursor(6, 1);
lcd.print("Gradi Nord");
lcd.setCursor(0, 1);
// Flash the LED on pin 13 just to show that something is happening
// Also serves as an indication that we're not "stuck" waiting for TWI data
ledState = !ledState;
if (ledState) {
digitalWrite(ledPin,HIGH);
}
else
{
digitalWrite(ledPin,LOW);
}
// Send a "A" command to the HMC6352
// This requests the current heading data
Wire.beginTransmission(slaveAddress);
Wire.send("A"); // The "Get Data" command
Wire.endTransmission();
delay(10); // The HMC6352 needs at least a 70us (microsecond) delay
// after this command. Using 10ms just makes it safe
// Read the 2 heading bytes, MSB first
// The resulting 16bit word is the compass heading in 10th's of a degree
// For example: a heading of 1345 would be 134.5 degrees
Wire.requestFrom(slaveAddress, 2); // Request the 2 byte heading (MSB comes first)
i = 0;
while(Wire.available() && i < 2)
{
headingData[i] = Wire.receive();
i++;
}
headingValue = headingData[0]*256 + headingData[1]; // Put the MSB and LSB together
headingValue = headingData[0]*256 + headingData[1]; // Put the MSB and LSB together
// Serial.print("Current heading: ");
Serial.println(int (headingValue / 10)); // The whole number part of the heading
// Serial.print(".");
// Serial.print(int (headingValue % 10)); // The fractional part of the heading
// Serial.println(" degrees");
lcd.print(int (headingValue / 10)); // The whole number part of the heading
lcd.setCursor(3, 1);
lcd.print(".");
lcd.setCursor(4, 1);
lcd.print(int (headingValue % 10));
delay(500);
}
my question is how can i read and recostruction this 16 bit in processig.
I try to use example "SimpleRead" but with bad result

Excuse me for my bad english,
Thanks, luca
1