Please I need help with my code.

Hi !

Forgive me for my bad English.

I'm trying to develop an wireless SNES controller to computers with two bluetooth modules HC-05, .

The receiver is a vusb keyboard to arduino. To make the transmitter, I use the code from a Brazilian programmer, Ricardo Gomes da Silva, which allows reading data from a SNES controller in Arduino.

The problem is that when I press a button, a state is triggered and keeps repeating the same key character all the time, until they cut off the communication between the bluetooth modules.

I wanted to create something like a "release key". I wanted the character is sent only if press the button. Does anyone have a suggestion of how I can program it ? My code is below.

 #include <SoftwareSerial.h>

SoftwareSerial BTMasterHC05(2,3); // RX | TX 
 
// Controller buttons
// (based on button-to-clock pulse assignment)
#define SNES_B        32768  // 1000000000000000
#define SNES_Y        16384  // 0100000000000000
#define SNES_SELECT   8192   // 0010000000000000
#define SNES_START    4096   // 0001000000000000
#define SNES_UP       2048   // 0000100000000000
#define SNES_DOWN     1024   // 0000010000000000
#define SNES_LEFT      512   // 0000001000000000
#define SNES_RIGHT     256   // 0000000100000000
#define SNES_A         128   // 0000000010000000
#define SNES_X          64   // 0000000001000000
#define SNES_L          32   // 0000000000100000
#define SNES_R          16   // 0000000000010000
 
// Arduino pins vs. SNES controller pins
// (default is latch 2, clock 3, data 4)
int LatchPin  = 5; // Latch
int ClockPin  = 6; // Clock
int DataPin   = 7; // Serial Data
 
// Current controller data
unsigned int ControllerData = 0;
 
// Setup the controller and serial output
void setup() {
  Serial.begin(9600);
  BTMasterHC05.begin(9600);
  pinMode(LatchPin,OUTPUT);
  pinMode(ClockPin,OUTPUT);
  pinMode(DataPin,INPUT);
 
  digitalWrite(LatchPin,HIGH);
  digitalWrite(ClockPin,HIGH);
}
 
// Read controller
void controllerRead() {
  // Reset controller states and data
  ControllerData = 0;
  digitalWrite(LatchPin,LOW);
  digitalWrite(ClockPin,HIGH);
 
  // Controller needs to latch the state of all buttons
  digitalWrite(LatchPin,HIGH);
  delayMicroseconds(12);
  digitalWrite(LatchPin,LOW);
  delayMicroseconds(6);
 
  // Read controller data (initial reading)
  ControllerData = digitalRead(DataPin);
 
  // Send 16 clock pulses, one for each button. 
  for (int i = 0; i < 16; i ++) {
	digitalWrite(ClockPin,LOW);
	delayMicroseconds(6);
	ControllerData = ControllerData << 1;
	ControllerData = ControllerData + digitalRead(DataPin) ;
	delayMicroseconds(6);
	digitalWrite(ClockPin,HIGH);
  }
 
  // Do a NOT, so '1' will be pressed buttons and '0' to the rest
  ControllerData = ~ControllerData;
}
 
// Program code
void loop() {
  // Read controller data
  controllerRead();
 
 
  if (ControllerData != 0) {
	Serial.print("Pressed:");
	if (ControllerData & SNES_B) {
	  BTMasterHC05.write('1'); Serial.println("1");
	}

delay(16);

  }
  
}

Thank you !

A wild guess is that your program needs to check if a new button is pressed (or released) and just send one message when that happens.

...R

Robin2:
A wild guess is that your program needs to check if a new button is pressed (or released) and just send one message when that happens.

...R

I tried to do a releaseKey function in the transmitter as the Arduino Leonardo. But I'm not able to do with the transmitter to stop sending data continuously.

Just below, is the current code. I thought with this exception the "BTMasterHC05. write", weren't going to send data. But unfortunately, continues sending data to the receiver.

 #include <SoftwareSerial.h>
