Skip to main content
added 1765 characters in body
Source Link

In the slave's loop(), when you say

input = char(BlueToothSerial.read());

I'd expect just the first character of the serial stream to be read and stored. Without further investigation, I don't know just what a String class does when you assign a character to it, but I doubt that any of the following code ever has more than one character at a time to work on. This will keep your getValue() calls from working like you want.

Instead of using String objects and your getValue() code, consider using code like the following to handle the serial input. The data format that this code expects is nnnxnnnynnnb, where each nnn is a series of digits, and x, y, b are literal characters.

This code shortens two of the overly-verbose parts of your code by:
• Use of an array and subscripting to replace the long list of if statements that sort out the combinations of low/medium/high x and y values.
• Use of Streaming.h (which you can install by unzipping Streaming5.zip from arduiniana.org into your sketchbook/libraries directory) to clean up long lists of Serial.println() statements, without increasing code size.

#include <Streaming.h>
int numVal = 0;

void doAction() {
  byte pins[] = { 9,0,0, 12,8,10, 0,11,0 };
  byte xy = 3*(x/500)+y/500;
  byte pin = pins[xy];
  if (pin) {
    clear();
    digitalWrite(pin, HIGH);
  }
  Serial << "xPosition =  " << xPosition << endl;
  Serial << "yPosition =  " << yPosition << endl;
  Serial << "buttonState = " << buttonState << endl;
  numVal = 0;
}

void loop() {
  while (BlueToothSerial.available()) {
    byte c = BlueToothSerial.read();
    if ('0' <= c && '9' >= c) {
        numVal = 10*numVal + c - '0';
    } else {
      switch (c) {
      case 'x':  xPosition = numVal;   break;
      case 'y':  yPosition = numVal;   break;
      case 'b':  buttonState = numVal; doAction(); break;
      }
      numVal = 0;
    }
  }
}

In the slave's loop(), when you say

input = char(BlueToothSerial.read());

I'd expect just the first character of the serial stream to be read and stored. Without further investigation, I don't know just what a String class does when you assign a character to it, but I doubt that any of the following code ever has more than one character at a time to work on. This will keep your getValue() calls from working like you want.

In the slave's loop(), when you say

input = char(BlueToothSerial.read());

I'd expect just the first character of the serial stream to be read and stored. Without further investigation, I don't know just what a String class does when you assign a character to it, but I doubt that any of the following code ever has more than one character at a time to work on. This will keep your getValue() calls from working like you want.

Instead of using String objects and your getValue() code, consider using code like the following to handle the serial input. The data format that this code expects is nnnxnnnynnnb, where each nnn is a series of digits, and x, y, b are literal characters.

This code shortens two of the overly-verbose parts of your code by:
• Use of an array and subscripting to replace the long list of if statements that sort out the combinations of low/medium/high x and y values.
• Use of Streaming.h (which you can install by unzipping Streaming5.zip from arduiniana.org into your sketchbook/libraries directory) to clean up long lists of Serial.println() statements, without increasing code size.

#include <Streaming.h>
int numVal = 0;

void doAction() {
  byte pins[] = { 9,0,0, 12,8,10, 0,11,0 };
  byte xy = 3*(x/500)+y/500;
  byte pin = pins[xy];
  if (pin) {
    clear();
    digitalWrite(pin, HIGH);
  }
  Serial << "xPosition =  " << xPosition << endl;
  Serial << "yPosition =  " << yPosition << endl;
  Serial << "buttonState = " << buttonState << endl;
  numVal = 0;
}

void loop() {
  while (BlueToothSerial.available()) {
    byte c = BlueToothSerial.read();
    if ('0' <= c && '9' >= c) {
        numVal = 10*numVal + c - '0';
    } else {
      switch (c) {
      case 'x':  xPosition = numVal;   break;
      case 'y':  yPosition = numVal;   break;
      case 'b':  buttonState = numVal; doAction(); break;
      }
      numVal = 0;
    }
  }
}
Source Link

In the slave's loop(), when you say

input = char(BlueToothSerial.read());

I'd expect just the first character of the serial stream to be read and stored. Without further investigation, I don't know just what a String class does when you assign a character to it, but I doubt that any of the following code ever has more than one character at a time to work on. This will keep your getValue() calls from working like you want.