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
}