Skip to content

Commit

Permalink
Added support for Arduino UNO R4
Browse files Browse the repository at this point in the history
Modified threshold values for joystick control
  • Loading branch information
Lorandil committed Dec 27, 2023
1 parent 7e276de commit 4de1133
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
17 changes: 9 additions & 8 deletions TinyJoypadUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,28 +57,29 @@ void InitTinyJoypad()
bool isLeftPressed()
{
uint16_t inputX = analogRead( LEFT_RIGHT_BUTTON );
return( ( inputX >= 750 ) && ( inputX < 950 ) );
serialPrintln( inputX );
return( ( inputX >= ANALOG_UPPER_LIMIT_MIN ) && ( inputX < ANALOG_UPPER_LIMIT_MAX ) );
}

/*-------------------------------------------------------*/
bool isRightPressed()
{
uint16_t inputX = analogRead( LEFT_RIGHT_BUTTON );
return( ( inputX > 500 ) && ( inputX < 750 ) );
return( ( inputX > ANALOG_LOWER_LIMIT_MIN ) && ( inputX < ANALOG_LOWER_LIMIT_MAX ) );
}

/*-------------------------------------------------------*/
bool isUpPressed()
{
uint16_t inputY = analogRead( UP_DOWN_BUTTON );
return( ( inputY > 500 ) && ( inputY < 750 ) );
return( ( inputY > ANALOG_LOWER_LIMIT_MIN ) && ( inputY < ANALOG_LOWER_LIMIT_MAX ) );
}

/*-------------------------------------------------------*/
bool isDownPressed()
{
uint16_t inputY = analogRead( UP_DOWN_BUTTON );
return( ( inputY >= 750 ) && ( inputY < 950 ) );
return( ( inputY >= ANALOG_UPPER_LIMIT_MIN ) && ( inputY < ANALOG_UPPER_LIMIT_MAX ) );
}

/*-------------------------------------------------------*/
Expand Down Expand Up @@ -113,25 +114,25 @@ void readAnalogJoystick()
/*-------------------------------------------------------*/
bool wasLeftPressed()
{
return( ( analogJoystickX >= 750 ) && ( analogJoystickX < 950 ) );
return( ( analogJoystickX >= ANALOG_UPPER_LIMIT_MIN ) && ( analogJoystickX < ANALOG_UPPER_LIMIT_MAX ) );
}

/*-------------------------------------------------------*/
bool wasRightPressed()
{
return( ( analogJoystickX > 500 ) && ( analogJoystickX < 750 ) );
return( ( analogJoystickX > ANALOG_LOWER_LIMIT_MIN ) && ( analogJoystickX < ANALOG_LOWER_LIMIT_MAX ) );
}

/*-------------------------------------------------------*/
bool wasUpPressed()
{
return( ( analogJoystickY > 500 ) && ( analogJoystickY < 750 ) );
return( ( analogJoystickY > ANALOG_LOWER_LIMIT_MIN ) && ( analogJoystickY < ANALOG_LOWER_LIMIT_MAX ) );
}

/*-------------------------------------------------------*/
bool wasDownPressed()
{
return( ( analogJoystickY >= 750 ) && ( analogJoystickY < 950 ) );
return( ( analogJoystickY >= ANALOG_UPPER_LIMIT_MIN ) && ( analogJoystickY < ANALOG_UPPER_LIMIT_MAX ) );
}

/*-------------------------------------------------------*/
Expand Down
33 changes: 31 additions & 2 deletions TinyJoypadUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
#undef USE_EXTENDED_CHECKS
#undef USE_SERIAL_PRINT
#else
#if defined(__AVR_ATmega2560__) || defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega328P__) || defined(_VARIANT_ARDUINO_ZERO_)
#if defined(__AVR_ATmega2560__) || defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega328P__) || defined(_VARIANT_ARDUINO_ZERO_) || defined(ARDUINO_UNOWIFIR4) || defined(ARDUINO_UNOR4_MINIMA)
// Arduino Mega 2560 (and compatible)
#define LEFT_RIGHT_BUTTON A0
#define UP_DOWN_BUTTON A3
Expand All @@ -57,9 +57,38 @@
#define SOUND_PORT_DDR DDRB
#define SOUND_PORT PORTB
#endif

// Analog limits for joystick control (controller/voltage dependent)
#if defined(__AVR_ATtiny85__)
const uint32_t ANALOG_LOWER_LIMIT_MIN = 500;
const uint32_t ANALOG_LOWER_LIMIT_MAX = 750;
const uint32_t ANALOG_UPPER_LIMIT_MIN = 750;
const uint32_t ANALOG_UPPER_LIMIT_MAX = 950;
#elif defined(__AVR_ATmega2560__) || defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega328P__)
const uint32_t ANALOG_LOWER_LIMIT_MIN = 500;
const uint32_t ANALOG_LOWER_LIMIT_MAX = 750;
const uint32_t ANALOG_UPPER_LIMIT_MIN = 750;
const uint32_t ANALOG_UPPER_LIMIT_MAX = 950;
#elif defined(_VARIANT_ARDUINO_ZERO_)
const uint32_t ANALOG_LOWER_LIMIT_MIN = 500;
const uint32_t ANALOG_LOWER_LIMIT_MAX = 750;
const uint32_t ANALOG_UPPER_LIMIT_MIN = 750;
const uint32_t ANALOG_UPPER_LIMIT_MAX = 950;
#elif defined(ARDUINO_UNOWIFIR4) || defined(ARDUINO_UNOR4_MINIMA)
const uint32_t ANALOG_LOWER_LIMIT_MIN = 450;
const uint32_t ANALOG_LOWER_LIMIT_MAX = 540;
const uint32_t ANALOG_UPPER_LIMIT_MIN = 540;
const uint32_t ANALOG_UPPER_LIMIT_MAX = 650;
#else
const uint32_t ANALOG_LOWER_LIMIT_MIN = 500;
const uint32_t ANALOG_LOWER_LIMIT_MAX = 750;
const uint32_t ANALOG_UPPER_LIMIT_MIN = 750;
const uint32_t ANALOG_UPPER_LIMIT_MAX = 950;
#endif

// use extensive checks (not memory restricted by ATtiny85...)
#define USE_EXTENDED_CHECKS
//#define USE_SERIAL_PRINT
#define USE_SERIAL_PRINT
#endif


Expand Down

0 comments on commit 4de1133

Please sign in to comment.