So I've made a little not-so-challenging game that uses a tricolor LED, mini buzzer, arduino and of course our Android device. The mechanics of the game is that you need to choose a color, red, green or blue from the Android app and then the accessory (arduino) will randomly pick a color after pressing the switch. If the color you've chosen matches the color that the accessory generated then you win the game.

### Circuit

The circuit consists of a push button switch, a tricolor LED, mini buzzer and a couple of resistors. Here are the parts list:

• 1 Tricolor LED (common anode)
• 1 Push Button Switch
• 1 Mini Buzzer
• 5 330 ohms resistor

In this example, we are using pins 9, 10 and 11 to control the tricolor LED. Since our LED is common anode which means that to show a specific color with full brightness, we will send 0 to the corresponding pin and 255 to remove the color. For example, if we want to display red, then we need to set pin 9 to 0 and set 255 to pins 10 and 11. To display green, we need to set pins 9 and 11 to 255 and pin 10 to 0.

Pin 12 will be used to send tone to our piezoelectric speaker or buzzer while pin 2 will be used to detect if our mini switch has been pressed or not. So in summary:


Input pin: Pin 2
Output pins: Pin 9, 10, 11, 12


The 3 resistors in series with the 3 legs of our tricolor LED is needed to limit the current that flows thus preventing the LED from being burned.

The resistor connected to the one pin of the push button switch serves as a pull down resistor. This is needed so that at normal position, pin 2 will read a zero input because it is directly grounded. When you push the switch, pin 2 will be logic 1 because the voltage from Vcc of Arduino in series with pin 2.

### Sketch

In our setup function:


void setup() {
pinMode(2,INPUT);
Serial.begin(9600);
acc.powerOn();
}


We set pin 2 as input then we initialize our serial communication and send a broadcast to our android device. All PWM pins are by default output pins so we don’t need to initialize them here.

Here’s our loop function:


void loop() {

if(acc.isConnected()){
if(buttonState == HIGH){
int inc = random(10, 20);
int top = random(500, 700);
for(int i= 1; i < top; i += inc){
if(pin == 9){
displayRed();
}
else if(pin == 10){
displayGreen();
}
else if(pin == 11){
displayBlue();
}

msg[0] = pin;
acc.write(msg, 1);

pin++;

if( pin > 11){
pin = 9;
}

tone(12, 900, 1000/8);
delay(i);
}

msg[0] = 12;
acc.write(msg, 1);
}
}
}


Here’s we are randomly generating the value for the end of our loop as well as the increment to mimic the random delays caused by friction in a real roulette game. Then for each iteration, we loop through the 3 colors and send them to the android device for display. The tone() produces the sound for our buzzer. It sends an 8th note 900hz on pin 12 to produce our clicking sound. Once the for loop exits, we need to inform our device that the random selection is done and needs to check whether the user wins or not.

And here are the functions referenced from the loop function:


void displayRed(){
analogWrite(9, 0);
analogWrite(10, 255);
analogWrite(11, 255);
}

void displayGreen(){
analogWrite(9, 255);
analogWrite(10, 0);
analogWrite(11, 255);
}

void displayBlue(){
analogWrite(9, 255);
analogWrite(10, 255);
analogWrite(11, 0);
}
{% endcodeblock %}

### APK
Again, the project is almost identical with all the other projects in our previous posts. The most significant part only is the run() method in our thread that reads the incoming data from the accessory.

{% codeblock Run lang:java %}
public void run() {
byte[] buffer = new byte[16384];
int i;
int retvalue = 0;

while (retvalue >= 0) {
try {
} catch (IOException e) {
break;
}

i = 0;
while (i < retvalue) {
Message m = Message.obtain(handler, 0);
m.arg1 = buffer[0];
handler.sendMessage(m);
i++;
}
}
}


And in our handler, we set the corresponding color to our view’s background based  on the value we receive from the accessory and once we received the signal that the random selection has ended, we then check if the user’s selected color matches the last color that has been sent to the device.


public void handleMessage(Message msg) {
if (msg.what == 0) {
int val = msg.arg1;
if (val == 9) {
result.setBackgroundColor(Color.RED);
} else if (val == 10) {
result.setBackgroundColor(Color.GREEN);
} else if (val == 11) {
result.setBackgroundColor(Color.BLUE);
}

if (val == 12) {
ColorDrawable valueColor = (ColorDrawable) value.getBackground();
ColorDrawable resultColor = (ColorDrawable) result.getBackground();

if (valueColor.getColor() == resultColor.getColor()) {
Toast.makeText(Main.this, "You win!", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(Main.this, "You lose!", Toast.LENGTH_LONG).show();
}
}
}
}