Bluetooth coding help for nes controller *Solved*

I have searched high and low, and to no avail I cant quite seem to find what I am looking for. I have compiled some code between other peoples projects, cleaned it up, and used tutorials I watched and read online to hopefully make what I thought Might work.
What I am trying to do, is take an NES controller and make it wireless through bluetooth. Its been done with IR many times, or as bluetooth to a platform with HID. I followed this page for some general ideas and modified his code: Nes wireless controller
He uses the original shift register mc4021 on both ends, I also followed his wiring diagram. But as mentioned I changed his code, cleaned it up. and added to it to hopefully make the bluetooth work.

Hardware:
HC-05 Bluetooth Modules (one as slave one as master)
Arduino Unos (for prototyping before I just use an atmel chip)

Here is the Transmitter code I have

#include <SoftwareSerial.h>

int nesClock = 8;
int nesLatch = 7;
int nesData = 5;
int x;
byte nesPad = 0;
int txPin = 10;
int rxPin = 11;
const byte BTpin = 4;

SoftwareSerial nesBT(rxPin, txPin);
boolean BTconnected = false;


void setup() {
  pinMode(BTpin, INPUT);
  pinMode(nesClock, OUTPUT);
  pinMode(nesLatch, OUTPUT);
  pinMode(nesData, INPUT);
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  digitalWrite(nesClock, LOW);
  digitalWrite(nesLatch, LOW);

  Serial.begin(9600);

  while (!BTconnected)
  {
    if ( digitalRead(BTpin) == HIGH)  {
      BTconnected = true;
    };
  }
  nesBT.begin(9600);
}
void loop() {
  //  if (Serial.available())
  // {
  digitalWrite(nesLatch, HIGH);
  delayMicroseconds(12);
  digitalWrite(nesLatch, LOW);
  // The Latch has now been sent, so send clock signal and read the button states
  for (x = 0; x < 8; x++) {
    digitalWrite(nesClock, LOW);
    delayMicroseconds(4);
    nesPad = nesPad << 1; // Shift the bits of nespad to the left
    nesPad = nesPad + digitalRead(nesData); // Add the new pin state, either a 1 or 0
    digitalWrite(nesClock, HIGH); // Next button state is output to the data line
    delayMicroseconds(4);
  }
  digitalWrite(nesClock, LOW);

  digitalWrite(13, true); // Flash a light to show transmitting
  nesBT.write(nesPad);
  delay(1);
  // Serial.println(nesPad, BIN);
  digitalWrite(13, false);
  //}
}

and here is the receiver code

#include <SoftwareSerial.h>

byte number;

int A = 9;
int B = 8;
int Select = 7;
int Start = 6;
int Up = 5;
int Down = 4;
int Left = 3;
int Right = 2;
int txPin = 10;
int rxPin = 11;
int x;
int nesByte;

SoftwareSerial nesData(rxPin, txPin);
boolean BTconnected = false;
const byte BTpin = 12;

void setup() {
  pinMode(BTpin, INPUT);
  pinMode(13, OUTPUT);
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  pinMode(A, OUTPUT);
  pinMode(B, OUTPUT);
  pinMode(Up, OUTPUT);
  pinMode(Down, OUTPUT);
  pinMode(Left, OUTPUT);
  pinMode(Right, OUTPUT);
  pinMode(Start, OUTPUT);
  pinMode(Select, OUTPUT);

  Serial.begin(9600);
  while (!BTconnected)
  {
    if ( digitalRead(BTpin) == HIGH)  {
      BTconnected = true;
    };
  }
  nesData.begin(9600);

}

void loop() {
  digitalWrite(13, false);
  if (Serial.available())
  {
    nesByte = nesData.read();

    byte buf[0];  // sets variable for received message
    // VW_MAX_MESSAGE_LEN is equal to 80, it is a constant in VirtualWire.h

    byte buflen = 8;  // sets maximum message length

    if (nesByte = buflen) {
      // Checks to see if the message was received
      // and if it was within than the maximum length
      digitalWrite(13, true);
      number = buf[0];
      Serial.println(number, BIN);
      for (x = 0; x < 8; x++) {
        // This code reads each bit in the received byte
        // Depending on the bit, the corresponding pin, which is two places
        // ahead of the number in the loop, is toggled high or low
        if (bitRead(number, x) == 1) {
          digitalWrite(x + 2, HIGH);
        } else {
          digitalWrite(x + 2, LOW);
        }
      }
    }
  }
}

any help or suggestions would be greatly appreciate. I might have overstepped my knowledge base with this one. The bluetooth modules connect. Nothing seems to send through however I feel like I am missing something. I have tried swapping RX and TX lines just incase. However I was positive I had them right.

From what I understand, Its taking the data string from the shift register, sending it through bluetooth, decoding it, and resending the button presses back through the shift register at the other end into the nintendo.

I have thought about removing the shift register on the controller side, and just sending the button presses through bluetooth and then having the shift register convert it.

