We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpElectronics,  Serial Library › Getting external variable in Processing
Page Index Toggle Pages: 1
Getting external variable in Processing? (Read 1881 times)
Getting external variable in Processing?
May 26th, 2010, 9:41am
 
Ok, so I have a bluetooth serial connection to a robot with a compass attached. I'm trying to get it to send back the heading value to a Processing sketch.

here's the recieve code:

Code:

void getHeadingValue()
{

if ( myPort.available() > 0) { // the available check method returns an int that is the value if the port
// if it is more than 0 then it means that there is a value
rawHeading = myPort.read(); //raw heading is whatever the compass returns
println("Raw Heading" + rawHeading);
}
else
{
println("myPort is not greater than 0");
}


}


As of now it perpetually prints "myPort is not greater than 0".

This is the code I'm trying to get to send heading. I get the right thing printed out over serial, I just need to send it.

Code:

#include <Wire.h>
int HMC6352Address = 0x42;
// This is calculated in the setup() function
int slaveAddress;
int ledPin = 13;
boolean ledState = false;
byte headingData[2];
int i, headingValue;
void setup()
{
// 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()
{
// 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("E"); // 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];
Serial.write (int(headingValue));


As of now its not working. Is Serial.write the right method to use for the robot?
Page Index Toggle Pages: 1