Last active
March 30, 2021 15:58
-
-
Save DusteDdk/d6058099da753e4b74550d77bec98ca7 to your computer and use it in GitHub Desktop.
Paddle for stella
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For a 1 axis joystick, suitable for use with the Stella Atari 2600 emulator | |
// Uses the Joystick lib https://github.com/MHeironimus/ArduinoJoystickLibrary/tree/version-2.0 | |
// And the box https://www.thingiverse.com/thing:4223937 | |
// It's a USB modem the first 10 seconds after poweer-up, so you can connect and calibrate it. | |
// Note: you must set sensitivity and calibrate it after first programming | |
// Schematic: | |
// Pin 21 (A3) goes to potmeter center | |
// VCC goes to other side of potmeter | |
// GND goes to one side of potmeter | |
// Pin 20 (A2) goes to one side of tact switch | |
// If it's moving the opposite direction, then switch around VCC and GND. | |
// If it's too jittery, try increasing HYS | |
// If it's not jittery enough, try decreasing HYS ;-) | |
#define HYS 2 | |
#define POT 21 | |
#define BTN 20 | |
#include <Joystick.h> | |
#include <EEPROM.h> | |
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_GAMEPAD, | |
1, 0, // Button Count, Hat Switch Count | |
true, false, false, // X, but no Y or Z Axis | |
false, false, false, // No Rx, Ry, or Rz | |
false, false, // No rudder or throttle | |
false, false, false); // No accelerator, brake, or steering | |
int potMin= 0; | |
int potMax = 1023; | |
float scale=1.0; | |
float sens = 1.0; | |
void setScale() { | |
scale = 1023.0/(float)(potMax-potMin); | |
} | |
int getVal(int pot) { | |
pot *= sens; | |
if(pot < potMin) { | |
pot=potMin; | |
} | |
if(pot > potMax) { | |
pot=potMax; | |
} | |
return (pot - potMin)*scale; | |
} | |
void setup() { | |
// Initialize Button Pins | |
pinMode(BTN, INPUT_PULLUP); | |
pinMode(POT, INPUT); | |
// Initialize Joystick Library | |
Joystick.begin(); | |
Joystick.setXAxisRange(1023, 0); | |
int countDown=10000; | |
bool gotData=false; | |
char k=0; | |
int state=0; | |
int last=0; | |
int val; | |
potMin = EEPROM[0]; | |
potMin |= (EEPROM[1] << 8); | |
potMax = EEPROM[2]; | |
potMax |= (EEPROM[3] << 8); | |
EEPROM.get(4,sens); | |
Serial.begin(9600); | |
while(gotData || countDown) { | |
countDown--; | |
delay(1); | |
state = analogRead(POT); | |
if(state+2 < last || state -2 > last) { | |
last=state; | |
Serial.print("Pot: "); | |
Serial.println(state); | |
Serial.print("Val: "); | |
setScale(); | |
Serial.println(getVal(state)); | |
} | |
if(Serial.available()) { | |
gotData=true; | |
k=Serial.read(); | |
if(k=='s') { | |
Serial.println("Saved..."); | |
EEPROM[0]= potMin & 0xFF; | |
EEPROM[1]= (potMin >> 8) & 0xFF ; | |
EEPROM[2]= potMax & 0xFF; | |
EEPROM[3]= (potMax >> 8) & 0xFF ; | |
EEPROM.put(4, sens); | |
delay(1000); | |
Serial.end(); | |
gotData=false; | |
countDown=0; | |
} else if(k=='l') { | |
delay(10); | |
Serial.print("Left: "); | |
Serial.println(state); | |
potMax=state; | |
} else if(k=='r') { | |
Serial.print("Right: "); | |
Serial.println(state); | |
potMin=state; | |
} else if( k=='1') { | |
sens = 2.0; | |
} else if( k=='2') { | |
sens = 1.5; | |
} else if( k=='3') { | |
sens = 1.0; | |
} else if( k=='4') { | |
sens = 0.75; | |
} else if( k=='5') { | |
sens = 0.5; | |
} else if( k=='6') { | |
sens = 0.25; | |
} else if( k!='\r' && k!='\n') { | |
Serial.println("Current calibration:"); | |
Serial.print(" Left: "); | |
Serial.println(potMax); | |
Serial.print(" Right: "); | |
Serial.println(potMin); | |
Serial.print(" Sensitivity: "); | |
Serial.println(sens); | |
Serial.println("Turn pot to left postion, press l"); | |
Serial.println("Turn pot to right postion, press r"); | |
Serial.println("Sensitivity: 1=2.0 2=1.5 3=1.0 4=0.75 5=0.5 6=0.25"); | |
Serial.println("Then press s to save"); | |
} | |
} | |
} | |
setScale(); | |
} | |
// Last state of the buttons | |
int lastButtonState = 0; | |
int lastPot= 0; | |
int coolDown=0; | |
void loop() { | |
int currentButtonState = !digitalRead(BTN); | |
int currentPot = analogRead(POT); | |
if(coolDown) { | |
coolDown--; | |
} | |
if (currentButtonState != lastButtonState && !coolDown) | |
{ | |
Joystick.setButton(0, currentButtonState); | |
lastButtonState = currentButtonState; | |
coolDown=100; | |
} | |
if( (currentPot + HYS) > lastPot || (currentPot - HYS < lastPot) ) { | |
Joystick.setXAxis( getVal(currentPot) ); | |
lastPot = currentPot; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment