Hi I am trying to read bytes 0,1,2,3,4,and 5 from a tilt sensor and display to the serial monitor. Tilt sensor connected via software serial.
From sensor specifics: Command "C" : Continuous Mode: Transfers packet
continuously at maximum rate.
which I think i have done. Then I need to receive the packet containing the data.
Then: Byte
0
1
2
3
4
5
Description
Header (always 255)
Pitch MSB (0-255)
Pitch LSB (0-255)
Roll MSB (0-255)
Roll LSB (0-255)
Checksum (8-bit sum
of bytes 1-4)
My code:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
mySerial.begin(9600);
byte C = C;
mySerial.write(C);
mySerial.print(C);
Look for Serial Input Basics within this forum. That will show you how serial is supposed to be read.
You need to be able to identify the start of a packet and then start reading characters one at a time from there. Just waiting for 5 might get you half of the back of one packet and the beginning of the next one.
If you told us which sensor, maybe with a link to a datasheet, we could be more helpful.
MorganS:
Look for Serial Input Basics within this forum. That will show you how serial is supposed to be read.
You need to be able to identify the start of a packet and then start reading characters one at a time from there. Just waiting for 5 might get you half of the back of one packet and the beginning of the next one.
If you told us which sensor, maybe with a link to a datasheet, we could be more helpful.
In C, it's traditional for the read() method to return an int. Values 0-255 are bytes, and a value of -1 means 'end of file' or 'nothing available'. You are stuffing these into an unsigned 8-bit, so they come out as 255.
Change all these to ints
int receiveSerial = mySerial.read();
Serial.println(receiveSerial);
If they say -1 rather than 255, then mySerial.read is not just returning a 255, it's specifically telling you that there is nothing available.
So why are you getting those when you explicitly check …
Oh.
{if(mySerial.available() >= 5);
No. It's:
if(mySerial.available() >= 5)
{
Google "C++ tutorial", and do the first couple of chapters of a tutorial. These will teach you how to properly build statements, blocks, and control constructs.
Create a local variable, assigning it the value that it already has, which is undefined. How in the hell is that useful?
Why send that garbage to the tilt sensor in both binary and ASCII mode?
You have a tilt sensor connected to the software serial pins, so why in the heck is the instance named mySerial? tiltSensor would make a hell of a lot more sense.