Getting data from compass

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:

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.

#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?

Serial.write (int(headingValue));

headingValue is an int. There is no reason to cast it to an int. The Serial.write function expects a byte sized argument. Since you have 2 bytes that you are putting together as an int, you might want to write the two bytes and put them together in Processing.

I get the right thing printed out over serial, I just need to send it.

Since Processing is replacing the Serial Monitor on the other end of the serial port from the Arduino, you ARE sending it.

The problem must, therefore, lie in how you are opening the port in the Processing sketch. Unfortunately, the scroll bar is missing on that window, and we can't scroll up far enough to see how that is being done.

What do you get when you print the list of available ports? Which port is the Arduino IDE talking to the Arduino on? Did you close the Serial Monitor window before starting the Processing sketch?

Sorry, here’s the rest of it:

import processing.serial.*;

Serial myPort; // Create object from Serial class
int val; // Data received from the serial port
static final int BG_COLOR = 0;

final int STANDARD_BUTTON_WIDTH = 80;
final int STANDARD_BUTTON_HEIGHT = 40;
final int STANDARD_BUTTON_COLOR = 255; //Standard button color is white

Bluetooth bt = new Bluetooth();

//Radar r = new Radar();

boolean looping = true;


void setup()
{
  //Part 1 - create window
  createWindow();
  
  //Part 2 - create serial port for hardware connection
  initializeSerial();
  
  //Part 3 - draw all buttons, etc. related to bluetooth
  drawBluetooth();
  
  //Part 4 - check if mouse is clicked
  
  //Part 5 draw radar
  drawRadar();
  
  
  

  
}

void createWindow()
{
  // Creates window
  size(1200, 700);
}

void initializeSerial()
{
  //Print serial data and create myPort, the port used for hardware connection
  
  println(Serial.list());
  String portName = Serial.list()[0];
  myPort = new Serial(this, portName, 115200);
  
  if (portName == null)
  {
    println("portName is null!!!");
  }
}

void drawBluetooth()
{
  
  bt.checkTestButtonMouseOver();
  bt.drawBt();
}

void drawRadar()
{
 // r.draw();
  
}

void mousePressed() 
{
  //println("Mouse Pressed!");
  bt.checkTestButtonClick();
  
}
void draw()
{
  background(BG_COLOR); //Draws background
  
  bt.drawBt(); // Draws all GUI data related to bluetooth  
  
}



//setup is essentially the same thing as the main.
//setup conitains a set of subroutines that are executed in a loop

/*void setup()
{
  //GUI win = new GUI();
  //win.setSize(700, 500);
  //win.setVisible(true);
  
  //size(200, 200);
//window.line(50, 50, 10, 10);
}*/


/*
void setup() {
//size(200, 200);
//println(Serial.list());//This will print a list of the serial ports
//String portName = Serial.list()[4]; //replace 4 with the number for tty.FireFly-XXXX.
myPort = new Serial(this, portName, 115200);
myPort.write('H');
}

void draw()
{
  background(255);
  fill(204);
}
*/

class Bluetooth
{
  final int BLUETOOTH_TEST_BUTTON_X = 100;
  final int BLUETOOTH_TEST_BUTTON_Y = 100;
  
  final int BLUETOOTH_LEFT_BUTTON_X = 50;
  final int BLUETOOTH_LEFT_BUTTON_Y = 300;
  
  final int BLUETOOTH_RIGHT_BUTTON_X = 150;
  final int BLUETOOTH_RIGHT_BUTTON_Y = 300;
  
  final int BLUETOOTH_FORWARD_BUTTON_X = 100;
  final int BLUETOOTH_FORWARD_BUTTON_Y = 220;
  
  final int STANDARD_BUTTON_WIDTH = 80;
  final int STANDARD_BUTTON_HEIGHT = 40;
  final int STANDARD_BUTTON_COLOR = 255; //Standard button color is white
  final Color MOUSE_OVER_BUTTON_COLOR = Color.green;
  
  final int BLUETOOTH_TEST_BUTTON_Label_X = 90;
  final int BLUETOOTH_TEST_BUTTON_Label_Y = 90;
  
  final int BLUETOOTH_LEFT_BUTTON_LABEL_X = 50;
  final int BLUETOOTH_LEFT_BUTTON_LABEL_Y = 290;
  
  final int BLUETOOTH_RIGHT_BUTTON_LABEL_X = 150;
  final int BLUETOOTH_RIGHT_BUTTON_LABEL_Y = 290;
  
  final int BLUETOOTH_FORWARD_BUTTON_LABEL_X = 95;
  final int BLUETOOTH_FORWARD_BUTTON_LABEL_Y = 210;
  
  final int TIME_BEFORE_RESET = 1000;
  
  static final int RADAR_X = 800;
  static final int RADAR_Y = 400;
  static final int RADAR_WIDTH = 500;
  static final int RADAR_HEIGHT = 500;
  static final int RADAR_CENTER_HEIGHT = 5;
  static final int RADAR_CENTER_WIDTH = 5;
  
