A (very) Moody Lamp.

Standard

So I was like very much in love with the weather today. I really like winters, especially winter nights. I have one too many memories to associate winter nights with. What I really like about winters is the change in mood. I become mellow like low hanging fog. I really like the silence of winter nights, I just sit there in my veranda taking small puffs of my poison alone with my own thoughts *Bandeh*. The moodiness of it all is very awesome. So I thought I should compliment this very lonely and moody night with something.

Well technically, it is a very easy, 1 hour coding session. A bit of PWM magic and some basic Android. The heart of it is the awesome Arduino Uno rev3 and RedBear Labs’ Bluetooth Low Energy module. The concept is a home automation device. You put on bluetooth on your phone and enter your room, the mood lamp welcomes you with a personal message and then you can set red, blue, green colors and make your own colors according to your mood… all through bluetooth. The device also acts as a “beacon”… umm I’ll explain this later.

Right now, the weather is very awesome here. There is slight wind blowing and the Psithurism (I learned this word today, it means rustling of wind through trees) is very relaxing. A slight chill on the spine and mellow thoughts oscillating (this is awesome) with every beat. After having a long day, such a relaxation is very welcoming. I just wish if I could have a welcoming partner who could melt in with me and be my mood. What else could be better than colors in such a case. The eternal light of infinite colors that burns inside every memory you have.

Memories are colorful but the colors are mellow, you miss them. “That was some shit I did… *slight sad smile*”. You look at them through different shades. Some memories are blue and sad or you are green with jealousy or maybe yellow with some confused times. It is life. You had someone in your past who understood your every emotion. Who was there when you needed someone the most but now they are just colorful memories. The time gone by… the time you think about on such silent nights. The time when lights were brighter. The “grass”… was greener.

So basically, the design of the mood lamp is very simple.

That blue one is my Arduino Uno. The red one is the BLE Mini from the Labs. The white one is the bread board and that small black one is my Philips Go Gear Raaga (cool shit that is… the headphones that came with it are very awesome, brilliant bass *this sounds very cool*). I studied the implementation of the Android app that you have to download from RedBear Labs’ site (linked above). It is very simple. They have made a service and your app uses that service to communicate with the BLE Mini chip. I tweaked the app little bit and added two seek bars. I’ll show the code:

20131220_003446

PWMSeekBar = (SeekBar) findViewById(R.id.PWMSeekBar);
        PWMSeekBar.setEnabled(false);
        PWMSeekBar.setMax(255);
        PWMSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
                byte[] buf = new byte[] { (byte) 0x02, (byte) 0x00, (byte) 0x00 };

                buf[1] = (byte) PWMSeekBar.getProgress();

                characteristicTx.setValue(buf);
                mBluetoothLeService.writeCharacteristic(characteristicTx);
            }
        });

The app (the concept is so cool that I guess I’ll generalize for all my future apps), uses unique commands to control different aspects of the Arduino via bluetooth. The above piece of code defines a byte array called buf. I’ll explain that with the Arduino code later. After setting the byte array, it simply transmits the command. characteristicTx is initialized as one of the services provided by the BLE Mini firmware (I’ll touch on that towards the end).

The Arduino code is very clean too.

#include "Boards.h"
#include <ble_mini.h>

const int pin = 4; // lets stay with pin 4.
const int pinMissed = 8;
byte buf[] = {'N','I','N','J','A'};
int incoming;
const int R = 9;
const int G = 10;
const int B = 11;

void setup() {
  BLEMini_begin(57600); // start the BLE device
  // Serial.begin(57600);
  pinMode(pin, OUTPUT);
  pinMode(pinMissed, OUTPUT);
}

void loop() {
  while(BLEMini_available()) {
    byte cmd = BLEMini_read(); // read command.
    byte toggleState = BLEMini_read();// read 0 or 1.
    byte extraData = BLEMini_read();
    switch(cmd) {
      case 0x01:
        switch(toggleState) {
          case 0x00:
            digitalWrite(pin, LOW);
            break;
          case 0x01:
            digitalWrite(pin, HIGH);
            BLEMini_write_bytes(buf, 5);
            break;
        }
        break;
      case 0xA2:
        switch(toggleState) {
          case 0xA1:
            digitalWrite(pinMissed, HIGH);
            break;
          case 0xA0:
            digitalWrite(pinMissed, LOW);
            break;
        }
        break;
      case 0x02:
        switch(extraData) {
          case 0x00:
            analogWrite(R, toggleState);
            break;
          case 0x01:
            analogWrite(G, toggleState);
            break;
          case 0x02:
            analogWrite(B, toggleState);
            break;
        }
       break;
    }
  }
}

Here we see the concept of unique commands. The commands in the app (after I hacked it) follow a pattern. The first byte is the access byte. It signifies which feature of our mood lamp we need. I have made 3 features. A beacon, which can detect your presence and start communicating with your phone. A incoming/missed call alerter. And a bluetooth controlled mood lamp.

The second byte is the state we want to put that feature in at a given moment. The moment the app connects to the BLE Mini, it sends its presence in the form of:

buf [] = {0x01, 0x01, 0x01};

