Sunday, June 26, 2016

The Rock, the Paper, and the Scissors

Four months and seven(ish) days ago, I started working on my first Android app. It was to be a rock paper scissors game, including a Lizard-Spock bonus feature (In a separate $0.99 version), and bluetooth phone-to-phone functionality. On June 19th, I uploaded it to the Google Play store.

It had humble beginnings: Solid color background, placeholder textures, hard-coded strings... Kind of like this:
Before

The animations were made slowly: Trying out a series of commands based on the current tick, and then compiling it to see what happens. If I didn't like it, I would have to make changes and re-compile.

I found free images from the internet (Who can copyright a digital piece of lined paper?), and modified them in GIMP to look right. Some modifications were simply removal of color, and others were all-out remakes.

Finding sound clips was one of the harder parts. A good 90% of the sound effects that I found weren't what I was looking for, and usually I would have to edit the pitch, speed, and volume of the clips in Audacity.

No points for guessing where the sound for the baby scissors army came from.

The Bluetooth feature still isn't ready. It's a very complex system and I've been doing research, learning, and planning on how to implement it. It will be on both the free and paid versions of the app, and they will even (hopefully) be compatible.

Coding the physics simulations weren't the easiest things either. I had to invent gravity, make the confetti descent look realistic, build a function for detecting collisions... One of the more interesting aspects of this was developing a mathematical formula to get the upwards velocity x needed to get up to y height with a gravity of 2 pixels/second/second. I'll share this particular one with you:
x = -2√y + 1
Or as it appears in my code:
plyr2XSpeed = (byte) (2*Math.sqrt((player2.getY() + player2.getHeight())) + 1);
The +1 is margin for inaccuracy.

Stay in school, because the engineers (Specifically those who work hard in math) are going to rule the world! (Bwa ha ha-COUGH!  COUgh! Cough! ha ha...)

After

If you have an Android device that is part of the 89.2% of Androids that *should* (Please tell me about bugs) be compatible with my app, you can download and start playing the free version TODAY! Just go to this link and click on the green install button (Google Play is probably the only place where this is safe to do... Everywhere else, the green buttons are probably deceptive ads.):
https://play.google.com/store/apps/details?id=g3ckobot.therockthepaperandthescissorsfree

Again, the bluetooth mode is not ready yet. It will be made available for both the free and full versions of the game.

Downloads and positive reviews on the store are really helpful to apps. They are the primary thing to boost their position in searches, and give a readily available average to users contemplating the use of their 19 MB.

Thank you for taking the time to read this. I hope you find this post and my app entertaining.

Wednesday, June 1, 2016

Arduino Rover Part 4: Bluetooth

A few weeks ago, I picked up the Arduino Uno that I've had for a while, and started thinking about what I should build with it. I decided on modifying an old toy rover that I had, and making it wirelessly drivable... but with 2 way audio and a camera feed.

This is the fourth part of several posts. I will be explaining about how I connected my phone to Arduino via Bluetooth.

Links:
Part 1: Initial Plan and Reverse-Engineering
Part 2: Preparing the Rover Chassis
Part 3: Motor Control
Part 4: Bluetooth!
Part 5: Mounting
Part 6: Batteries
Part 7: The Test


The rover was coming along nicely, however I still couldn't control it remotely. Options for communications were:

A. WiFi: Hosting a webpage on the rover, and inputting commands from a computer or phone. Advantages: Higher bandwidth, range while in free WiFi. Disadvantages: would only work in free WiFi, and would require a special "shield" that goes on top of Arduino.

B. Bluetooth: Connecting via phone and driving with an interface. Advantages: Works without WiFi, doesn't require special shield, uses Arduino's built in serial capabilities for bit-by-bit communication. Disadvantages: Lower bandwidth than WiFi, lower range while in free WiFi zone.

Let's compare:

WiFi
Bluetooth

Bandwith
Flexibility
Control quality
Weight and size
Cost
Cool factor (0.25 points)

Sorry, WiFi, but I think Bluetooth wins 4.25 to 1.

