Let's Make Robots!

serial sent delay time

I've attached a copy of what I'm using for a simple tank drive bot with pan & tilt setup.

I've also made a couple of android bluetooth apps to control it.

My problem is how to add a statement to each of the following commands like:

"if delay number sent after letter store number to timevar1 for delay & do for the delay time:"

// U (85) = Forward
// D (68) = Backward
// L (76) = Left
// R (82) = Right
// S (83) = Stop
// A (65) = Turn On LED1
// B (66) = Turn Off LED1
// C (67) = Toggle LED2 ON/OFF
// Z (90) = Do a 180 u-turn (backwards to the left) & then go forward.
// Y (89) = Do a 360 turn (stop turn right) & then go forward.

I've set up an app to send "<" before a letter

and ">" after the delay time number.

After hours of reading, the 2 links below come close to explaining what I need to do, but I still dont understand enough to make it work.

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1173924069

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1294835596

I would like to be able to send the pan & tilt commands while the bot runs through a string of motor cotrol commands.

I asume this means I can't use the delay function, so I've added the timer libary, but I've not used timer libary before.

Again after alot of reading still unsure how to implament timer as a delay.

I've also added & edited out some statements of what I think I need for the "U (85) = Forward" command.

I would greatly appreciate it if someone could edit the "U (85) = Forward" command so as to work like the following:

If I send "U" or "<U" the bot go's forward. (this part is working)

If I send "<U3000>S" the bot go's forward for 3 seconds then stops. (stuck here)

Even if someone can make it work using the delay command & there for stopping the pan & tilt commands, I would then be able to use there example to sort out the rest of the commands.

Thanks in advance for any help provided.

AttachmentSize
BT_Bot_4_X.pde8.05 KB

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Thank you for the comment.

I'm up for changing anything to make it work, if I know how.

After reading about "EOL character, be it \0 (null) or CR (carriage return, 13)" & "place them in an array and then parse the array to find out what the actual code" I think I understand  I little more but I still don't know where to start for:

what "statment" would I write If want it to read until it recieves "\0" my new EOL character replacing ">" sent from app, and would this "statment" replace   "if (Serial.available() > 0) {"   just after the   "void loop() {"   ?

what statment would I write to use what has been read and put it into the variable "timevar1" ?   timevar1 = Serial.read()  ?

How do I "place them in an array and then parse the array to find out what the actual code is" ?

If you could piont me some where that has info or tutorial with pics or video using code doing something simalar to:
If I send "U" or "0/U" LED1 turns on
If I send "0/U3000\0S" LED1 turns on for 3 seconds then turns off

Sorry for so many qusetions?

Probably should add the app was made with app inventor, which for the most part I understand, but this I strugle to understand without an example.

 

Thanks again

First, I would get rid of the extraneous characters. As in, don't send them. So, instead of < or > just send the characters you want to send, and, when you are finished with a command send \0.

I hope the following code works and will help you.

void loop() {
  // see if there's incoming serial data:
  counter = 0
  while ( Serial.available() != 0 ) {
    // read the oldest byte in the serial buffer:
    incomingByte[counter] = Serial.read(); //incomingByte is a char array
    counter++;
  }

  if ( counter > 1 ) { 
    string command ( incomingByte ); // this may need to be ( incomingByte, counter ) 
    delaystr = command.substr ( 2 ); // get the last portion of the command
    delay = atoi ( delaystr ); //atoi ascii to integer
    command.substr( 1, 1 ); // get the command
  else
    string command ( incomingByte );
  //realize that now the command is a string and not a character. It 
  //will require " " instead of ' '. Follow this with your original if statements
  }
}

Reference

http://www.velocityreviews.com/forums/t277231-char-array-to-string.html

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1294917216

http://www.cplusplus.com/reference/clibrary/cstdlib/atoi/

http://www.cplusplus.com/reference/string/string/substr/

Thanks heaps for all the info very helpful.

Will have a go at incorporating the code you've kindy written after I've had a chance to read all the links properly.

Thanks again

Instead of checking for each individual character as it comes in, I would suggest you read the data until you get some kind of EOL character, be it \0 (null) or CR (carriage return, 13). As you read the bytes in, you can place them in an array and then parse the array to find out what the actual code is that was received.