What this means, according the Arduino above, I should put on the LED on pin 4. It then sends a custom message to the phone. The app shows it as a notification in the status bar. But we cannot send a message every time we light up pin 4. The third byte in the command is meant to control that. Only when it connects, it sets the third byte as 0x01. The “extra data” is meant to control these tiny features of the Arduino.

What I really like about a winter night is the time I get alone with myself. Once in a while, I really like to revisit those long forgotten and deeply buried memories. The slow rumble of the trucks passing by on the highway bring back those cold and harsh nights that I spent in utmost misery *I’m the highway*. And slowly grooving with my poison and the music it all seems a colossal kaleidoscope of colors. Some so harsh and striking that they blind me, some so mellow and sorrowful that I’m filled with emotions. Some so crazy that make me think of all the stupid stuff I did. Some are black, some are silent… and most of them are like the winter night. Deep, dark and mysterious. Those are mine. The moments I had. The colors I have seen. Those are mine.

I remember that girl who was kind enough to hold my hand and ask me who I was. I remember those mellow colors of the evening that her smile would brighten. I remember that red color of her irritation, the cool teal color of her touch. The maroon of her unsaid words. The bright yellow of the sun on her face. The gray of her sad smile. *no… only she and nothing else matters*.

I feel the blue of her absence. I feel the green of my envy if she ignores me. I feel the playful orange of her laughter when I hear voice on my phone. I feel the purple of my longing, craving for being with her. I feel the violet of my helplessness of loving her.

I’ll put up my rendition of the Labs’ Android app on Git soon. As another example, I’ll explain how I managed to add the missed call/incoming call alert feature.

telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);     
        telephonyManager.listen(new PhoneStateListener() {

            @Override
            public void onCallStateChanged(int state, String incomingNumber)
             {
                     //  React to incoming call.
                     // number=incomingNumber;
                      // If phone ringing
                     if(state == TelephonyManager.CALL_STATE_RINGING)
                     {
                         byte buf[] = new byte[] { (byte) 0xA2, (byte) 0xA1, (byte) 0x00 };
                         characteristicTx.setValue(buf);
                          mBluetoothLeService.writeCharacteristic(characteristicTx);
                     }
                       // If incoming call received
                     if(state == TelephonyManager.CALL_STATE_OFFHOOK)
                     {
                         byte buf[] = new byte[] { (byte) 0xA2, (byte) 0xA0, (byte) 0x00 };
                         characteristicTx.setValue(buf);
                          mBluetoothLeService.writeCharacteristic(characteristicTx);
                     }

                     if(state == TelephonyManager.CALL_STATE_IDLE)
                     {
                         // do something here
                     }
              } 
        }, PhoneStateListener.LISTEN_CALL_STATE);

I used the Telephony Manager class. I listen to the current call state and set the appropriate commands. If there is an incoming call, the command byte is set to 0xA2 which maps to controlling of pin 8 on the Arduino. If the user answers the call, the light is unset notifying that the user has attended the call. Else, if the user did not attend the call or dismissed the call, it is a missed call and the LED will keep on glowing.

The slight chill of the winter night makes you think about yourself. You are one with yourself *Journeyman*. You think about the sea green memories of your mistake. You think about the bright brownish streaks of the things to come. You feel the pink of the love your soul-mate.

The cold low lying fog cries light gray tears of that faded love for someone once very close to your heart. The black sky makes you think of all those times when you were alone and desperate. And the white spiral of the smoke from my burning poison reminds me of that one forgotten day where, once, I had nothing and no one. Each memory I have has a different color with it. Some good and worth seeing once again and some are so grotesque that I’d never bother to see them again.

Those are my experiences the different shades of life that I have seen and what is better than a moodily lit room where I lie alone with my thoughts on a cold winter night.

The following are the pics of my functioning mood lamp. I’ll put up a video too tomorrow along with the Android app.

20131220_021624 20131220_021604 20131220_021530 20131220_021332 20131220_021156 20131220_021104

Well that was quite cool and well executed. It was a fun project. Something I could play with using my phone. Next step would be to implement something better. I’ll work on interfacing an OLED display panel that will display a welcome message and all your notifications and let you play with LEDs to set your mood and many other good stuff. A whole room controlled by bluetooth in your phone.

May the force be with thee and all the memories you have.

D.

UPDATE: you can find the app here. Make sure you have Android 4.3 or higher as Bluetooth Low Energy is only supported by Android 4.3 or higher.

Advertisements

2 thoughts on “A (very) Moody Lamp.

  1. Martina

    Hello! Great project!
    I am working on something not exactly the same, but there is a lamp(s) too and they have to be controlled by the presence or the absence of people in the room. I am not sure that the RedBear BLE Shield would work for me, so I would like to ask you based on your experience.
    The main thing is that the person should not do anything. Is it possible the BLE Shield to detect the presence of the phone and send signal to the Arduino board to turn on the light without any interaction required from the user?
    Thank you in advance!

    • Hello Martina! I’m so sorry to reply to you after such a long time. I have been busy with my new job and all.
      Anyway, Yeah, you can turn on/off the light without any user interaction. Send a command string the moment your app connects to the device. And if you’re willing for an adventure, you must try BLE Nano from RBL.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s