Serial sent delay time

BT_Bot_4_X.pde (8242Bytes)

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.

I have a suggestion that would require you to rework your code.

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.

Thank you for the

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

Sorry for my short reply.

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

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

Thanks for the help so far,

Thanks for the help so far, but after a weekend of reading & trying I still don’t understand enough to make it work.

If using arduino-1.0.1 do I need to add a libary for the “string” command, or anything else?

Do I need to declare “counter = 0” or anything else before the viod loop?

Am I correct Before it was:  if (incomingByte == ‘A’)     Now it needs to be:  if (incomingByte == “A”) ?

If I change the app to send:

U\0 = Forward

D\0 = Backward

S\0 = Stop

Does this mean sending “U3000D3000S\0” would = Forward for 3seconds then Backward for 3seconds then stop?

I’m unsure if I have correctly added the code you provide, below is the list of errors I get & below that is my latest attempt at adding your code.

Thank you again for any help you may be able to provide.

 

BT_Bot_4_X3.cpp: In function ‘void loop()’:
BT_Bot_4_X3:80: error: invalid types ‘int[int]’ for array subscript
BT_Bot_4_X3:84: error: ‘string’ was not declared in this scope
BT_Bot_4_X3:84: error: expected ;' before 'command'<br />BT_Bot_4_X3:85: error: 'delaystr' was not declared in this scope<br />BT_Bot_4_X3:85: error: 'command' was not declared in this scope<br />BT_Bot_4_X3:88: error: expected }’ before ‘else’
BT_Bot_4_X3:89: error: ‘string’ was not declared in this scope
BT_Bot_4_X3:89: error: expected `;’ before 'command’
BT_Bot_4_X3:98: error: ISO C++ forbids comparison between pointer and integer
BT_Bot_4_X3:104: error: ISO C++ forbids comparison between pointer and integer
BT_Bot_4_X3:110: error: ISO C++ forbids comparison between pointer and integer
BT_Bot_4_X3:116: error: ISO C++ forbids comparison between pointer and integer
BT_Bot_4_X3:122: error: ISO C++ forbids comparison between pointer and integer
BT_Bot_4_X3:128: error: ISO C++ forbids comparison between pointer and integer
BT_Bot_4_X3:134: error: ISO C++ forbids comparison between pointer and integer
BT_Bot_4_X3:140: error: ISO C++ forbids comparison between pointer and integer
BT_Bot_4_X3:146: error: ISO C++ forbids comparison between pointer and integer
BT_Bot_4_X3:152: error: ISO C++ forbids comparison between pointer and integer
BT_Bot_4_X3:158: error: ISO C++ forbids comparison between pointer and integer
BT_Bot_4_X3:163: error: ISO C++ forbids comparison between pointer and integer
BT_Bot_4_X3:168: error: ISO C++ forbids comparison between pointer and integer
BT_Bot_4_X3:173: error: ISO C++ forbids comparison between pointer and integer
BT_Bot_4_X3:178: error: ISO C++ forbids comparison between pointer and integer
BT_Bot_4_X3.cpp: At global scope:
BT_Bot_4_X3:186: error: expected declaration before ‘}’ token

 

// BT_Bot_4_X3
// Uses these keys (upper Case)
// U\0 (85) = Forward
// D\0 (68) = Backward
// L\0 (76) = Left
// R\0 (82) = Right
// S\0 (83) = Stop
// A\0 (65) = Turn On LED1
// B\0 (66) = Turn Off LED1
// C\0 (67) = Toggle LED2 ON/OFF
// Z\0 (90) = Do a 180 u-turn (backwards to the left) & then go forward.
// Y\0 (89) = Do a 360 turn (stop turn right) & then go forward.
// Uses these keys for Pan & Tilt (lower Case)
// u\0 (117) = Tilt Up
// d\0 (100) = Tilt Down
// l\0 (108) = Pan Left
// r\0 (114) = Pan Right
// c\0 (99) = Reset To Center

#include <DigitalToggle.h>
#include <Servo.h>


/////////////////////////////////////////////////////////////////
int incomingByte;      // a variable to read incoming serial data into

int LED1 = 8;     // LED1 = seperate ON & OFF
int LED2 = 13;    // LED2 = toggle ON & OFF

/////////// L298 & L293D motor control (H-Bridge) ////////////////

// Left Motor
int motor1Pin1 = 5;    // pin 7 on L293D //IN4
int motor1Pin2 = 7;    // pin 2 on L293D //IN3
int enablePinB = 6;    // pin 1 on L293D //ENB

// Right Motor
int motor2Pin3 = 2;    // pin 10 on L293D //IN2
int motor2Pin4 = 4;    // pin 15 on L293D //IN1
int enablePinA = 3;    // pin 9 on L293D //ENA


/////////// Variables for time delays //////////////// 

int counter = 0;// delay time for forward

/////////// Pan & Tilt ////////////////

Servo pan_servo;
Servo tilt_servo;

void setup() {
  // initialize serial communication:
  Serial.begin(9600);

  pinMode(LED1, OUTPUT);// LED1 = seperate ON & OFF
  pinMode(LED2, OUTPUT);// LED2 = toggle ON & OFF

  pinMode(motor1Pin1, OUTPUT);
  pinMode(motor1Pin2, OUTPUT);
  pinMode(enablePinB, OUTPUT);
  digitalWrite(enablePinB, HIGH);// set enablePinB high so that motor1 can turn on:

  pinMode(motor2Pin3, OUTPUT);
  pinMode(motor2Pin4, OUTPUT);
  pinMode(enablePinA, OUTPUT);
  digitalWrite(enablePinA, HIGH);// set enablePinA high so that motor2 can turn on:

  // attach the servos and startup the serial connection
  pan_servo.attach(10);
  tilt_servo.attach(11);
  resetAll();

}

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

  // see if there’s incoming serial data:
  if (Serial.available() > 0) {
    // read the oldest byte in the serial buffer:
    incomingByte = Serial.read();
    // if incoming byte is a A, turn on the LED:
    if (incomingByte == “A”) {
      Serial.print("Duino received: ");
      Serial.println(incomingByte, DEC);// Print number to serial monitor
      digitalWrite(LED1, HIGH);
    }
    // if incoming byte is a B, turn off the LED:
    else if (incomingByte == “B”) {
      Serial.print("Duino received: ");
      Serial.println(incomingByte, DEC);// Print number to serial monitor
      digitalWrite(LED1, LOW);
    }
    // if incoming byte is a C, Toggle LED2 ON/OFF:
    else if (incomingByte == “C”) {
      Serial.print("Duino received: ");
      Serial.println(incomingByte, DEC);// Print number to serial monitor
      digitalToggle(LED2);
    }
    // if incoming byte is a U, go forward:
    else if (incomingByte == “U”) {
      Serial.print("Duino received: ");
      Serial.println(incomingByte, DEC);// Print number to serial monitor
      moveforward();
    }
    // if incoming byte is a D, go backwards:
    else if (incomingByte == “D”) {
      Serial.print("Duino received: ");
      Serial.println(incomingByte, DEC);// Print number to serial monitor
      movebackward();
    }
    // if incoming byte is a S, stop:
    else if (incomingByte == “S”) {
      Serial.print("Duino received: ");
      Serial.println(incomingByte, DEC);// Print number to serial monitor
      movestop();
    }
    // if incoming byte is a R, turn right:   
    else if (incomingByte == “R”) {
      Serial.print("Duino received: ");
      Serial.println(incomingByte, DEC);// Print number to serial monitor
      moveright();
    }
    // if incoming byte is a L, turn left:   
    else if (incomingByte == “L”) {
      Serial.print("Duino received: ");
      Serial.println(incomingByte, DEC);// Print number to serial monitor
      moveleft();
    }
    // if incoming byte is a Z, do a 180 u-turn (backwards to the left) & then go forward.
    else if (incomingByte == “Z”) {
      Serial.print("Duino received: ");
      Serial.println(incomingByte, DEC);// Print number to serial monitor
      move180();
    }
    // if incoming byte is a Y, do a 360 turn (stop turn right) & then go forward.
    else if (incomingByte == “Y”) {
      Serial.print("Duino received: ");
      Serial.println(incomingByte, DEC);// Print number to serial monitor
      move360();
    }
    // Uses these keys for Pan & Tilt
    else if (incomingByte == “u”) {
      Serial.print("Duino received: ");
      Serial.println(incomingByte, DEC);// Print number to serial monitor
      moveServo(tilt_servo, 1);
    }
    else if (incomingByte == “d”) {
      Serial.print("Duino received: ");
      Serial.println(incomingByte, DEC);// Print number to serial monitor
      moveServo(tilt_servo, -1);
    }
    else if (incomingByte == “l”) {
      Serial.print("Duino received: ");
      Serial.println(incomingByte, DEC);// Print number to serial monitor
      moveServo(pan_servo, 1);
    }
    else if (incomingByte == “r”) {
      Serial.print("Duino received: ");
      Serial.println(incomingByte, DEC);// Print number to serial monitor
      moveServo(pan_servo, -1);
    }
    else if (incomingByte == “c”) {
      Serial.print("Duino received: ");
      Serial.println(incomingByte, DEC);// Print number to serial monitor
      resetAll();
    }
  }

}
}
}
// move the servo a given amount
void moveServo(Servo servo, int delta) {
  int previousValue = servo.read();
  int newValue = previousValue + delta;
  if (newValue > 180 || newValue < 30) {
    return;
  }
  servo.write(newValue);
}

// put the servos back to the “default” position
void resetAll() {
  reset(pan_servo);
  reset(tilt_servo);
}

// put a servo back to the “default” position (100 deg)
void reset(Servo servo) {
  int newPos = 100;
  int previousPos = servo.read();
  if (newPos > previousPos) {
    for (int i=previousPos; i<newPos; i++) {
      servo.write(i);
      delay(15);
    }
  }
  else {
    for (int i=previousPos; i>newPos; i–) {
      servo.write(i);
      delay(15);
    }
  }
}
void moveforward() {
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, HIGH);
  digitalWrite(motor2Pin3, HIGH);
  digitalWrite(motor2Pin4, LOW);
}
void movebackward() {
  digitalWrite(motor1Pin1, HIGH);
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor2Pin3, LOW);
  digitalWrite(motor2Pin4, HIGH);
}
void movestop() {
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor2Pin3, LOW);
  digitalWrite(motor2Pin4, LOW);
}
void moveright() {
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, HIGH);
  digitalWrite(motor2Pin3, LOW);
  digitalWrite(motor2Pin4, HIGH);
}
void moveleft() {
  digitalWrite(motor1Pin1, HIGH);
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor2Pin3, HIGH);
  digitalWrite(motor2Pin4, LOW);
}
void move180() {
  movebackward();
  delay(500);//go backwards for 0.5 seconds
  moveleft();
  delay(1700);//turn left for 1.8 seconds
  moveforward();//go forward
}
void move360() {
  movestop();
  delay(200);//stop for 0.2 seconds
  moveright();
  delay(3400);//turn Right for 3.5 seconds
  moveforward();//go forward
}


You need to do a bit more research.

You should look at http://arduino.cc/en/Reference/StringConstructor . I made a syntax(?) error calling string command. It should have been at the very least String command. The first error int[int] is due to you calling incomingByte an int earlier in the program. It needs to be declared as char incomingByte[] at the very least. You may need to add a number inside the []. You won’t be able to easily break out more than one command/delay mix from a single string. All the variables that are listed need to be declared properly before they are used. The program as written has some problems, but, they seem to be pretty easy to fix, if you are willing to dig into the language. If I really have to I will step through all your problems, but, I didn’t see anything that was terribly mystical.

Again Thank you very much

Again Thank you very much for the help so far, I wish I could say “I didn’t see anything that was terribly mystical”.

“If I really have to I will step through all your problems” Please don’t feel obligated to continue helping, the help you’ve provide so far is extremely appreciated & I’m sure you have alot on your plate already.

“if you are willing to dig into the language” Yes very willing to learn, most of links you’ve provided I have read before (& again & again & again since). My problem is I have something called Asperger’s & therfore find learning this on my own more difficult than it probably is for most people.

If you are able to continue helping, my next few questions are below followed by the first few errors I get, & below that is my latest code.

Thank you again for all help you have provided.

I know it says “There are no stupid questions. Ask here.” but I feel some of mine are, if so I apolagize.

Q1:Dont understand why " error: ‘class String’ has no member named ‘substr’" also tried “str.substr” as per the example in this link http://www.cplusplus.com/reference/string/string/substr/

Q2: “All the variables that are listed need to be declared properly before they are used” is my current attempt correct or should it be char instead of int before counter = 0; & delaystr = 0;

Q3:Does this “You won’t be able to easily break out more than one command/delay mix from a single string” mean I can do: “U3000S\0”  = Forward for 3seconds then stop? But can’t do: “U3000D3000S\0”  = Forward for 3seconds then Backward for 3seconds then stop?

 

 

BT_Bot_4_X3.cpp: In function ‘void loop()’:
BT_Bot_4_X3:86: error: ‘class String’ has no member named 'substr’
BT_Bot_4_X3:87: error: invalid conversion from ‘int’ to 'const char*'
BT_Bot_4_X3:87: error: initializing argument 1 of 'int atoi(const char*)'
BT_Bot_4_X3:87: error: assignment of function 'void delay(long unsigned int)'
BT_Bot_4_X3:87: error: cannot convert ‘int’ to ‘void ()(long unsigned int)’ in assignment
BT_Bot_4_X3:88: error: ‘class String’ has no member named 'substr’
BT_Bot_4_X3:98: error: incompatible types in assignment of ‘int’ to 'char [0]'
BT_Bot_4_X3:102: error: call of overloaded ‘println(char [0], int)’ is ambiguous
C:\arduino-1.0.1\hardware\arduino\cores\arduino/Print.h:68: note: candidates are: size_t Print::println(unsigned char, int) <near match>
C:\arduino-1.0.1\hardware\arduino\cores\arduino/Print.h:69: note:                 size_t Print::println(int, int) <near match>
C:\arduino-1.0.1\hardware\arduino\cores\arduino/Print.h:70: note:                 size_t Print::println(unsigned int, int) <near match>
C:\arduino-1.0.1\hardware\arduino\cores\arduino/Print.h:71: note:                 size_t Print::println(long int, int) <near match>
C:\arduino-1.0.1\hardware\arduino\cores\arduino/Print.h:72: note:                 size_t Print::println(long unsigned int, int) <near match>

 

 

#include <DigitalToggle.h>
#include <Servo.h>


/////////////////////////////////////////////////////////////////
char incomingByte[0];      // a variable to read incoming serial data into

int LED1 = 8;     // LED1 = seperate ON & OFF
int LED2 = 13;    // LED2 = toggle ON & OFF

/////////// L298 & L293D motor control (H-Bridge) ////////////////

// Left Motor
int motor1Pin1 = 5;    // pin 7 on L293D //IN4
int motor1Pin2 = 7;    // pin 2 on L293D //IN3
int enablePinB = 6;    // pin 1 on L293D //ENB

// Right Motor
int motor2Pin3 = 2;    // pin 10 on L293D //IN2
int motor2Pin4 = 4;    // pin 15 on L293D //IN1
int enablePinA = 3;    // pin 9 on L293D //ENA


/////////// Variables for time delays //////////////// 

int counter = 0;// delay time for forward
int delaystr = 0;

/////////// Pan & Tilt ////////////////

Servo pan_servo;
Servo tilt_servo;

void setup() {
  // initialize serial communication:
  Serial.begin(9600);

  pinMode(LED1, OUTPUT);// LED1 = seperate ON & OFF
  pinMode(LED2, OUTPUT);// LED2 = toggle ON & OFF

  pinMode(motor1Pin1, OUTPUT);
  pinMode(motor1Pin2, OUTPUT);
  pinMode(enablePinB, OUTPUT);
  digitalWrite(enablePinB, HIGH);// set enablePinB high so that motor1 can turn on:

  pinMode(motor2Pin3, OUTPUT);
  pinMode(motor2Pin4, OUTPUT);
  pinMode(enablePinA, OUTPUT);
  digitalWrite(enablePinA, HIGH);// set enablePinA high so that motor2 can turn on:

  // attach the servos and startup the serial connection
  pan_servo.attach(10);
  tilt_servo.attach(11);
  resetAll();

}

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

  // see if there’s incoming serial data:
  if (Serial.available() > 0) {
    // read the oldest byte in the serial buffer:
    incomingByte = Serial.read();
    // if incoming byte is a A, turn on the LED:
    if (incomingByte == “A”) {
      Serial.print("Duino received: ");
      Serial.println(incomingByte, DEC);// Print number to serial monitor
      digitalWrite(LED1, HIGH);
    }
    // if incoming byte is a B, turn off the LED:
    else if (incomingByte == “B”) {
      Serial.print("Duino received: ");
      Serial.println(incomingByte, DEC);// Print number to serial monitor
      digitalWrite(LED1, LOW);
    }
    // if incoming byte is a C, Toggle LED2 ON/OFF:
    else if (incomingByte == “C”) {
      Serial.print("Duino received: ");
      Serial.println(incomingByte, DEC);// Print number to serial monitor
      digitalToggle(LED2);
    }
    // if incoming byte is a U, go forward:
    else if (incomingByte == “U”) {
      Serial.print("Duino received: ");
      Serial.println(incomingByte, DEC);// Print number to serial monitor
      moveforward();
    }
    // if incoming byte is a D, go backwards:
    else if (incomingByte == “D”) {
      Serial.print("Duino received: ");
      Serial.println(incomingByte, DEC);// Print number to serial monitor
      movebackward();
    }
    // if incoming byte is a S, stop:
    else if (incomingByte == “S”) {
      Serial.print("Duino received: ");
      Serial.println(incomingByte, DEC);// Print number to serial monitor
      movestop();
    }
    // if incoming byte is a R, turn right:   
    else if (incomingByte == “R”) {
      Serial.print("Duino received: ");
      Serial.println(incomingByte, DEC);// Print number to serial monitor
      moveright();
    }
    // if incoming byte is a L, turn left:   
    else if (incomingByte == “L”) {
      Serial.print("Duino received: ");
      Serial.println(incomingByte, DEC);// Print number to serial monitor
      moveleft();
    }
    // if incoming byte is a Z, do a 180 u-turn (backwards to the left) & then go forward.
    else if (incomingByte == “Z”) {
      Serial.print("Duino received: ");
      Serial.println(incomingByte, DEC);// Print number to serial monitor
      move180();
    }
    // if incoming byte is a Y, do a 360 turn (stop turn right) & then go forward.
    else if (incomingByte == “Y”) {
      Serial.print("Duino received: ");
      Serial.println(incomingByte, DEC);// Print number to serial monitor
      move360();
    }
    // Uses these keys for Pan & Tilt
    else if (incomingByte == “u”) {
      Serial.print("Duino received: ");
      Serial.println(incomingByte, DEC);// Print number to serial monitor
      moveServo(tilt_servo, 1);
    }
    else if (incomingByte == “d”) {
      Serial.print("Duino received: ");
      Serial.println(incomingByte, DEC);// Print number to serial monitor
      moveServo(tilt_servo, -1);
    }
    else if (incomingByte == “l”) {
      Serial.print("Duino received: ");
      Serial.println(incomingByte, DEC);// Print number to serial monitor
      moveServo(pan_servo, 1);
    }
    else if (incomingByte == “r”) {
      Serial.print("Duino received: ");
      Serial.println(incomingByte, DEC);// Print number to serial monitor
      moveServo(pan_servo, -1);
    }
    else if (incomingByte == “c”) {
      Serial.print("Duino received: ");
      Serial.println(incomingByte, DEC);// Print number to serial monitor
      resetAll();
    }
  }

}
}
}
// move the servo a given amount
void moveServo(Servo servo, int delta) {
  int previousValue = servo.read();
  int newValue = previousValue + delta;
  if (newValue > 180 || newValue < 30) {
    return;
  }
  servo.write(newValue);
}

// put the servos back to the “default” position
void resetAll() {
  reset(pan_servo);
  reset(tilt_servo);
}

// put a servo back to the “default” position (100 deg)
void reset(Servo servo) {
  int newPos = 100;
  int previousPos = servo.read();
  if (newPos > previousPos) {
    for (int i=previousPos; i<newPos; i++) {
      servo.write(i);
      delay(15);
    }
  }
  else {
    for (int i=previousPos; i>newPos; i–) {
      servo.write(i);
      delay(15);
    }
  }
}
void moveforward() {
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, HIGH);
  digitalWrite(motor2Pin3, HIGH);
  digitalWrite(motor2Pin4, LOW);
}
void movebackward() {
  digitalWrite(motor1Pin1, HIGH);
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor2Pin3, LOW);
  digitalWrite(motor2Pin4, HIGH);
}
void movestop() {
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor2Pin3, LOW);
  digitalWrite(motor2Pin4, LOW);
}
void moveright() {
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, HIGH);
  digitalWrite(motor2Pin3, LOW);
  digitalWrite(motor2Pin4, HIGH);
}
void moveleft() {
  digitalWrite(motor1Pin1, HIGH);
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor2Pin3, HIGH);
  digitalWrite(motor2Pin4, LOW);
}
void move180() {
  movebackward();
  delay(500);//go backwards for 0.5 seconds
  moveleft();
  delay(1700);//turn left for 1.8 seconds
  moveforward();//go forward
}
void move360() {
  movestop();
  delay(200);//stop for 0.2 seconds
  moveright();
  delay(3400);//turn Right for 3.5 seconds
  moveforward();//go forward
}


My plate is fairly empty. :slight_smile:

Somedays I am an acceptable teacher and others I might be mistaken for a donkey/mule. :stuck_out_tongue:

BT_Bot_4_X3:86: error: ‘class String’ has no member named 'substr’
Arduino does not have substr. They instead have substring. The substring function can take one or two extra values. It is in the form of stringvariable.substring(from,[to]) . The to value is optional, if it is left out the stringvariable will be cut starting at the from index to the end of the string. The index starts at 0 and then increments by 1 for each following character. In your case, if you send U3000, U is index 0, 3 is index 1, and so on to the end of the string.

BT_Bot_4_X3:87: error: invalid conversion from ‘int’ to 'const char*'
I believe all of the following are probably due to the variable mismatch as well:

BT_Bot_4_X3:87: error: initializing argument 1 of 'int atoi(const char*)'
BT_Bot_4_X3:87: error: assignment of function 'void delay(long unsigned int)'
BT_Bot_4_X3:87: error: cannot convert ‘int’ to ‘void ()(long unsigned int)’ in assignment
int delaystr = 0;
You need to change delaystr to a String variable. I called it delaystr because I wanted to tell myself that it was a string variable holding the delay length. It will then be converted to an int and be able to be used as a int in a delay call.

BT_Bot_4_X3:98: error: incompatible types in assignment of ‘int’ to 'char [0]'
You need to get rid of or just comment out:
 // see if there’s incoming serial data:
  if (Serial.available() > 0) {
    // read the oldest byte in the serial buffer:
    incomingByte = Serial.read();

You will also need to get rid of the closing } that goes with the above IF.

BT_Bot_4_X3:102: error: call of overloaded ‘println(char [0], int)’ is ambiguous
I am not sure about this one. Let us get the other errors covered first. :slight_smile:

Now to get to your questions. :slight_smile:

Q0: Are your questions stupid?
A: Nope. I am an ass. :smiley: If you don’t ask, we can’t help you. :slight_smile:

Q1: What is up with substr?
A: Already answered, but, I will answer again. Arduino doesn’t know substr, but, it does know substring. This might help with the String objects: http://arduino.cc/en/Reference/StringObject

Q2: “All the variables that are listed need to be declared properly before they are used” is my current attempt correct or should it be char instead of int before counter = 0; & delaystr = 0;
A: counter is just a counting variable or an int. delaystr, as I mentioned earlier should be declared as a String. When the program gets to it, the String variable delaystr should contain the requested delay as a string variable. Shortly after that has occurred, it will be converted to an interger and placed in the variable delay.

Q3: Does this “You won’t be able to easily break out more than one command/delay mix from a single string” mean I can do: “U3000S\0”  = Forward for 3seconds then stop? But can’t do: “U3000D3000S\0”  = Forward for 3seconds then Backward for 3seconds then stop?
A: I have not considered trying to grab more than one command and one delay from an incoming serial string. I am sure it is possible, but, it will require a fair bit more programming.
After re-reading my post, I realized I hadn’t answered your question. I believe, at least for starters, we should get you capable of being able to send either a command or a command and a delay and get the robot to be able to decypher what you are telling it. Multiple commands, I believe, will require a good deal more programming to get everything broken out into a usable form. 

Hit me up when you need some more assistance.

 

Thanks for the continued

Thanks for the continued help.

“Somedays I am an acceptable teacher and others I might be mistaken for a donkey/mule. :P” So far your teaching style is fine by me & very appreciated.

I put the project aside for a day in the hope that fresh eyes would help me understand. I don’t think it made much difference to my ability to understand.
I’m not to worried if I can’t send multipule commands and delays, I would be extremly happy if I could just do something like :

When “U\0” is received  the bot go’s Forward until Stop is received. (this part is working)
When “D\0” is received  the bot go’s Backward until Stop is received.
(this part is working)
When “S\0” is received  the bot Stops.
(this part is working)
When “UX\0” is received  the bot go’s Forward for X amount of seconds then Stops.
(stuck here)
When “DX\0” is received  the bot go’s Backward for X amount of seconds then Stops.
(stuck here)

X being the time delay number set & sent from the app.

Q1:So do I atleast understand this: 

String command ( incomingByte ); // this may need to be ( incomingByte, counter )
delaystr = command.substring ( 2 ); // get the last portion of the command
delay = atoi ( delaystr ); //atoi ascii to integer
command.substring( 1, 1 );// get the command

My analogy:
The word “command” is sort of like a notepad
& the word “substring” is sort of like a page of the that notepad
& this here “( 2 )” is the (from) index
& this here “( 1, 1 )” is the (from,[to]) indexs
& the (from,[to]) indexs are sort of like a word written on a page of that notepad, & Say if the word was “Robot” then the letter “R” would be index 0 & the letter “T” would be index 4.

Q2:So if using the above analogy, is the below statement correct:

String notepad1 ( incomingByte );
delaystr = notepad1.Page1 ( 2 );
delay = atoi ( delaystr );
notepad1.Page1( 1, 1 );

Q3:Where I have changed “substr” to “substring”, I shouldn’t have done this as “substr” is realy just short for:

This is the substring of the String named command.

Q4:Am I stil not understanding how & where to declare a String properly.

Thanks Again

 

Below is the first few errors I now get, & below that is my latest code:

BT_Bot_4_X4.cpp: In function ‘void loop()’:
BT_Bot_4_X4:87: error: cannot convert ‘String’ to ‘const char*’ for argument ‘1’ to 'int atoi(const char*)'
BT_Bot_4_X4:98: error: call of overloaded ‘println(char [0], int)’ is ambiguous
C:\arduino-1.0.1\hardware\arduino\cores\arduino/Print.h:68: note: candidates are: size_t Print::println(unsigned char, int) <near match>
C:\arduino-1.0.1\hardware\arduino\cores\arduino/Print.h:69: note:                 size_t Print::println(int, int) <near match>
C:\arduino-1.0.1\hardware\arduino\cores\arduino/Print.h:70: note:                 size_t Print::println(unsigned int, int) <near match>
C:\arduino-1.0.1\hardware\arduino\cores\arduino/Print.h:71: note:                 size_t Print::println(long int, int) <near match>
C:\arduino-1.0.1\hardware\arduino\cores\arduino/Print.h:72: note:                 size_t Print::println(long unsigned int, int) <near match>
BT_Bot_4_X4:104: error: call of overloaded ‘println(char [0], int)’ is ambiguous

 



#include <DigitalToggle.h>
#include <Servo.h>


/////////////////////////////////////////////////////////////////
char incomingByte[0];      // a variable to read incoming serial data into

int LED1 = 8;     // LED1 = seperate ON & OFF
int LED2 = 13;    // LED2 = toggle ON & OFF

/////////// L298 & L293D motor control (H-Bridge) ////////////////

// Left Motor
int motor1Pin1 = 5;    // pin 7 on L293D //IN4
int motor1Pin2 = 7;    // pin 2 on L293D //IN3
int enablePinB = 6;    // pin 1 on L293D //ENB

// Right Motor
int motor2Pin3 = 2;    // pin 10 on L293D //IN2
int motor2Pin4 = 4;    // pin 15 on L293D //IN1
int enablePinA = 3;    // pin 9 on L293D //ENA


/////////// Variables for time delays //////////////// 

int counter = 0;// delay time for forward
String delaystr = 0;

/////////// Pan & Tilt ////////////////

Servo pan_servo;
Servo tilt_servo;

void setup() {
  // initialize serial communication:
  Serial.begin(9600);

  pinMode(LED1, OUTPUT);// LED1 = seperate ON & OFF
  pinMode(LED2, OUTPUT);// LED2 = toggle ON & OFF

  pinMode(motor1Pin1, OUTPUT);
  pinMode(motor1Pin2, OUTPUT);
  pinMode(enablePinB, OUTPUT);
  digitalWrite(enablePinB, HIGH);// set enablePinB high so that motor1 can turn on:

  pinMode(motor2Pin3, OUTPUT);
  pinMode(motor2Pin4, OUTPUT);
  pinMode(enablePinA, OUTPUT);
  digitalWrite(enablePinA, HIGH);// set enablePinA high so that motor2 can turn on:

  // attach the servos and startup the serial connection
  pan_servo.attach(10);
  tilt_servo.attach(11);
  resetAll();

}

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.substring ( 2 ); // get the last portion of the command
delay = atoi ( delaystr ); //atoi ascii to integer
command.substring( 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

    // if incoming byte is a A, turn on the LED:
    if (incomingByte == “A”) {
      Serial.print("Duino received: ");
      Serial.println(incomingByte, DEC);// Print number to serial monitor
      digitalWrite(LED1, HIGH);
    }
    // if incoming byte is a B, turn off the LED:
    else if (incomingByte == “B”) {
      Serial.print("Duino received: ");
      Serial.println(incomingByte, DEC);// Print number to serial monitor
      digitalWrite(LED1, LOW);
    }
    // if incoming byte is a C, Toggle LED2 ON/OFF:
    else if (incomingByte == “C”) {
      Serial.print("Duino received: ");
      Serial.println(incomingByte, DEC);// Print number to serial monitor
      digitalToggle(LED2);
    }
    // if incoming byte is a U, go forward:
    else if (incomingByte == “U”) {
      Serial.print("Duino received: ");
      Serial.println(incomingByte, DEC);// Print number to serial monitor
      moveforward();
    }
    // if incoming byte is a D, go backwards:
    else if (incomingByte == “D”) {
      Serial.print("Duino received: ");
      Serial.println(incomingByte, DEC);// Print number to serial monitor
      movebackward();
    }
    // if incoming byte is a S, stop:
    else if (incomingByte == “S”) {
      Serial.print("Duino received: ");
      Serial.println(incomingByte, DEC);// Print number to serial monitor
      movestop();
    }
    // if incoming byte is a R, turn right:   
    else if (incomingByte == “R”) {
      Serial.print("Duino received: ");
      Serial.println(incomingByte, DEC);// Print number to serial monitor
      moveright();
    }
    // if incoming byte is a L, turn left:   
    else if (incomingByte == “L”) {
      Serial.print("Duino received: ");
      Serial.println(incomingByte, DEC);// Print number to serial monitor
      moveleft();
    }
    // if incoming byte is a Z, do a 180 u-turn (backwards to the left) & then go forward.
    else if (incomingByte == “Z”) {
      Serial.print("Duino received: ");
      Serial.println(incomingByte, DEC);// Print number to serial monitor
      move180();
    }
    // if incoming byte is a Y, do a 360 turn (stop turn right) & then go forward.
    else if (incomingByte == “Y”) {
      Serial.print("Duino received: ");
      Serial.println(incomingByte, DEC);// Print number to serial monitor
      move360();
    }
    // Uses these keys for Pan & Tilt
    else if (incomingByte == “u”) {
      Serial.print("Duino received: ");
      Serial.println(incomingByte, DEC);// Print number to serial monitor
      moveServo(tilt_servo, 1);
    }
    else if (incomingByte == “d”) {
      Serial.print("Duino received: ");
      Serial.println(incomingByte, DEC);// Print number to serial monitor
      moveServo(tilt_servo, -1);
    }
    else if (incomingByte == “l”) {
      Serial.print("Duino received: ");
      Serial.println(incomingByte, DEC);// Print number to serial monitor
      moveServo(pan_servo, 1);
    }
    else if (incomingByte == “r”) {
      Serial.print("Duino received: ");
      Serial.println(incomingByte, DEC);// Print number to serial monitor
      moveServo(pan_servo, -1);
    }
    else if (incomingByte == “c”) {
      Serial.print("Duino received: ");
      Serial.println(incomingByte, DEC);// Print number to serial monitor
      resetAll();
    }
}
}
}
// move the servo a given amount
void moveServo(Servo servo, int delta) {
  int previousValue = servo.read();
  int newValue = previousValue + delta;
  if (newValue > 180 || newValue < 30) {
    return;
  }
  servo.write(newValue);
}

// put the servos back to the “default” position
void resetAll() {
  reset(pan_servo);
  reset(tilt_servo);
}

// put a servo back to the “default” position (100 deg)
void reset(Servo servo) {
  int newPos = 100;
  int previousPos = servo.read();
  if (newPos > previousPos) {
    for (int i=previousPos; i<newPos; i++) {
      servo.write(i);
      delay(15);
    }
  }
  else {
    for (int i=previousPos; i>newPos; i–) {
      servo.write(i);
      delay(15);
    }
  }
}
void moveforward() {
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, HIGH);
  digitalWrite(motor2Pin3, HIGH);
  digitalWrite(motor2Pin4, LOW);
}
void movebackward() {
  digitalWrite(motor1Pin1, HIGH);
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor2Pin3, LOW);
  digitalWrite(motor2Pin4, HIGH);
}
void movestop() {
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor2Pin3, LOW);
  digitalWrite(motor2Pin4, LOW);
}
void moveright() {
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, HIGH);
  digitalWrite(motor2Pin3, LOW);
  digitalWrite(motor2Pin4, HIGH);
}
void moveleft() {
  digitalWrite(motor1Pin1, HIGH);
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor2Pin3, HIGH);
  digitalWrite(motor2Pin4, LOW);
}
void move180() {
  movebackward();
  delay(500);//go backwards for 0.5 seconds
  moveleft();
  delay(1700);//turn left for 1.8 seconds
  moveforward();//go forward
}
void move360() {
  movestop();
  delay(200);//stop for 0.2 seconds
  moveright();
  delay(3400);//turn Right for 3.5 seconds
  moveforward();//go forward
}