This means that a continuous video feed is not really an option. The highest serial 'baud' that Arduino can muster is 115200. If I was to try to send a 640 by 480 pixel video feed of 10 frames per second (using grayscale instead of color), that would be 640 * 480 * 10 = 3,072,000 necessary bytes per second. That's about 3 megabytes per second, 26.66 times what is available. Kind of disappointing. On the bright side, I'll still be able to press a button on my phone and receive a single picture... over the course of 2.66 seconds.

I looked online to see how people usually do Bluetooth connections with their Arduino and discovered that the most popular Bluetooth module was the HC-06. After some research, I discovered that it was capable of using a higher baud rate than Arduino could handle, but had a lower default. It also ran on the standard 5V that Arduino outputs... Not bad. There were also guides everywhere, so the HC-06 it was.


It's $9.49 on Amazon with free 2 day shipping (Amazon Prime only). When I got mine, I think there was a 50% sale.

While waiting for it to come in the mail, I had to slap together some Android code. I downloaded this guy's example and set about reverse-engineering it. There was a disturbing number of references to Chihuahuas. I've been working on my own app for a while, so all I had to focus on was the Bluetooth code. Please note that the project folder is formatted for Eclipse. I had to manually copy the source code into Android Studio (I only took part of it).

Modifying the code was a big task (Android code terminology ahead!). The first thing I did was delete the Chihuahuas. Then I ditched the layout and created my own (one that will be easier if I'm looking at the rover rather than my phone). I changed the search term for the paired devices from "HC-05" to "HC-06". Then I set up the controls to broadcast a single-byte command every quarter of a second depending on which buttons were pressed, rather than as the buttons were pressed. I added another activity for pre-driving setup (custom search term, more stuff to be added later). I also worked on cleaning up and custom-formatting the code.



Then there was the Arduino code to design. A quick Google search revealed that it's as simple as making a SoftwareSerial on the proper pins. I didn't bother downloading anything for it. The complete code sends a message through the HardwareSerial to my computer every time it receives a command.

On the day of projected arrival, I discovered that the relays had come early, and didn't notice the big yellow bubble-protected envelope containing the HC-06 (facepalm). I realized my mistake in the late afternoon...

On the hardware side of things, it's pretty simple to set up.



Here's where the pins go:
GND --> Arduino's GND
VCC --> Arduino's +5V
RXD --> Voltage Divider to TX on Arduino (More on this in next paragraph)
TXD --> RX on Arduino (Don't send these to the 0 or 1 pins. These are used to communicate with the computer)

After doing my research, I noticed that some people skipped the voltage divider, and some people regard it as necessary. Both groups say that it works for them, but the RXD pin is technically not supposed to be exposed to the full 5V of Arduino's pins. Here's a link to the page I learned most about how to do it.

Looking into my inventory, I don't have any 20KΩ resistors. That's ok. I'll just use two 10KΩ resistors in series.

Plugging everything in and verifying hardware connections... No bluetooth connection. More troubleshooting:

Switching RX and TX pins: Nope.
Staring at it to use the Force: Why??
Checking on Bluetooth menu in phone settings: Getting somewhere. It sees the HC-06.
Tapping on it and inputting the default password (1234): Bingo! Now the phone and Arduino are "paired" together!
Trying again: It works! Every quarter of a second while pressing a button on my phone, Arduino sent a message of "Received right command" or "Received forward command"!

I wonder why the Chihuahua code didn't automatically pair with the device... Probably doesn't want to accidentally pair with random devices (smart).

I think I'll wait to post downloads of my own code until the post where the rover is driving successfully (Sure, I'll do it sooner if the comments ask me to). I encourage anyone wanting to, to try and make their own code. I only downloaded the Chihuahua code to figure out how to create and maintain a bluetooth connection (I'd been struggling). Anyone with the same problem can go ahead and get the Chihuahua code. The Arduino code is so simple it shouldn't be necessary to download.

Part 5 will be about my search for ways to mount my Arduino to the rover.

Tips jar is for you, not me!:

1. Be conservative. It wouldn't have been very fun to kill the bluetooth module via overvoltage.

2. Do it yourself (if you can). It's very important to learn what's going on under the hood, and the best way to do that is to make it yourself. If you're stuck, you can get some help, but still do your best to understand.

3. Don't give up troubleshooting!

Edit: 7/8/16: Updated list of links
Edit: 8/25/16: Updated list of links
Edit: 7/6/17: Updated list of links