//Cria um objeto do tipo SoftwareSerial
//chamado BTMasterHC05 e seta as portas 12 RX e 11 TX
SoftwareSerial BTMasterHC05(2,3); // RX | TX  

// Controller buttons
// (based on button-to-clock pulse assignment)
#define SNES_B        32768  // 1000000000000000
#define SNES_Y        16384  // 0100000000000000
#define SNES_SELECT   8192   // 0010000000000000
#define SNES_START    4096   // 0001000000000000
#define SNES_UP       2048   // 0000100000000000
#define SNES_DOWN     1024   // 0000010000000000
#define SNES_LEFT      512   // 0000001000000000
#define SNES_RIGHT     256   // 0000000100000000
#define SNES_A         128   // 0000000010000000
#define SNES_X          64   // 0000000001000000
#define SNES_L          32   // 0000000000100000
#define SNES_R          16   // 0000000000010000
 
// Arduino pins vs. SNES controller pins
// (default is latch 2, clock 3, data 4)
int LatchPin  = 5; // Latch
int ClockPin  = 6; // Clock
int DataPin   = 7; // Serial Data
 
// Current controller data
unsigned int ControllerData = 0;
 
// Setup the controller and serial output
void setup() {
  Serial.begin(9600);
  BTMasterHC05.begin(9600);
  pinMode(LatchPin,OUTPUT);
  pinMode(ClockPin,OUTPUT);
  pinMode(DataPin,INPUT);
 
  digitalWrite(LatchPin,HIGH);
  digitalWrite(ClockPin,HIGH);
}
 
// Read controller
void controllerRead() {
  // Reset controller states and data
  ControllerData = 0;
  digitalWrite(LatchPin,LOW);
  digitalWrite(ClockPin,HIGH);
 
  // Controller needs to latch the state of all buttons
  digitalWrite(LatchPin,HIGH);
  delayMicroseconds(12);
  digitalWrite(LatchPin,LOW);
  delayMicroseconds(6);
 
  // Read controller data (initial reading)
  ControllerData = digitalRead(DataPin);
 
  // Send 16 clock pulses, one for each button. 
  for (int i = 0; i < 16; i ++) {
	digitalWrite(ClockPin,LOW);
	delayMicroseconds(6);
	ControllerData = ControllerData << 1;
	ControllerData = ControllerData + digitalRead(DataPin) ;
	delayMicroseconds(6);
	digitalWrite(ClockPin,HIGH);
  }
 
  // Do a NOT, so '1' will be pressed buttons and '0' to the rest
  ControllerData = ~ControllerData;
}


void keyReleased() {
Serial.print("No button pressed");
}
 
// Program code
void loop() {
  // Read controller data
  controllerRead();
 
 
  if (ControllerData != 0) {
	Serial.print("Pressed:");
	if (ControllerData & SNES_B) {
	  BTMasterHC05.write('1');Serial.println("LED: on");
             }
 
            }else {
              if (ControllerData == 0) {
keyReleased();

              }
  }
 delay(16); 
}

Also tried a Boolean check, but it didn't work. When I test receiver with a test application that made the App Inventor 2, works correctly.

Thank you.

I don't know what your function controllerRead() is supposed to do. It seems as if it just changes the variable ControllerData between 0 and 1. If that is correct can you tell me what makes it do that and how ofter it does it (how many millisecs between changes).

Maybe you need code like this pseudo code

prevControlData = ControllerData;
controllerRead();
if (ControllerData != prevControlData and ControllerData == 1) {
   // code to send a message
}

...R

Robin2:
I don't know what your function controllerRead() is supposed to do. It seems as if it just changes the variable ControllerData between 0 and 1. If that is correct can you tell me what makes it do that and how ofter it does it (how many millisecs between changes).

Maybe you need code like this pseudo code...

Forgive me for being late, I'm at work right now. Well, you summed up the function "controllerRead ();" and that's it. In fact, this function controls the Clock signals, Latch and date, when a button is pressed.

When I get home tomorrow, after coming home from work, I'll try your suggestion.

Thank you so much for your help.

Best Regards.