Hello,
I have a Arduino Uno. Wrote code to use with the LSM303 and using the USB cable, I get a nice number of NMEA sentences. However, the data needs to go into a serial port on my (old) computer, or go into the multiplexer. This needs to be tty logic.
I spent hours searching for a solution on how to get the TX pin working, and did not find a fix, only many people searching for a similar solution.
The code I'm using:
#include <Wire.h>
#include <LSM303.h>
#include <AverageList.h>
#include <SoftwareSerial.h>
LSM303 compass;
SoftwareSerial mySerial(2, 3); // RX, TX
const byte MAX_NUMBER_OF_READINGS = 10;
int storage[MAX_NUMBER_OF_READINGS] = {0};
AverageList<int> list = AverageList<int>( storage, MAX_NUMBER_OF_READINGS );
void setup() {
Serial.begin(9600);
pinMode(2, INPUT);
Wire.begin();
compass.init();
compass.enableDefault();
// Calibration values. Use the Calibrate example program to get the values for
// your compass.
compass.m_min.x = -550; compass.m_min.y = -357; compass.m_min.z = -460;
compass.m_max.x = 223; compass.m_max.y = 378; compass.m_max.z = 300;
}
/*----------------------------------------------------------------------------
1 2 3 4 5 6
| | | | | |
$--HDG,x.x,x.x,a,x.x,a*hh<CR><LF>
------------------------------------------------------------------------------
Field Number:
1. Magnetic Sensor heading in degrees
2. Magnetic Deviation, degrees
3. Magnetic Deviation direction, E = Easterly, W = Westerly
4. Magnetic Variation degrees
5. Magnetic Variation direction, E = Easterly, W = Westerly
6. Checksum */
char nmeastr[17]; // HCHDG,000.00,,,,*
int checksum;
char hs[6]; // 000.00
void loop() {
strcpy(nmeastr,"HCHDG,");
//dtostrf(FLOAT,WIDTH,PRECSISION,BUFFER);
compass.read();
int heading = compass.heading((LSM303::vector){0,-1,0}); //1,0,0 will allign North with SFE silkscreen
//int rand = compass.heading((LSM303::vector){0,-1,0});
//Serial.print("Heading list : ");
//Serial.println(heading);
list.addValue(heading);
//list.debug("list",Serial); //print contents to serial console
//Serial.print("Average: ");
//Serial.println(list.getAverage());
//dtostrf(compass.heading((LSM303::vector){0,-1,0}), 5, 2, hs);
dtostrf(list.getAverage(), 5, 2, hs);
//dtostrf(heading, 5, 2, hs);
//Serial.print("hs ");
//Serial.print(hs);
//Serial.println();
strcat(nmeastr,hs);
strcat(nmeastr,",,,,");
//add a checksum
checksum=0;
for (int n=0; n < strlen(nmeastr); n++) {
checksum ^= nmeastr[n];
}
Serial.print("$");
Serial.print(nmeastr);
Serial.print("*");
Serial.print(checksum, HEX);
Serial.print("\t");
Serial.println();
/* 5hz == 200 */
delay(100);
}
For the tty logic I built a board as found here: http://prosje.be/CO/Schemas/RS232-to-TTL3.png The mentioned rs232 in the schema is connected to the TX pin 1.
Any help is highly appraciated on how to solve this issue. Am I working in the right direction??
Many thanks in advance!