Hey folks,
Iam new to Microcontroller Programming, as a first Project i decide to develope a simple SNES-to-USB Gamepad device.
It's working like intended, Iam able to play my favourite old school SNES games on an Emulator.
Feel free to comment my Code
Greets Punnie
//Developed by Punnie, feel free to edit and copy.
//Good Documentation of the SNES Controller (German) http://www.rn-wissen.de/index.php/SNES_Controller
#include "DigiJoystick.h"
boolean pinState = false;
int clockPin = 2; //yellow on a European Controller
int latchPin = 5; //orange
int dataPin = 0; // red
//White is the +5v and brown ground
uint16_t controllerRegister = 0x0000;
void setup(){
pinMode(clockPin,OUTPUT);
pinMode(latchPin,OUTPUT);
pinMode(dataPin,INPUT);
digitalWrite(clockPin,HIGH);
digitalWrite(latchPin,LOW);
}
void loop(){
//Initalize the Gamepad have to be done once before reading the Gamepad's Register
digitalWrite(latchPin, HIGH);
delayMicroseconds(12);
digitalWrite(latchPin, LOW);
//Read the Gamepad's Register 16 bit long
for(int i=0; i<16; i++)
{
delayMicroseconds(6);
digitalWrite(clockPin, LOW);
pinState=!digitalRead(dataPin);
if(pinState){
controllerRegister |= (0x01<<i);
}else{
controllerRegister &= ~(0x01<<i);
}
delayMicroseconds(6);
digitalWrite(clockPin, HIGH);
}
//Set the Buttons on the USB-HID-Controler
DigiJoystick.setButtons((char) controllerRegister , (char) (controllerRegister >> 8));
DigiJoystick.delay(16);
}