Help with Controlling Display via Android Bluetooth Controller

Hey Everyone,

So I found this thread http://forum.arduino.cc/index.php/topic,91411.0.html on here awhile ago for a simple pong like game and I was able to load it and get it running on my display without any problems.

I want to try and set it up so I can control it using an Android Bluetooth Controller and an HC-05.

I found two different ways to control it but my programing skills aren't very good and I'm having a lot of trouble trying to get it to work. The first way was using this Android program TechBitar - Home which I thought would be easiest to use with the PWM function of the slide bar to control the paddle. The website also has some code to input in the Arduino code but at that point I started to get confused.

The other way was to use this Android Bluetooth Serial Controller https://play.google.com/store/apps/details?id=mBluetoothSerialController.nomal and edit the Arduino code so that I would just need two buttons, one to move the paddle up and one to move the paddle down. I'm pretty sure my bluetooth code isn't correct on the code I currently have and I had some simple code written using if else statements but after that point I wasn't sure how to implement it in the Arduino:

#include <LedControl.h>			//Library used to control the matrix
#include <SoftwareSerial.h>		//Library used 


int incomingP1Byte = 0;   // variable to store serial data

LedControl myMatrix = LedControl(11,13,10, 1);

//const int ledPin = 8;     // an LED used to indicate visually when a known 
						   // command is received
int column = 1, row = random(8)+1;  //randomly selects a spot for the ball to start
int directionX = 1, directionY = 1;	//make the ball travel left to right
int paddle1 = 5, paddle1Val;		//pin number 5 on the arduino, and the value that is received from the potentometer 'paddle1Val'
int speed = 300;					//ball movement speed in milliseconds
int counter = 0;					//not sure if this is needed --> mult = 10;		
	
SoftwareSerial player1Serial(2, 3); // Initialize SoftwareSerial (RX, TX) for the bluetooth connections



void setup()
{
  // sets the LED pin as outputs:
  //pinMode(ledPin, OUTPUT);

  player1Serial.begin(9600);
  bluetooth.begin(115200);  // The Bluetooth Mate defaults to 115200bps
  bluetooth.print("$$");  // Enter command mode
  delay(100);  // Short delay, wait for the Mate to send back CMD
  bluetooth.println("U,9600,N");  // Temporarily Change the baudrate to 9600, no parity
  // 115200 can be too fast at times for NewSoftSerial to relay the data reliably
  bluetooth.begin(9600);  // Start bluetooth serial at 9600

  myMatrix.shutdown(0, false);  //LED Matrix setup
  myMatrix.setIntensity(0, 8);	//LED Matrix sets brightness 0 - 15
  myMatrix.clearDisplay(0);		//LED Matrix
  randomSeed(analogRead(0));
  ballmissed();
}

void loop()
{

	//if some date is sent over bluetooth, it reads it and saves it in the input variable
	if(player1Serial.available() > 0){     
	  incomingByte = player1Serial.read();   
	  flag=0;
	}
	
	// check incoming byte for direction
	//bytes from 1 and 2 reserved for Left Paddle


	// if the byte is equal to "1", make the left paddle go up
	if (incomingByte == 1){
	player1(paddle_up);
	delay(25);
	}

	// if the byte is equal to "2", make the left paddle go down
	else if (incomingByte == 2){
	player1(paddle_down);
	delay(25);
	}

	else {
	stopPaddle();
	}







  paddle1Val = analogRead(paddle1);			//originally setup to read information from an analog potentometer, not sure how to change this to work with the bluetooth input
  paddle1Val = map(paddle1Val, 0, 6, 0,6);  //tried to convert map function to accept digital input, not sure if it worked yet
  column += directionX;
  row += directionY;


  if (column == 6 && directionX == 1 && (paddle1Val == row || paddle1Val+1 == row || paddle1Val+2 == row)) {directionX = -1;}  //checks to see if the ball hit the paddle
  if (column == 0 && directionX == -1 ) {directionX = 1;}	//checks to see if the ball hits the top
  if (row == 7 && directionY == 1 ) {directionY = -1;}		//or bottom and
  if (row == 0 && directionY == -1 ) {directionY = 1;}		//reverses the ball direction
  if (column == 7) { ballmissed();}							//if the ball hits column 7 then its past the paddle and it jumps to ballmissed function
  myMatrix.clearDisplay(0);

  //these are used to draw the paddle
  myMatrix.setLed(0, column, row, HIGH);
  myMatrix.setLed(0, 7, paddle1Val, HIGH);
  myMatrix.setLed(0, 7, paddle1Val+1, HIGH);
  myMatrix.setLed(0, 7, paddle1Val+2, HIGH);

   
  if (!(counter % 10)) {speed -= 5; }			//decreases the speed
  delay(speed);
  counter++;		//increase counter value 
}


void ballmissed() {
  for (int x=0; x<3; x++){
	myMatrix.clearDisplay(0);
	delay(250);
	for (int y=0; y<8; y++) {
	  myMatrix.setRow(0, y, 255);
	}
	delay(250);
  }
  counter=0;			//resets the values
  speed=300;		
  column=0;
  row = random(8)+1;	//chooses a new starting location
}

