Skip to main content

ArduinoProjectUno

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.






Popular posts from this blog

SalsaNight

Zorgania

The Zorganian Republic has some very strange customs. Couples only wish to have female children as only females can inherit the family's wealth, so if they have a male child they keep having more children until they have a girl. If they have a girl, they stop having children. What is the ratio of girls to boys in Zorgania?
The ratio of girls to boys in Zorgania is 1:1. This might be a little counter-intuitive at first. Here are some ways of tackling this problem. 1. Monte Carlo Simulation: Although, Monte Carlo simulation does not necessarily show why the result is 1:1, it is appropriate because of the very counter-intuitive nature of the problem. At the very least, it helps us see that the result is indeed 1:1. Therefore, this is a good start.
The following R code estimates the probability of a child being a boy in Zorgania. 
couples <-100000 boycount <-0for (i in1:couples){ # 0: boywhile (sample(c(0,1),1) ==0) { boycount=boycount+1 } } probability <- boycount/(co…

Simple Launcher

A simple minimal launcher application for Android devices that shows battery percentage using lzyzsd's CirclProgress library (ArchProgress used in this case) and BroadcastReciever for battery state, Android's clock widgets, a built-in flash light switch and an app list view that can be toggled. Currently, the toggle simply filters all the app that I am working on at present. Future implementation can allow users to select their favorite apps or populate second toggle based on the most used applications.