1.byte dis[6]; //distance package
2.int t,i,pp; //temp value
3.dis[0] = 65; //check value
- case 65: //Return distance value
for(i=5;i>0;i--){
dis = t%10;
- t/=10;*
- }*
- //Pack value*
- for(i=0;i<=5;i++)*
_ BTSerial.write(dis*);_
_ //Return value*_
* break;*
I don't know what the codes mean and what things they can do.
Please help me to explain them.
And if I delete all of code about "dis" and "t", Does my code work?
I have attached the arduino file.
This is my code
```
*#include <SoftwareSerial.h>
#include <Servo.h>
Servo servoRight;
Servo servoLeft;
SoftwareSerial BTSerial(10, 11); // RX, TX
byte dis[6]; //distance package
int t,i,pp; //temp value
void setup(){
Serial.begin(9600);
BTSerial.begin(57600); //BTSerial 9600,19200,38400,57600
servoRight.attach(12);
servoLeft.attach(13);
dis[0] = 65; //check value
}
void loop(){
if (BTSerial.available()>0){
pp=BTSerial.read();
Serial.println(pp);
switch(pp){
case 'f': //Forward
Up();
break;
case 'x': //Back
Down();
break;
case 'a': //Turn Left
Left();
break;
case 'd': //Turn Right
Right();
break;
case 's': //Stop
stop();
break;
case 65: //Return distance value
for(i=5;i>0;i--){
dis[i] = t%10;
t/=10;
}
//Pack value
for(i=0;i<=5;i++)
BTSerial.write(dis[i]);
//Return value
break;
default:
break;
}
}
}
int Up(){
servoRight.writeMicroseconds(1300);
servoLeft.writeMicroseconds(1700);
//delay(1000);
}
int Down(){
servoRight.writeMicroseconds(1700);
servoLeft.writeMicroseconds(1300);
//delay(1000); //視情況調整
}
int Right(){
servoRight.writeMicroseconds(1700);
servoLeft.writeMicroseconds(1700);
}
int Left(){
servoRight.writeMicroseconds(1300);
servoLeft.writeMicroseconds(1300);
}
int stop(){
servoRight.writeMicroseconds(1500);
servoLeft.writeMicroseconds(1500);
}
_```_
Thank you very much:)
_rbtest.ino (1.45 KB)*_
Please make it easier to provide help by posting the whole program here. When you do, select the code in the forum editor and click the code tags icon (</>) top left of the editor window before saving the post. This will prevent silly things happening like parts of the code turning into italics like it did in your original post.
UKHeliBob:
Please make it easier to provide help by posting the whole program here. When you do, select the code in the forum editor and click the code tags icon (</>) top left of the editor window before saving the post. This will prevent silly things happening like parts of the code turning into italics like it did in your original post.
Sorry, I don't post whole program.
I have posted the whole program already.
Thank for your remind 
byte dis[6]; //distance package
declare an array of bytes with 6 elements
int t,i,pp; //temp value
Declare 3 int variables but don't initialise their values
dis[0] = 65; //check value
Set the first element of the array to 65
case 65: //Return distance value
for(i=5;i>0;i--){
dis[i] = t%10;
t/=10;
}
//Pack value
for(i=0;i<=5;i++)
BTSerial.write(dis[i]);
//Return value
break;
default:
break;
When the pp variable (previously read from Bluetooth) equals 65 (almost certainly the character 'A') set the 5 elements of the dis array to the remainder of the value of t divided by 10 (ie each digit) and after each step of the for loop divide t by 10. Then send each of the elements of the dis array (each digit) to the Bluetooth device
UKHeliBob:
byte dis[6]; //distance package
declare an array of bytes with 6 elements
int t,i,pp; //temp value
Declare 3 int variables but don't initialise their values
dis[0] = 65; //check value
Set the first element of the array to 65
case 65: //Return distance value
for(i=5;i>0;i--){
dis[i] = t%10;
t/=10;
}
//Pack value
for(i=0;i<=5;i++)
BTSerial.write(dis[i]);
//Return value
break;
default:
break;
When the pp variable (previously read from Bluetooth) equals 65 (almost certainly the character 'A') set the 5 elements of the dis array to the remainder of the value of t divided by 10 (ie each digit) and after each step of the for loop divide t by 10. Then send each of the elements of the dis array (each digit) to the Bluetooth device
If I delete all of code about "dis" and "t", Does my code work?
jremington:
Try it and see.
But make a backup copy first 
...R
AllenSun:
And if I delete all of code about "dis" and "t", Does my code work?
If you open up the bonnet of your car and just start ripping things out that you don't understand, would you expect your car to work?
No. Everything that is in your engine bay is there for a reason. A car is built out of parts that work together to achieve a purpose.
Lets say that instead of ripping things out of a car, you went to the factory where they make them and got at the blueprints for making a car, and erased stuff you didn't understand. Would you expect the cars coming out o that factory to work?
Computer code is not like a document meant for people to read. It is more like a detailed set of blueprints for a functioning object - the functioning object being some arduino-driven device.
You don't know some of the basics of programming in C++. A person who doesn't know those basics can't make a C++ program work by chopping it up until it looks right.
Google "C++ tutorial".