Author Topic: Bluetooth samples?  (Read 3079 times)

deejayspinz

  • Newbie
  • *
  • Posts: 12
Bluetooth samples?
« on: February 27, 2013, 04:09:07 pm »
I'm looking to get the Digispark working with one of those cheapo bluetooth modules.  Purpose is to send commands from a mobile device and have the spark read them.  I've got a sample sketch that I am trying to cobble together from different bits, but am having issues with software serial.  Specifically the line Serial.readBytesUntil('\0', serialData, 15);

The error is BTBotControl2:82: error: 'class TinyDebugSerial' has no member named 'readBytesUntil', which I interpret as the Tiny version for the spark does not support this method.    Does anyone have any bluetooth polling samples that they can share?  I'm looking for something that will read commands such as this  23,-4  which would be interpreted as motor speeds 23 for left and -4 for right.


Note - code is an incomplete sample as it does not have the call to drive motors.  I am trying to get basic parsing working first.




Code: [Select]



#include <SoftwareSerial.h>


SoftwareSerial blueToothSerial(1, 2); // RX, TX


void setup() 
{
  Serial.begin(9600);
  Serial.println("Goodnight moon!");
  // set the data rate for the SoftwareSerial port
  blueToothSerial.begin(4800);
  //blueToothSerial.println("Hello, world?");
}




char serialData[16];
void loop()
{
  if(blueToothSerial.available() > 0)
  {
    Serial.readBytesUntil('\0', serialData, 15);
    //Serial.readBytesUntil(character, buffer, length)
    switch(serialData[0])
    {
      case 's':
        int speed[2];
        parseCommand(serialData, speed);
        setSpeed(speed[0], speed[1]);
        break;
    }
    // always echo
    Serial.write(serialData);
    // end of message is marked with a \0
    Serial.print('\0');


    // clear serialData array
    memset(serialData, 0, sizeof(serialData));
  }
}






void parseCommand(char* command, int* returnValues)
{
  // parsing state machine
  byte i = 2, j = 0, sign = 0;
  int temp = 0;
  while(*(command + i) != '\0')
  {
    switch(*(command + i))
    {
      case ',':
        returnValues[j++] = sign?-temp:temp;
        sign = 0;
        temp = 0;
        break;
      case '-':
        sign = 1;
        break;
      default:
        temp = temp * 10 + *(command + i) - 48;
    }
    i++;
  }
  // set last return value
  returnValues[j] = sign?-temp:temp;
}