  private int rawHeading = 280;
  
  
  
  
  boolean testButtonMouseOver = false;
  
  
  //Checks to see if the Test button is clicked, if so it will send a signal via
  //Bluetooth to turn on the LED test button
  void checkTestButtonClick()
  {
    //println("Button Click Tested!");
    
    if (mouseX > BLUETOOTH_TEST_BUTTON_X && mouseX < BLUETOOTH_TEST_BUTTON_X + STANDARD_BUTTON_WIDTH &&
    mouseY > BLUETOOTH_TEST_BUTTON_Y && mouseY <  BLUETOOTH_TEST_BUTTON_Y + STANDARD_BUTTON_HEIGHT)
    {
      println("Test Button CLicked!");
      
      myPort.write('A'); // Sends 'A' char to on board computer to be processed and interpreted
      delay(TIME_BEFORE_RESET);
      myPort.write('Z');
    }
    
    if (mouseX > BLUETOOTH_LEFT_BUTTON_X && mouseX < BLUETOOTH_LEFT_BUTTON_X + STANDARD_BUTTON_WIDTH &&
    mouseY > BLUETOOTH_LEFT_BUTTON_Y && mouseY <  BLUETOOTH_LEFT_BUTTON_Y + STANDARD_BUTTON_HEIGHT)
    {
      println("Left Button CLicked!");
      
      myPort.write('B'); // Sends 'B' char to on board computer to be processed and interpreted
      delay(TIME_BEFORE_RESET);
      myPort.write('Z');
    }
    
    if (mouseX > BLUETOOTH_RIGHT_BUTTON_X && mouseX < BLUETOOTH_RIGHT_BUTTON_X + STANDARD_BUTTON_WIDTH &&
    mouseY > BLUETOOTH_RIGHT_BUTTON_Y && mouseY <  BLUETOOTH_RIGHT_BUTTON_Y + STANDARD_BUTTON_HEIGHT)
    {
      println("Right Button CLicked!");
      
      myPort.write('C'); // Sends 'C' char to on board computer to be processed and interpreted
      delay(TIME_BEFORE_RESET);
      myPort.write('Z');
    }
    
    if (mouseX > BLUETOOTH_FORWARD_BUTTON_X && mouseX < BLUETOOTH_FORWARD_BUTTON_X + STANDARD_BUTTON_WIDTH &&
    mouseY > BLUETOOTH_FORWARD_BUTTON_Y && mouseY <  BLUETOOTH_FORWARD_BUTTON_Y + STANDARD_BUTTON_HEIGHT)
    {
      println("Forward Button CLicked!");
      
      myPort.write('D'); // Sends 'B' char to on board computer to be processed and interpreted
      //println("Written D");
      delay(TIME_BEFORE_RESET);
      myPort.write('Z');
      println("Written Z");
    }
  }
  
  void checkTestButtonMouseOver()
  {
    println("Enter checkTestButtonMouseOver");
  if (mouseX > BLUETOOTH_TEST_BUTTON_X && mouseX < BLUETOOTH_TEST_BUTTON_X + STANDARD_BUTTON_WIDTH &&
    mouseY > BLUETOOTH_TEST_BUTTON_Y && mouseY <  BLUETOOTH_TEST_BUTTON_Y + STANDARD_BUTTON_HEIGHT)
    {
      println("Test Button Mouse Over!");
      testButtonMouseOver = true;
    }
  }
  
  
  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");
  }
    
    
  }
  
  
  void drawBt() //Draws all things related to bluetooth
  {
    
    if (testButtonMouseOver)
    {
     fill(0, 102, 153);
    }
     PFont font;
     font = loadFont("BankGothic-Light-20.vlw");
     textFont(font);
     text("Test LED", BLUETOOTH_TEST_BUTTON_Label_X, BLUETOOTH_TEST_BUTTON_Label_Y); 
     rect(BLUETOOTH_TEST_BUTTON_X, BLUETOOTH_TEST_BUTTON_Y, STANDARD_BUTTON_WIDTH, STANDARD_BUTTON_HEIGHT); //Draw Bluetooth Test button
     //fill(STANDARD_BUTTON_COLOR); //Fill button
     
     text("Left", BLUETOOTH_LEFT_BUTTON_LABEL_X, BLUETOOTH_LEFT_BUTTON_LABEL_Y); 
     rect(BLUETOOTH_LEFT_BUTTON_X, BLUETOOTH_LEFT_BUTTON_Y, STANDARD_BUTTON_WIDTH, STANDARD_BUTTON_HEIGHT);
     
     text("Right", BLUETOOTH_RIGHT_BUTTON_LABEL_X, BLUETOOTH_RIGHT_BUTTON_LABEL_Y); 
     rect(BLUETOOTH_RIGHT_BUTTON_X, BLUETOOTH_RIGHT_BUTTON_Y, STANDARD_BUTTON_WIDTH, STANDARD_BUTTON_HEIGHT);
     
     text("Forward", BLUETOOTH_FORWARD_BUTTON_LABEL_X, BLUETOOTH_FORWARD_BUTTON_LABEL_Y); 
     rect(BLUETOOTH_FORWARD_BUTTON_X, BLUETOOTH_FORWARD_BUTTON_Y, STANDARD_BUTTON_WIDTH, STANDARD_BUTTON_HEIGHT);
     
     // DRAW RADAR STUFF
     
     ellipse(RADAR_X, RADAR_Y, RADAR_WIDTH, RADAR_HEIGHT);
     
     fill(0);
     ellipse(RADAR_X, RADAR_Y, RADAR_CENTER_HEIGHT, RADAR_CENTER_WIDTH);
     fill(255);
     
     //r.draw();
    //println("About to get heading data");
    getHeadingValue();
    //println("Request for heading data sent");
  }
 
}

And these questions?

What do you get when you print the list of available ports? Which port is the Arduino IDE talking to the Arduino on? Did you close the Serial Monitor window before starting the Processing sketch?