I'm trying to use two teensy 4.0 microcontrollers to translate serial data to gamepad outputs. I have one teensy taking serial data from one host over USB and sending it to the other teensy using the pins which sends the gamepad output to another host over USB. I set up a simple low-pass filter to try and smooth the PWM voltage, which works ok but, when I send button data I get a small amount of joystick output up and right. Did I connect something wrong? I have them wired like this (The Arduinos represent the Teensys, it was the closest thing in the diagram software). The orange wires are not connected to each other, they connect the same pins on both Teensys. EG: Pin 0 to pin 0, pin 1 to pin 1, etc.:
Here is my code.
Left Teensy:
Right Teensy:
The capacitors are 50V electrolytic capacitors. https://www.amazon.com/dp/B0CMQC587X?ref=ppx_yo2ov_dt_b_fed_asin_title&th=1
Here is my code.
Left Teensy:
C++:
/* SerialReceive.ino
Takes serial input and sends it over pins.
*/
const int ledPin = 13;
const int joyXPin = 5;
int xVal = 0;
const int joyYPin = 6;
int yVal = 0;
int buttonIndex = 0, buttonState = 0;
void setup() {
pinMode(ledPin, OUTPUT);
// Button pins
pinMode(0, OUTPUT);
pinMode(1, OUTPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
// Joystick pins
pinMode(joyXPin, OUTPUT);
pinMode(joyYPin, OUTPUT);
// Center joystick
analogWrite(joyXPin, 128);
analogWrite(joyYPin, 128);
Serial.begin(38400);
}
int count = 0;
char data[1024];
bool finished = false;
void loop() {
digitalWrite(ledPin, HIGH); // set the LED on
// Read data
if(Serial.available() > 0 && !finished){
data[count] = Serial.read();
if(data[count] == '!'){
finished = true;
}
count++;
}
if(finished){
// Process data
int index = 1;
// Joystick data processing
if(data[0] == 'J'){
xVal = 0;
yVal = 0;
// Get the number of digits for x
int numDigits = pow(10, data[index++] - 49);
// Start reading x
while(data[index] != ','){
xVal += (data[index] - 48) * numDigits;
numDigits /= 10;
index++;
}
// Skip comma
index++;
// Get the number of digits for y
numDigits = pow(10, data[index++] - 49);
// Start reading y
while(data[index] != '!'){
yVal += (data[index] - 48) * numDigits;
numDigits /= 10;
index++;
}
} else if(data[0] == 'B'){
// Read button
buttonIndex = data[index++] - 48;
buttonState = data[index++] - 48;
}
if(data[0] == 'J'){
analogWrite(joyXPin, xVal);
analogWrite(joyYPin, yVal);
} else if(data[0] == 'B') {
digitalWrite(buttonIndex, buttonState);
}
for(int i = 0; i < count; i++){
data[i] = '\0';
}
count = 0;
finished = false;
}
if(!Serial.available()){
delay(25);
}
}
Right Teensy:
C++:
/* GamepadSend.ino
Takes data from pins and outputs gamepad commands.
*/
#include <cmath>
const int ledPin = 13;
const int deadZoneVal = 35;
void setup() {
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
// Button pins
pinMode(0, INPUT_PULLDOWN);
pinMode(1, INPUT_PULLDOWN);
pinMode(2, INPUT_PULLDOWN);
pinMode(3, INPUT_PULLDOWN);
// Joystick pins
pinMode(A0, INPUT_DISABLE);
pinMode(A1, INPUT_DISABLE);
}
void loop() {
int buttonVal = 0;
// read the digital inputs and set the buttons
for(int button = 0; button < 4; button++){
buttonVal = digitalRead(button);
Joystick.button(button+1, buttonVal);
}
// read analog input and set X position
int xVal = analogRead(A0);
// Deadzone
if(abs(xVal - 512) < deadZoneVal){
xVal = 512;
}
Joystick.X(xVal);
// read analog input and set Y position
int yVal = analogRead(A1);
// Deadzone
if(abs(yVal - 512) < deadZoneVal){
yVal = 512;
}
Joystick.Y(yVal);
// a brief delay
delay(25);
}
The capacitors are 50V electrolytic capacitors. https://www.amazon.com/dp/B0CMQC587X?ref=ppx_yo2ov_dt_b_fed_asin_title&th=1