Assignment: Networking and communications

1. Bluetooth HC-05
2. Two 328P Boards communicate with Bluetooth modules
3. Control LED Matrix by mobile

Features:

  • Voltage:3.6-6V
  • Baud rate:1200,2400,4800,9600,19200,38400,57600,115200
  • Working mode:AT command mode , Auto connect mode
  • Connection: Bluetooth use serial port to communicate, the TXD and RXD wire connection flow below:


    Home

    Description

    This project, there are two pieces of 328p board, each board connecting one Bluetooth module, one board is connected with a potentiometer, another piece of board is connected with LED matrix. Rotating potentiometer, the birghtness of the next board changes. This reason is that the data is transfered by the bluetooth module.

    The componets of the project list below:

  • 328p board x 2
  • bluetooth module x 2
  • potentiometer x 1
  • LED Matrix x 1

  • Bluetooth module pairing

    Connecting the Bluetooth module to the computer through the USBTOTTL module, the serial communication software is opened, and the parameters of the Bluetooth module are modified through the AT commands .

  • change the same name: at+name=FablabBeijing
  • change the same password: at+pswd=1234
  • change one bluetooth role to slave: at+role=0
  • change another bluetooth role to master: at+role=1
  • Connection

    Connectint the master bluetooth module and potentiometer to a 328P board.

  • RXD(buletooth) ------ TXD(328p)
  • Middle pin(potentiometer) ------ A0(328p)
  • Connecting the slave bluetooth module and LED Matrix to another 328p board.

  • TXD(buletooth) ------ RXD(328p)
  • Middle pin(LED Matrix) ------ 5(328p)
  • The picture is as following.

    I use a shield to supply 5V power to the two boards and other components.

    Program and effort

    The program with the master bluetooth module is the following:

    
    	void setup() {
    	  // put your setup code here, to run once:
    	  Serial.begin(9600);
    	}
    
    	void loop() {
    	  // put your main code here, to run repeatedly:
    	  int i = analogRead(A0) / 4;
    	  Serial.write(i);
    	  delay(100);
    	}
    

    You can download the master program here.

    The program with the master bluetooth module is the following:

    
    	#include <Adafruit_NeoPixel.h>
    
    	#define Red Color(255,0,0)
    	#define Green Color(0,255,0)
    	#define Blue Color(0,0,255)
    
    	#define PIN            5
    	#define NUMPIXELS      25
    
    	Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
    
    	void setup() {
    	  // put your setup code here, to run once:
    	  Serial.begin(9600);
    	  pixels.begin(); // This initializes the NeoPixel library.
    	  pixels.show();
    	}
    
    	void loop() {
    	  // put your main code here, to run repeatedly:
    	  int brightness;
    	  if (Serial.available()) {
    		brightness = Serial.read();
    		Serial.println(brightness);
    		lightLED(pixels.Red, brightness);         //Order lit the red light
    
    		lightLED(pixels.Green, brightness);       //Order lit the green light
    
    		lightLED(pixels.Blue, brightness);        //Order lit the blue light
    
    
    	  }
    	}
    	void lightLED(uint32_t c, int brightness) {
    	  // For a set of NeoPixels the first NeoPixel is 0, second is 1, all the way up to the count of pixels minus one.
    	  for (int i = 0; i << NUMPIXELS; i++) {
    		pixels.setPixelColor(i, c); // Moderately bright green color.
    		pixels.setBrightness(brightness);
    		pixels.show(); 
    	  }
    	}
    

    You can download the slave program here.

    When the two board is power on, the two bluetooth modules connect automatically, turn around the potentiometer, the brightness of LED Matrix connect to the anothe board changes.

    Home

    Description

    This project uses mobile App to control LED Matrix through the Bluetooth wireless communicaiton. The interface of the App displays below, when you press the arrow key of different direction, the LED Matrix will display the same arrow direction.

    You can download the App software here.


    Connection

    Connect the bluetooth and the LED Matrix to the 328P, 328P connect to the computer through FabISP .

    Program and effort

    Uploading the Arduino program to the 328P through FabISP. You can download the program here.

    	#include <Adafruit_NeoPixel.h>
    
    	#define Red Color(255,0,0)
    	#define Green Color(0,255,0)
    	#define Blue Color(0,0,255)
    	#define White Color(10,10,10)
    
    	#define PIN            5
    	#define NUMPIXELS      25
    
    	unsigned char Position[5][5] =
    	{
    	  0, 1, 2, 3, 4,
    	  9, 8, 7, 6, 5,
    	  10, 11, 12, 13, 14,
    	  19, 18, 17, 16, 15,
    	  20, 21, 22, 23, 24
    	};
    
    	unsigned char Left[5][5] =
    	{
    	  0, 0, 1, 0, 0,
    	  0, 1, 1, 0, 0,
    	  1, 1, 1, 1, 1,
    	  0, 1, 1, 0, 0,
    	  0, 0, 1, 0, 0,
    	};
    	unsigned char Right[5][5] =
    	{
    	  0, 0, 1, 0, 0,
    	  0, 0, 1, 1, 0,
    	  1, 1, 1, 1, 1,
    	  0, 0, 1, 1, 0,
    	  0, 0, 1, 0, 0,
    	};
    	unsigned char Up[5][5] =
    	{
    	  0, 0, 1, 0, 0,
    	  0, 1, 1, 1, 0,
    	  1, 1, 1, 1, 1,
    	  0, 0, 1, 0, 0,
    	  0, 0, 1, 0, 0,
    	};
    	unsigned char Down[5][5] =
    	{
    	  0, 0, 1, 0, 0,
    	  0, 0, 1, 0, 0,
    	  1, 1, 1, 1, 1,
    	  0, 1, 1, 1, 0,
    	  0, 0, 1, 0, 0,
    	};
    	unsigned char LeftUp[5][5] =
    	{
    	  1, 1, 1, 1, 0,
    	  1, 1, 0, 0, 0,
    	  1, 0, 1, 0, 0,
    	  1, 0, 0, 1, 0,
    	  0, 0, 0, 0, 1,
    	};
    	unsigned char RightUp[5][5] =
    	{
    	  0, 1, 1, 1, 1,
    	  0, 0, 0, 1, 1,
    	  0, 0, 1, 0, 1,
    	  0, 1, 0, 0, 1,
    	  1, 0, 0, 0, 0,
    	};
    	unsigned char LeftDown[5][5] =
    	{
    	  0, 0, 0, 0, 1,
    	  1, 0, 0, 1, 0,
    	  1, 0, 1, 0, 0,
    	  1, 1, 0, 0, 0,
    	  1, 1, 1, 1, 0,
    	};
    	unsigned char RightDown[5][5] =
    	{
    	  1, 0, 0, 0, 0,
    	  0, 1, 0, 0, 1,
    	  0, 0, 1, 0, 1,
    	  0, 0, 0, 1, 1,
    	  0, 1, 1, 1, 1,
    	};
    	byte readBuffer[4];
    	unsigned int Command;   //接受按键:红外遥控器和手机app
    	Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
    
    
    	void setup() {
    	  pixels.begin(); // This initializes the NeoPixel library.
    	  pixels.show();
    	  Serial.begin(9600);
    	}
    
    	void loop() {
    	  Command = 0;
    	  getCommand();    //获取按键
    	  Serial.println(Command, HEX);
    
    	  switch (Command) {
    		case 0x4c53 :         //
    		  displayChar(Left);
    		  break;
    		case 0x5253 :         // 
    		  displayChar(Right);
    		  break;
    		case 0x5553:                 //
    		  displayChar(Up);
    		  break;
    		case 0x4453:                 //
    		  displayChar(Down);
    		  break;
    		case 0x4452:                 //
    		 displayChar(RightDown);
    		  break;
    		case 0x444c:
    		  displayChar(LeftDown);
    		  break;
    
    		case 0x554c :
    		  displayChar(LeftUp);
    		  break;
    		case 0x5552 :
    		  displayChar(RightUp);
    		  break;
    	  }
    	}
    
    	void getCommand() {
    	  char flag = 1;
    	  uint8_t num = 0;
    	  while (flag) {
    		if (Serial.available()) {
    		  uint8_t tempBuffer = Serial.read();
    		  readBuffer[num] = tempBuffer;
    		  num++;
    		  if (tempBuffer == 0x0D) {
    			Command = (readBuffer[0] <<<< 8) | (readBuffer[1]);
    			flag = 0;
    		  }
    		}
    	  }
    	}
    
    	void  displayChar(unsigned char Char[5][5]) {
    	  for (int i = 0; i << 5; i++) {
    		for (int j = 0; j << 5; j++) {
    		  if (Char[i][j]) {  //如果值为1, 显示红
    			pixels.setPixelColor(Position[i][j], pixels.Green);
    	//        pixels.setBrightness(30);
    		  }
    		  else {
    			pixels.setPixelColor(Position[i][j], pixels.White);
    			pixels.setBrightness(5);
    		  }
    		}
    	  }
    	  pixels.show();
    	}
    
    	void lightLED(uint32_t c) {
    	  for (int i = 0; i << NUMPIXELS; i++) {
    		pixels.setPixelColor(i, c); // Moderately bright green color.
    		pixels.setBrightness(30);
    		pixels.show(); // This sends the updated pixel color to the hardware.
    	  }
    	}
    

    Connect you mobile to the bluetooth, the password of the bluetooth is "1234". Then open the App, select the connection name you just add, App will display the message if you connect correctly.

    Then press the different arrow key in the App interface, the same arrow will display in the LED Matrix. You can see the effort on the following video.

    Home