Now that the hardware was functional, I had to make it respond to input from the user. This had two parts: the Arduino software and the controller app on the phone.
It took a bit of experimentation but I was finally able to run the motors at the correct speed. My aim was to create methods that moved the car front, back, left and right. And, of course, the all important stop command. To test it, I wrote a small program that invoked these methods and made the car go in all directions.
I then modified the loop to accept commands from the Bluetooth controller. Since all I read was bytes, I used the ASCII characters (‘f’, ‘b’, ‘l’, ‘r’, ‘s’) to denote the various commands that the car understood.
For the app, I decided to use Swift. This was my first time writing an app and I ended up using the Internet as my guide! I learnt that iOS apps use the CoreBluetooth library. I ended up creating a BluetoothManager object that was responsible for detecting when peripherals were in range as well as connecting to them. Since I wanted the application to be seamless, I was going to have the app automatically detect when the car was in range and connect to it. The user could then send commands via the interface. I chose to use a Single View App as my base project. The app looked as follows:
Once the car was in range, the app would connect to it. The user could then use the buttons to send the car forward, back, left, right and stop the car if necessary. If for any reason, the connection to the car was lost, the user could force the connection by tapping on the Bluetooth icon. The app would only accept commands if the car was connected. You can see a video of the car working below:
View this post on Instagram
This is part 2 of my bluetooth-controlled car. You can read Part 1 as well.