The Arduino machine, controlled by an Android device via Bluetooth, is the application code and mk (part 2)
About the first part of
In the first part, I described the physical part of the design and only a small chunk of code. Now consider the software component - the Android application and the Arduino sketch.
First, I will give a detailed description of each moment, and at the end I will leave the links to the whole project + video of the result, which should you
github.com/IDolgopolov/BluetoothWorkAPP.git . There the code without comments, therefore it looks much cleaner, less and easier.
Sketch Arduino
Android application is dismantled, written, understood and then it will be easier. I'll try to consider everything step by step, and then I'll give a link to the full file.
The variables are
First, let's look at the constants and variables you need.
#include
//reassign the bluetooth input pin
//do not have to take it out while filling the sketch onto the board
SoftwareSerial BTSerial (? 9);
//turn and speed pins
int speedRight = 6;
int dirLeft = 3;
int speedLeft = 11;
int dirRight = 7;
//pins of the engine turning the wheels
int angleDirection = 4;
int angleSpeed = 5;
//a pin to which plus pieces are attached, defining the rotation
//the detailed technology is described in the first part of
int pinAngleStop = 12;
//here we will write the values
String val;
//rotation speed
int speedTurn = 180;
//the pins that define the rotation
//table and descriptions of the system in the first article
int pinRed = A0;
int pinWhite = A1;
int pinBlack = A2;
//variable for the time
long lastTakeInformation;
//variables indicating that
will now be read. boolean readAngle = false;
boolean readSpeed = false;
The setup () method is
In the setup () method, we set the parameters of the pins: they will work on the input or output. Also, we will establish the speed of communication between the computer and the Arduinka, the bluetooth with the Arduinka.
void setup () {
pinMode (dirLeft, OUTPUT);
pinMode (speedLeft, OUTPUT);
pinMode (dirRight, OUTPUT);
pinMode (speedRight, OUTPUT);
pinMode (pinRed, INPUT);
pinMode (pinBlack, INPUT);
pinMode (pinWhite, INPUT);
pinMode (pinAngleStop, OUTPUT);
pinMode (angleDirection, OUTPUT);
pinMode (angleSpeed, OUTPUT);
//this speed is relevant only for model HC-05
//if you have a different version of the module, see the documentation
BTSerial.begin (38400);
//this speed is constant
Serial.begin (9600);
}
The loop () method and the additional functions
In a recurring loop () method, data is read. First, consider the main algorithm, and then the functions involved in it.
void loop () {
//if at least unread bytes are
if (BTSerial.available ()> 0) {
//read the last unread byte
char a = BTSerial.read ();
if (a == '@') {
//if it is @ (randomly chosen by me symbol)
//zero the variable val
val = "";
//indicate that now we consider the speed
readSpeed = true;
} else if (readSpeed) {
//if it's time to read the speed and the byte is not equal to the lattice
//add a byte to val
if (a == '#') {
//if the byte is equal to the grid, the speed data is over
//output to port monitor for debugging
Serial.println (val);
//indicate that the speed is no longer read
readSpeed = false;
//transfer the received speed to the ride function
go (val.toInt ());
//zero the val
val = "";
//exit the loop to read the next byte
return;
}
val + = a;
} else if (a == '*') {
//begin to read the angle of rotation
readAngle = true;
} else if (readAngle) {
//if the lattice, then finish reading the angle
//not yet a lattice, add the value to val
if (a == '#') {
Serial.println (val);
Serial.println ("-----");
readAngle = false;
//pass the value to the rotation function
turn (val.toInt ());
val = "";
return;
}
val + = a;
}
//get the time of the last data reception
lastTakeInformation = millis ();
} else {
//if there are no unread bytes, and there were not more than 150 milliseconds
//muffle the engines
if (millis () - lastTakeInformation> 150) {
lastTakeInformation = 0;
analogWrite (angleSpeed, 0);
analogWrite (speedRight, 0);
analogWrite (speedLeft, 0);
}
}
}
We get the result: from the phone we send bytes in the style of "@ speed # angle #" (for example, the typical command "@ 200 # 60 #" .This cycle is repeated every 100 milliseconds, since on the android we set this interval of sending commands. there is no sense, since they will start to become in the queue,and if done longer, the wheels will start to move in spurts.
All the delays through the delay () command, which you'll see later, are not matched by physical and mathematical calculations, but by empirical means. Thanks to all the exposed ditches, the car travels smoothly, and all teams have time to work (currents have time to run).
In the cycle, two side functions are used, they accept the received data and force the machine to go and spin.
void go (int mySpeed) {
//if the speed is greater than 0
if (mySpeed> 0) {
//go ahead
digitalWrite (dirRight, HIGH);
analogWrite (speedRight, mySpeed);
digitalWrite (dirLeft, HIGH);
analogWrite (speedLeft, mySpeed);
} else {
//and if less than ? then backwards
digitalWrite (dirRight, LOW);
analogWrite (speedRight, abs (mySpeed) + 30);
digitalWrite (dirLeft, LOW);
analogWrite (speedLeft, abs (mySpeed) + 30);
}
delay (10);
}
void turn (int angle) {
//supply current to plus of the angle determinant
digitalWrite (pinAngleStop, HIGH);
//give a delay so that the current can be established
delay (5);
//if the angle is 150 or more, turn to the right
//if 30 and less, then left
//the interval from 31 to 149 is left for the movement straight
if (angle> 149) {
//if white is closed, but black and red are open
//means the extreme position is reached, it is impossible to turn on further
//exit the function via return
if (digitalRead (pinWhite) == HIGH && digitalRead (pinBlack) == LOW && digitalRead (pinRed) == LOW) {
return;
}
//if the check for the maximum angle is passed
//spin the wheels
digitalWrite (angleDirection, HIGH);
analogWrite (angleSpeed, speedTurn);
} else if (angle ? if (digitalRead (pinRed) == HIGH && digitalRead (pinBlack) == HIGH && digitalRead (pinWhite) == HIGH) {
return;
}
digitalWrite (angleDirection, LOW);
AnalogWrite (angleSpeed, speedTurn);
}
//remove power
DigitalWrite (pinAngleStop, LOW);
delay (5);
}
.
.
Turn, when the android sends data that the user clamped the angle of 6? 9? 12? it is not necessary, otherwise you can not go straight. Yes, it might not be worthwhile to send a command from the android to a turn if the angle is too small, but it's somehow clumsy in my opinion.
The results of sketch
The sketch has only three important steps: reading the command, processing the rotation restrictions and applying current to the motors. Everything, sounds simple, and in performance is easier than easy, although it was created long and with blunts. The full version of the sketch is github.com/IDolgopolov/AgroArduinoF .
Eventually
A full inventory of several months of work is over. The physical part is dismantled, the software part is even more so. The principle remains the same - turn to incomprehensible phenomena, we will understand together.
And the comments under the first part are interesting, advised a mountain of useful advice, thanks to each.
Video of the result
It may be interesting
This publication has no comments.
weber
Author29-09-2018, 15:22
Publication DateDevelopment / Development of robotics
Category- Comments: 0
- Views: 318
Comments
I’m going to read this. I’ll be sure to come back. thanks for sharing. and also This article gives the light in which we can observe the reality. this is very nice one and gives indepth information. thanks for this nice article...Adsense Safe Traffic
Very useful post. This is my first time i visit here. I found so many interesting stuff in your blog especially its discussion. Really its great article. Keep it up. 123 movies
Cảm ơn vì đã chia sẻ bài viết này. Tôi rất vui khi thấy bài viết tuyệt vời này.https://skribblio.co/
Can I find someone to write my paper for me free? At our cheap for-pay academic help service with writers across all subjects. Discover more about us here. write a paper online free
Pretty nice post. I just stumbled upon your weblog and wanted to say that I have really enjoyed browsing your blog posts. After all I’ll be subscribing to your feed and I hope you write again soon! soaptoday