So, you ARE using Serial to talk to the PC and the Android. Didn't I say KNOCK THAT OFF!
yes, you did say that.
In your very first code in the opening post, you used SoftwareSerial for the bluetooth. Go back to that for the receiving in recvWithStartEndMarkers(); you can still print on Serial for debugging.
As I don't have a bluetooth module available, I used HardwareSerial to demonstrate.
And I suggest that you add a Serial.print/println in processData for debugging purposes to see the received message.
I put the println statement where you suggested, and the attached screenshot (capture1) shows the results in the serial monitor. What I would expect is that when the Android phone is tilted up at the top (y-axis) I get a positive number 0-9 in the serial monitor, down at the top I get a negative number 0-9, down on the left (x-axis) I get a positive number 0-9, and down on the right I get a negative number 0-9. All those numbers correspond with the degree of tilt on the android phone What does happen is that I get a positive number 0-9 in the serial monitor when the phone is tilted down on the left (x-axis) or up at the top towards me (y-axis). If the phone is tilted down at the top or down on the right, negative numbers, all I get is the - symbol. I do get an 'a' or 'b' from the android when those buttons are pressed.
#include <SoftwareSerial.h>
// * RX is digital pin 10 (connect to TX of other device)
// * TX is digital pin 11 (connect to RX of other device)
SoftwareSerial mySerial(10, 11); // RX, TX
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars]; // temporary array for use when parsing
// variables to hold the parsed data
int pwmLeftFromAndroid = 0;
int pwmRightFromAndroid = 0;
boolean newData = false;
int motorPinL = 5;
int motorPinR = 6;
int led = 8;
//============
void setup() {
pinMode(led, OUTPUT);
pinMode(motorPinL, OUTPUT);
pinMode(motorPinR, OUTPUT);
Serial.begin(57600);
Serial.println("test");
mySerial.begin(57600); //receives data from the Android over bluetooth
}
//============
void loop()
{
recvWithStartEndMarkers();
if (newData == true)
{
processData();
newData = false;
}
}
void processData()
{
Serial.println(receivedChars[0]);
// if the first character is 'a' or 'b', control the led
if (receivedChars[0] == 'a' || receivedChars[0] == 'b') {
ledControl();
}
// else control the motors
else
{
motorControl();
}
}
//============
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (mySerial.available() > 0 && newData == false) {
rc = mySerial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
//============
void motorControl() { // split the data into its parts
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(tempChars, ",");
pwmLeftFromAndroid = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
pwmRightFromAndroid = atoi(strtokIndx); // convert this part to an integer
analogWrite(motorPinL, pwmLeftFromAndroid);
analogWrite(motorPinR, pwmRightFromAndroid);
}
void ledControl()
{
if (receivedChars[0] == 'a')
{
digitalWrite(led, LOW);
}
if (receivedChars[0] == 'b')
{
digitalWrite(led, HIGH);
}
}
If I put the println statement like this:
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (mySerial.available() > 0 && newData == false) {
rc = mySerial.read();
Serial.println(rc);
I get the results I would expect. It looks like the attached screenshot (capture). I get numbers I expect like: <-6.7708025, -0.11492168>
by the time the data gets through the motorControl function, the serial.println results are all 0 like the screen capture and sketch in my reply 17. Not what I expect. Any ideas?
Let me ask this. Your device is sending -6.7708025, -0.11492168, which doesn't equal 'a' or 'b', so you call motorControl() to deal with the string.
You get the token up to the comma, -6.7708025, and call atoi() (ASCII to int). Why? Does that value LOOK like an int?
You are going to get -6 as the return value, which you pass to analogWrite(). Why? What do you really think it is going to do with a negative value?