If someone could help me figure out one or the other I'd really appreciate it. Please point out any of my mistakes so I can understand what I did incorrectly. Thanks everyone!

I can´t see bluetooth being declared anywhere.

Please post your hardware setup.

Thanks for replying Smithy,

I'm using an Arduino Uno, My Max7219 is connected using SPI on pins 11, 12, 10. The HC-05 bluetooth module has its RX connected to the Arduino Pin 3 TX and the bluetooth module Tx connected to the Arduino Pin 2 Rx.

I connected the bluetooth according to this line in the code that I was trying to use:

SoftwareSerial player1Serial(2, 3); // Initialize SoftwareSerial (RX, TX) for the bluetooth connections

I'm able to pair my phone with the bluetooth module without any problem.

I noticed a few small mistakes in my code so I corrected them:

#include <LedControl.h>			//Library used to control the matrix
#include <SoftwareSerial.h>		//Library used 


int incomingP1Byte = 0;   // variable to store serial data

LedControl myMatrix = LedControl(11,13,10, 1);

//const int ledPin = 8;     // an LED used to indicate visually when a known 
						   // command is received
int column = 1, row = random(8)+1;  //randomly selects a spot for the ball to start
int directionX = 1, directionY = 1;	//make the ball travel left to right
int paddle1 = 5, paddle1Val;		//pin number 5 on the arduino, and the value that is received from the potentometer 'paddle1Val'
int speed = 300;					//ball movement speed in milliseconds
int counter = 0;					//not sure if this is needed --> mult = 10;		
	
SoftwareSerial player1Serial(2, 3); // Initialize SoftwareSerial (RX, TX) for the bluetooth connections



void setup()
{
  // sets the LED pin as outputs:


  player1Serial.begin(9600);
  bluetooth.begin(115200);  // The Bluetooth Mate defaults to 115200bps
  bluetooth.print("$$");  // Enter command mode
  delay(100);  // Short delay, wait for the Mate to send back CMD
  bluetooth.println("U,9600,N");  // Temporarily Change the baudrate to 9600, no parity
  // 115200 can be too fast at times for NewSoftSerial to relay the data reliably
  bluetooth.begin(9600);  // Start bluetooth serial at 9600

  myMatrix.shutdown(0, false);  //LED Matrix setup
  myMatrix.setIntensity(0, 8);	//LED Matrix sets brightness 0 - 15
  myMatrix.clearDisplay(0);		//LED Matrix
  randomSeed(analogRead(0));
  ballmissed();
}

void loop()
{

	//if some date is sent over bluetooth, it reads it and saves it in the input variable
	if(player1Serial.available() > 0){     
	  incomingP1Byte = player1Serial.read();   
	  flag=0;
	}
	
	// check incoming byte for direction
	//bytes from 1 and 2 reserved for Left Paddle


	// if the byte is equal to "1", make the left paddle go up
	if (incomingP1Byte == 1){
	player1(paddle_up);
	delay(25);
	}

	// if the byte is equal to "2", make the left paddle go down
	else if (incomingP1Byte == 2){
	player1(paddle_down);
	delay(25);
	}

	else {
	stopPaddle();
	}







  paddle1Val = analogRead(paddle1);			//originally setup to read information from an analog potentometer, not sure how to change this to work with the bluetooth input
  paddle1Val = map(paddle1Val, 0, 6, 0,6);  //tried to conver map function to accept digital input, not sure if it worked yet
  
  column += directionX;
  row += directionY;


  if (column == 6 && directionX == 1 && (paddle1Val == row || paddle1Val+1 == row || paddle1Val+2 == row)) {directionX = -1;}  //checks to see if the ball hit the paddle
  if (column == 0 && directionX == -1 ) {directionX = 1;}	//checks to see if the ball hits the top
  if (row == 7 && directionY == 1 ) {directionY = -1;}		//or bottom and
  if (row == 0 && directionY == -1 ) {directionY = 1;}		//reverses the ball direction
  if (column == 7) { ballmissed();}							//if the ball hits column 7 then its past the paddle and it jumps to ballmissed function
  myMatrix.clearDisplay(0);

  //these are used to draw the paddle
  myMatrix.setLed(0, column, row, HIGH);
  myMatrix.setLed(0, 7, paddle1Val, HIGH);
  myMatrix.setLed(0, 7, paddle1Val+1, HIGH);
  myMatrix.setLed(0, 7, paddle1Val+2, HIGH);

   
  if (!(counter % mult)) {speed -= 5; mult * mult;}			//decreases the speed
  delay(speed);
  counter++;		//increase counter value 
}


void ballmissed() {
  for (int x=0; x<3; x++){
	myMatrix.clearDisplay(0);
	delay(250);
	for (int y=0; y<8; y++) {
	  myMatrix.setRow(0, y, 255);
	}
	delay(250);
  }
  counter=0;			//resets the values
  speed=300;		
  column=0;
  row = random(8)+1;	//chooses a new starting location
}

My Max7219 is connected using SPI on pins 11, 12, 10.

LedControl myMatrix = LedControl(11,13,10, 1);

One of these is wrong.