zerorob:
I have tried swapping RX and TX lines just incase. However I was positive I had them right.

This is meaningless. There is only one way to connect serial, and that is Tx>Rx and Rx<Tx. You could use wire connections instead, thereby proving that Bluetooth really is the problem. If that proves to be the case, it may be in their configuration.

Nick_Pyner:
This is meaningless. There is only one way to connect serial, and that is Tx>Rx and Rx<Tx. You could use wire connections instead, thereby proving that Bluetooth really is the problem. If that proves to be the case, it may be in their configuration.

I know It was meaningless, and thats the way it needs to be wired, But mistakes happen and sometimes its the simplest things get mixed up. I wanted to iterate that I checked that and the code. and tried.
Now I have tried wiring the 2 units together as well, Code doesn’t seem to send the data at all.

The logic of

  while (!BTconnected)
  {
    if ( digitalRead(BTpin) == HIGH)  {
      BTconnected = true;
    };
  }

may be suss, it may also be redundant junk. You might move Bluetooth to hardware serial, thereby checking what is sent on the serial monitor.

I removed that code, Changed a couple other things, Now it works when I hardwire the arduinos together. However the bluetooth connectivity doesn't seem to want to send the data/receive it. the 2 units are paired tho.

Nick_Pyner:
This is meaningless. There is only one way to connect serial, and that is Tx>Rx and Rx<Tx. You could use wire connections instead, thereby proving that Bluetooth really is the problem. If that proves to be the case, it may be in their configuration.

…except for the boards that label TX as RX and RX as TX… :slight_smile:

and I finally got it. RX and TX on master have to goto rx/tx on the arduino. and rx goes to tx on the slave.

and now it seems to have stopped. HMMM

Alright, Everything connects and works… ISH. Whats happening now, is it seems to be reading them out as random buttons if I hold anything down. But work fine if I just quickly single push it. here is my somewhat updated code:

Transmitter from controller:

#include <SoftwareSerial.h>

int nesClock = 8;
int nesLatch = 7;
int nesData = 5;
int x;
byte nesPad = 0;
int txPin = 10;
int rxPin = 11;
const byte BTpin = 4;

SoftwareSerial nesBT(rxPin, txPin);
boolean BTconnected = false;


void setup() {
  pinMode(13, OUTPUT);
  pinMode(BTpin, INPUT);
  pinMode(nesClock, OUTPUT);
  pinMode(nesLatch, OUTPUT);
  pinMode(nesData, INPUT);
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  digitalWrite(nesClock, LOW);
  digitalWrite(nesLatch, LOW);

  Serial.begin(9600);

  nesBT.begin(9600);
}
void loop() {
  digitalWrite(nesLatch, HIGH);
  delayMicroseconds(12);
  digitalWrite(nesLatch, LOW);
  // The Latch has now been sent, so send clock signal and read the button states
  for (x = 0; x < 8; x++) {
    digitalWrite(nesClock, LOW);
    delayMicroseconds(4);
    nesPad = nesPad << 1; // Shift the bits of nespad to the left
    nesPad = nesPad + digitalRead(nesData); // Add the new pin state, either a 1 or 0
    digitalWrite(nesClock, HIGH); // Next button state is output to the data line
    delayMicroseconds(4);
  }
  digitalWrite(nesClock, LOW);

  digitalWrite(13, true); // Flash a light to show transmitting
  nesBT.write(nesPad);
  delay(1);
  Serial.println(nesPad, BIN);
  digitalWrite(13, false);
}

and the Receiver on the nes:

#include <SoftwareSerial.h>

int A = 9;
int B = 8;
int Select = 7;
int Start = 6;
int Up = 5;
int Down = 4;
int Left = 3;
int Right = 2;
int txPin = 10;
int rxPin = 11;
int x;
byte nesByte;

SoftwareSerial nesData(rxPin, txPin);
boolean BTconnected = false;
const byte BTpin = 12;

void setup() {
  pinMode(BTpin, INPUT);
  pinMode(13, OUTPUT);
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  pinMode(A, OUTPUT);
  pinMode(B, OUTPUT);
  pinMode(Up, OUTPUT);
  pinMode(Down, OUTPUT);
  pinMode(Left, OUTPUT);
  pinMode(Right, OUTPUT);
  pinMode(Start, OUTPUT);
  pinMode(Select, OUTPUT);

  Serial.begin(9600);
  nesData.begin(9600);

}

void loop() {
  digitalWrite(13, false);
  if (nesData.available())
  {
    nesByte = nesData.read();
    digitalWrite(13, true);
    nesData.println(nesByte, BIN);
    for (x = 0; x < 8; x++) {
      // This code reads each bit in the received byte
      // Depending on the bit, the corresponding pin, which is two places
      // ahead of the number in the loop, is toggled high or low
      if (bitRead(nesByte, x) == 1) {
        digitalWrite(x + 2, HIGH);
      } else {
        digitalWrite(x + 2, LOW);
      }
    }
  }
}

I thought I had it at one point, But just not quite there.