Controlling LEDs using an Android device
I have wanted to learn Arduino for a while now. However, I also wanted to incorporate Android in there somewhere. This seemed to be a pretty simple start-up project.Android App
Connecting to Arduino Bluetooth shield
On clicking "Connect to Arduino BT", the Android device connects with Arduino. For my purposes, I hard-coded the MAC address of my Arduino Bluetooth in the Android program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | bluetoothConnect.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { try { String macAddress = "20:13:06:26:10:39"; if (connect(macAddress)) { greenLED.setVisibility(View.VISIBLE); blueLED.setVisibility(View.VISIBLE); redLED.setVisibility(View.VISIBLE); goRound.setVisibility(View.VISIBLE); showToast("Connected"); } } catch (final Exception e) { showToast(e.getMessage()); } } }); |
Sending Characters that mean something to the Arduino
1 2 3 4 5 6 7 8 9 | private void send(final String message) throws IOException { if (mConnected) { if (mBluetoothSocket != null) { mOutputStream.write(message.getBytes()); } } } |
For green LED, if check-box is checked, 1 is sent. If it is unchecked, 0 is sent. Red and green LED follows the same process (with different Char values of course). Finally for the RGB Go Round button, 6 is sent. The code for green LED and RGB Go Round button is below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | greenLED.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { try { if (isChecked) { send("1"); } else { send("0"); } } catch (IOException e) { showToast(e.getMessage()); } } }); goRound.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { redLED.setChecked(false); greenLED.setChecked(false); blueLED.setChecked(false); try { send("6"); } catch (IOException e) { e.printStackTrace(); } } }); |
Arduino Side
//Project: Bluetooth LED #include <SoftwareSerial.h>// import the serial library SoftwareSerial newPorts(10, 11); // RX, TX const byte LED_PINgreen = 13; const byte LED_PINblue = 9; const byte LED_PINred=8; void setup() { newPorts.begin(9600); pinMode(LED_PINgreen, OUTPUT); pinMode(LED_PINblue, OUTPUT); pinMode(LED_PINred, OUTPUT); digitalWrite(LED_PINgreen, LOW); digitalWrite(LED_PINblue, LOW); digitalWrite(LED_PINred, LOW); } void loop() { while (newPorts.available() > 0) { char ch = newPorts.read(); executeReceivedCommand(ch); } } void executeReceivedCommand(char command) { switch (command) { case '0': digitalWrite(LED_PINgreen, LOW); break; case '1': digitalWrite(LED_PINgreen, HIGH); break; case '2': digitalWrite(LED_PINblue, LOW); break; case '3': digitalWrite(LED_PINblue, HIGH); break; case '4': digitalWrite(LED_PINred, LOW); break; case '5': digitalWrite(LED_PINred, HIGH); break; case '6': digitalWrite(LED_PINgreen, LOW); digitalWrite(LED_PINblue, LOW); digitalWrite(LED_PINred, LOW); while (newPorts.available() <= 0){ digitalWrite(LED_PINgreen, HIGH); delay(100); digitalWrite(LED_PINgreen, LOW); delay(50); digitalWrite(LED_PINred, HIGH); delay(100); digitalWrite(LED_PINred, LOW); delay(50); digitalWrite(LED_PINblue, HIGH); delay(100); digitalWrite(LED_PINblue, LOW); delay(50); } break; } }
Demo
Use:
Currently, I have put this assembly behind a lotus vase in my room. Whenever I cannot sleep at night, this will give me some company. In future, I would like to transform it into some sort of "Northern Light" simulator. At this point, I do not have enough experience to know if that is even possible.