Simple Bluetooth machine on Arduino

The widespread and low cost of the Arduino platform and various robot platforms allowed fans to create radio-controlled cars for every taste. A widespread use of smartphones has allowed them to be used as controllers of these machines. The main problem for many Arduino lovers is the lack of experience in programming for Android. Today I’ll talk about how to easily solve this problem using the App Inventor 2 Android visual development environment.



The construction of any machine should begin with hardware, so I
’ll briefly describe what I used for my machine: arduino nano
bluetooth module HC-05
Z-Mini Motor Sensor Shield L293D
2WD Motor Chassis
The hardware configuration does not play a big role in this project, therefore the chassis, shield and the arduino itself can be replaced with any analogs.

Now let's move on to creating an Android application. App Inventor - a visual development environment for android applications, works from a browser. We go to the site, allow access to your Google account, click the "create" button and create a new project. In the new project using the “Drag and Drop” method, we create 4 buttons for choosing the direction of movement and one for connecting to our bluetooth module. Like that:

image

Next, click on the “Blocks” button in the upper right corner and using the same drag and drop method we create the logic of our android application like this:

image

Now it remains to compile the application by clicking on the “Build” button.

I don’t think that arduino lovers will write a sketch, I’ll just say that you can choose from ready-made sketches where the machine is controlled from a computer via a sireal port. I used this
sketch
int val;
int IN1 = 4;
int IN2 = 7;
int EN1 = 6;
int EN2 = 5;

void setup ()
{
Serial.begin (9600);
pinMode (IN1, OUTPUT);
pinMode (IN2, OUTPUT);
pinMode (EN1, OUTPUT);
pinMode (EN2, OUTPUT);

}
void loop ()
{
if (Serial.available ())
{
val = Serial.read ();

// Set the forward movement
if (val == 'W') // When the “W” key is pressed
{
// The pins are configured according to the Motor Shield
// Motors spin forward
digitalWrite (EN1, HIGH);
digitalWrite (EN2, HIGH);
digitalWrite (IN1, HIGH);
digitalWrite (IN2, HIGH);
}

// Set backward movement
if (val == 'S')
{
digitalWrite (EN1, HIGH);
digitalWrite (EN2, HIGH);
digitalWrite (IN1, LOW);
digitalWrite (IN2, LOW);
}

// Set the movement to the right
if (val == 'D')
{
digitalWrite (EN1, HIGH);
digitalWrite (EN2, HIGH);
digitalWrite (IN1, HIGH);
digitalWrite (IN2, LOW);
}

// Set the left movement
if (val == 'A')
{
digitalWrite (EN1, HIGH);
digitalWrite (EN2, HIGH);
digitalWrite (IN1, LOW);
digitalWrite (IN2, HIGH);
}

// Stop mode
// When you release the keys in the program, “T” is sent to the port
if (val == 'T') // When you press the "T" key
{
// ENABLE pins are pulled to minus, motors do not work
digitalWrite (EN1, LOW);
digitalWrite (EN2, LOW);
}
}
}

So, this is how I got a machine:



Those who like the design in applications can change a little. I will not describe in detail how to do this, it’s not difficult there to figure it out yourself. I can only say that for this you basically need to use .png files, instead of .jpeg, which do not support transparent background. For example, any unprepared person can make such a design in half an hour or an hour:

image

PS For those who have not encountered application development in App Inventor 2, I made a more detailed guide on developing this application (you need to go to YouTube to view it).



PPS A collection of more than 100 instructional materials on arduino for beginners and pros here .
The online Arduino Gicktime course is here.
UPD 02/02/2017: posted the application on the play market.
Similar projects with other shields here.

Also popular now: