Bluetooth Light

From Microduino Wiki
Revision as of 06:55, 30 September 2016 by Fengfeng (talk)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Language: English  • 中文

Overview

Here we can control the color and brightness of a light by App Android. When the phone is not connected to the Bluetooth, the light can automatically change color. While after the phone is connected, you can change the light to any color through the phone.

Bluetooth-light.jpg

Principle

The system can tell whether the mobile phone Bluetooth and mCookie-BT are connected or not. When connected, the system receives the data from the App to the mobile phone mCookie-BT, and analysis it out so as to control the color of the lamp. When the Bluetooth is disconnected, the system enters the pre-set color changing mode. Unless there is a Bluetooth connection, otherwise it'll enter the cycle mode.


Bluetooth-light-sch.jpg

Equipment

Module Number Function
Microduino-CoreUSB 1 Core board
Microduino-Sensorhub 1 Sensor pinboard
Microduino-BT 1 Bluetooth module
Microduino-Color LED 1 colored light
Microduino-BM 1 Battery management
Ble lamp.jpg

Preparation

  • Setup 1: Use a USB cable to connect the CoreUSB and the PC/Mac, the open Arduino IDE.
CoreUSB Ble pc.jpg
  • Setup 2:

Click Files > Examples > mCookie > _103_BLE_Light, and load the program. Or download:

103 BLE Light.jpg
  • Setup 3: Select the right board and COM port for program download.

Program Description

  • Here the Bluetooth adopts serial communication. For mCookie-CoreUSB, it uses Serial1. So we define serial port service.
#define my_Serial Serial1
  • At the same time, the baud rate of the Bluetooth communication is 9,600. You need to define serial communication rate in setup().
my_Serial.begin(9600);
  • Colored LED Control Description
#define PIXEL_PIN    A0    // Digital IO pin connected to the NeoPixels.
#define PIXEL_COUNT  6

Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);
    • PIXEL_PIN: The colored LED's control pin. Users can customize the pin according to personal needs but need to change hardware connection at the same time.
    • PIXEL_COUNT: The number of the LED light. One Bluetooth can only control six lights due to the APP Bluetooth transmission data write six at most.
  • “#define xxx yy”function: Give the value of " yy " to " xxx ". You can change the function above to:
Adafruit_NeoPixel strip = Adafruit_NeoPixel(6, A0, NEO_GRB + NEO_KHZ800);
  • "ble()" function is used to receive and analyze data sent from the phone:
sscanf((char *)strstr((char *)buffer, "C:"), "C:%d,%d,%d,%d", &sta[0], &sta[1], &sta[2], &sta[3]);

Transmission protocol analyzed from the code: " C:%d,%d,%d,%d " and assign value of the four analyzed data "%d " to sta[0], sta[1], sta[2] and sta[3], three of which are the color values of the R, G and B. The last value is the control mode selection.

  • Use the analyzed data to control light color change.
    if (-1 == sta[3]) {
      colorSet(strip.Color(sta[0], sta[1], sta[2]));
    }
    else if ((0 <= sta[3]) && (sta[3] < PIXEL_COUNT)) {
      colorSet(strip.Color(sta[0], sta[1], sta[2]), sta[3]);
    }

If the value of sta[3] is -1, then all lights connected display the same color. That color is combined by " sta[0], sta[1], sta[2]",namely, the color selected on the phone. Otherwise, these six lights can also be controlled separately.

  • When there is Bluetooth connection, it will keep sending "\n " data to BT. As long as there is receiving, we can consider the Bluetooth is in connection. In this case, the value of " color_en " is true.
  if (c == '\n')
    {
      color_en = true;
      safe_ms = millis();
    }
  • When the Bluetooth is disconnected, the system will make the value of " color_en " false in three seconds, starting to perform custom color change. "!" means "not".
  if (millis() - safe_ms > 3000)//Users can change the time. The unit of "3,000" is "ms". 
  {
    safe_ms = millis();
    color_en = false;
  }
  • "rainbowCycle(w, R, G, B, x);" Function Using Description: Users can change the color as you like.
    • w: Delay time of the color gradual change.
    • R, G, B : Three primary colors.
    • x: "0" indicates the brightness changes from weak to strong; "1" indicates the brightness changes from strong to weak.

Hardware Buildup

  • Setup 1: Since the program code control adopts A0, you can connect the LED light to the Hub's A0 interface. Users can also customize the interface here.
CoreUSB Ble steup1.jpg

The connection method of the LED light can refer to the picture below. Please be noted of the connection order, which connect from LED's IN interface to OUT.

CoreUSB Ble steup11.jpg
  • Setup 2: Connect the activated battery box and the BM module.
CoreUSB Ble steup2.jpg
  • Setup 3: Then, stack all modules together without considering order. Congratulations! You've finished the circuit buildup.
CoreUSB Ble steup3.jpg

APP Debugging

  • Setup 1: Scan the two-dimensional code on the left and download Bluetooth Light App.
App Ble steup1.jpg

ios Source download link:LightBlue App

  • Setup 2: Open the App after the download and installation. If the Bluetooth is disconnected, the system will pop up a notice.
App Ble steup2.jpg
  • Setup 3: Click Scan and the phone will start searching Bluetooth devices and display them below; Select the Microduino device.
App Ble steup3.jpg
  • Setup 4: After the connection, you can use your phone to control LED lights.
App Ble steup4.jpg

Video