Help with ps2 controller

help i have wired up my ps2 reciver but now the serial monitor says cannot enter configuration mode this is the code /*******************************************************************************

  • This file is part of PsxNewLib. *
  •                                                                         *
    
  • Copyright (C) 2019-2020 by SukkoPera software@sukkology.net *
  •                                                                         *
    
  • PsxNewLib is free software: you can redistribute it and/or *
  • modify it under the terms of the GNU General Public License as published by *
  • the Free Software Foundation, either version 3 of the License, or *
  • (at your option) any later version. *
  •                                                                         *
    
  • PsxNewLib is distributed in the hope that it will be useful, *
  • but WITHOUT ANY WARRANTY; without even the implied warranty of *
  • MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  • GNU General Public License for more details. *
  •                                                                         *
    
  • You should have received a copy of the GNU General Public License *
  • along with PsxNewLib. If not, see Licenses - GNU Project - Free Software Foundation. *

  • This sketch will dump to serial whatever is done on a PSX controller. It is
  • an excellent way to test that all buttons/sticks are read correctly.
  • It's missing support for analog buttons, that will come in the future.
  • This example drives the controller by bitbanging the protocol, there is
  • another similar one using the hardware SPI support.
    */

#include <DigitalIO.h>
#include <PsxControllerBitBang.h>

#include <avr/pgmspace.h>
typedef const __FlashStringHelper * FlashStr;
typedef const byte* PGM_BYTES_P;
#define PSTR_TO_F(s) reinterpret_cast<const __FlashStringHelper *> (s)

const byte PIN_PS2_ATT = 13;
const byte PIN_PS2_CMD = 12;
const byte PIN_PS2_DAT = 10;
const byte PIN_PS2_CLK = 11;

const byte PIN_BUTTONPRESS = A0;
const byte PIN_HAVECONTROLLER = A1;

const char buttonSelectName[] PROGMEM = "Select";
const char buttonL3Name[] PROGMEM = "L3";
const char buttonR3Name[] PROGMEM = "R3";
const char buttonStartName[] PROGMEM = "Start";
const char buttonUpName[] PROGMEM = "Up";
const char buttonRightName[] PROGMEM = "Right";
const char buttonDownName[] PROGMEM = "Down";
const char buttonLeftName[] PROGMEM = "Left";
const char buttonL2Name[] PROGMEM = "L2";
const char buttonR2Name[] PROGMEM = "R2";
const char buttonL1Name[] PROGMEM = "L1";
const char buttonR1Name[] PROGMEM = "R1";
const char buttonTriangleName[] PROGMEM = "Triangle";
const char buttonCircleName[] PROGMEM = "Circle";
const char buttonCrossName[] PROGMEM = "Cross";
const char buttonSquareName[] PROGMEM = "Square";

const char* const psxButtonNames[PSX_BUTTONS_NO] PROGMEM = {
buttonSelectName,
buttonL3Name,
buttonR3Name,
buttonStartName,
buttonUpName,
buttonRightName,
buttonDownName,
buttonLeftName,
buttonL2Name,
buttonR2Name,
buttonL1Name,
buttonR1Name,
buttonTriangleName,
buttonCircleName,
buttonCrossName,
buttonSquareName
};

byte psxButtonToIndex (PsxButtons psxButtons) {
byte i;

for (i = 0; i < PSX_BUTTONS_NO; ++i) {
	if (psxButtons & 0x01) {
		break;
	}

	psxButtons >>= 1U;
}

return i;

}

FlashStr getButtonName (PsxButtons psxButton) {
FlashStr ret = F("");

byte b = psxButtonToIndex (psxButton);
if (b < PSX_BUTTONS_NO) {
	PGM_BYTES_P bName = reinterpret_cast<PGM_BYTES_P> (pgm_read_ptr (&(psxButtonNames[b])));
	ret = PSTR_TO_F (bName);
}

return ret;

}

void dumpButtons (PsxButtons psxButtons) {
static PsxButtons lastB = 0;

if (psxButtons != lastB) {
	lastB = psxButtons;     // Save it before we alter it
	
	Serial.print (F("Pressed: "));

	for (byte i = 0; i < PSX_BUTTONS_NO; ++i) {
		byte b = psxButtonToIndex (psxButtons);
		if (b < PSX_BUTTONS_NO) {
			PGM_BYTES_P bName = reinterpret_cast<PGM_BYTES_P> (pgm_read_ptr (&(psxButtonNames[b])));
			Serial.print (PSTR_TO_F (bName));
		}

		psxButtons &= ~(1 << b);

		if (psxButtons != 0) {
			Serial.print (F(", "));
		}
	}

	Serial.println ();
}

}

void dumpAnalog (const char *str, const byte x, const byte y) {
Serial.print (str);
Serial.print (F(" analog: x = "));
Serial.print (x);
Serial.print (F(", y = "));
Serial.println (y);
}

const char ctrlTypeUnknown[] PROGMEM = "Unknown";
const char ctrlTypeDualShock[] PROGMEM = "Dual Shock";
const char ctrlTypeDsWireless[] PROGMEM = "Dual Shock Wireless";
const char ctrlTypeGuitHero[] PROGMEM = "Guitar Hero";
const char ctrlTypeOutOfBounds[] PROGMEM = "(Out of bounds)";

const char* const controllerTypeStrings[PSCTRL_MAX + 1] PROGMEM = {
ctrlTypeUnknown,
ctrlTypeDualShock,
ctrlTypeDsWireless,
ctrlTypeGuitHero,
ctrlTypeOutOfBounds
};

PsxControllerBitBang<PIN_PS2_ATT, PIN_PS2_CMD, PIN_PS2_DAT, PIN_PS2_CLK> psx;

boolean haveController = false;

void setup () {
fastPinMode (PIN_BUTTONPRESS, OUTPUT);
fastPinMode (PIN_HAVECONTROLLER, OUTPUT);

delay (300);

Serial.begin (115200);
Serial.println (F("Ready!"));

}

void loop () {
static byte slx, sly, srx, sry;

fastDigitalWrite (PIN_HAVECONTROLLER, haveController);

if (!haveController) {
	if (psx.begin ()) {
		Serial.println (F("Controller found!"));
		delay (300);
		if (!psx.enterConfigMode ()) {
			Serial.println (F("Cannot enter config mode"));
		} else {
			PsxControllerType ctype = psx.getControllerType ();
			PGM_BYTES_P cname = reinterpret_cast<PGM_BYTES_P> (pgm_read_ptr (&(controllerTypeStrings[ctype < PSCTRL_MAX ? static_cast<byte> (ctype) : PSCTRL_MAX])));
			Serial.print (F("Controller Type is: "));
			Serial.println (PSTR_TO_F (cname));

			if (!psx.enableAnalogSticks ()) {
				Serial.println (F("Cannot enable analog sticks"));
			}
			
			//~ if (!psx.setAnalogMode (false)) {
				//~ Serial.println (F("Cannot disable analog mode"));
			//~ }
			//~ delay (10);
			
			if (!psx.enableAnalogButtons ()) {
				Serial.println (F("Cannot enable analog buttons"));
			}
			
			if (!psx.exitConfigMode ()) {
				Serial.println (F("Cannot exit config mode"));
			}
		}
		
		haveController = true;
	}
} else {
	if (!psx.read ()) {
		Serial.println (F("Controller lost :("));
		haveController = false;
	} else {
		fastDigitalWrite (PIN_BUTTONPRESS, !!psx.getButtonWord ());
		dumpButtons (psx.getButtonWord ());

		byte lx, ly;
		psx.getLeftAnalog (lx, ly);
		if (lx != slx || ly != sly) {
			dumpAnalog ("Left", lx, ly);
			slx = lx;
			sly = ly;
		}

		byte rx, ry;
		psx.getRightAnalog (rx, ry);
		if (rx != srx || ry != sry) {
			dumpAnalog ("Right", rx, ry);
			srx = rx;
			sry = ry;
		}
	}
}


delay (1000 / 60);

}

and this is the serial monitor
Controller lost :frowning:
Controller found!
Cannot enter config mode
Controller lost :frowning:
Controller found!
Cannot enter config mode
Controller lost :frowning:
Controller found!

Hello, do yourself a favour and please read How to get the best out of this forum and modify your post accordingly (including code tags and necessary documentation for your ask).

Look at a bad connection or weak batteries.

Some reading...

More reading...

#include <DigitalIO.h> // <-- download with IDE
#include <PsxControllerBitBang.h> // <-- https://github.com/SukkoPera/PsxNewLib

#include <avr/pgmspace.h>
typedef const __FlashStringHelper * FlashStr;
typedef const byte* PGM_BYTES_P;
#define PSTR_TO_F(s) reinterpret_cast<const __FlashStringHelper *> (s)

const byte PIN_PS2_ATT = 13;
const byte PIN_PS2_CMD = 12;
const byte PIN_PS2_DAT = 10;
const byte PIN_PS2_CLK = 11;

const byte PIN_BUTTONPRESS = A0;
const byte PIN_HAVECONTROLLER = A1;

const char buttonSelectName[] PROGMEM = "Select";
const char buttonL3Name[] PROGMEM = "L3";
const char buttonR3Name[] PROGMEM = "R3";
const char buttonStartName[] PROGMEM = "Start";
const char buttonUpName[] PROGMEM = "Up";
const char buttonRightName[] PROGMEM = "Right";
const char buttonDownName[] PROGMEM = "Down";
const char buttonLeftName[] PROGMEM = "Left";
const char buttonL2Name[] PROGMEM = "L2";
const char buttonR2Name[] PROGMEM = "R2";
const char buttonL1Name[] PROGMEM = "L1";
const char buttonR1Name[] PROGMEM = "R1";
const char buttonTriangleName[] PROGMEM = "Triangle";
const char buttonCircleName[] PROGMEM = "Circle";
const char buttonCrossName[] PROGMEM = "Cross";
const char buttonSquareName[] PROGMEM = "Square";

const char* const psxButtonNames[PSX_BUTTONS_NO] PROGMEM = {
  buttonSelectName,
  buttonL3Name,
  buttonR3Name,
  buttonStartName,
  buttonUpName,
  buttonRightName,
  buttonDownName,
  buttonLeftName,
  buttonL2Name,
  buttonR2Name,
  buttonL1Name,
  buttonR1Name,
  buttonTriangleName,
  buttonCircleName,
  buttonCrossName,
  buttonSquareName
};

byte psxButtonToIndex (PsxButtons psxButtons) {
  byte i;
  for (i = 0; i < PSX_BUTTONS_NO; ++i) {
    if (psxButtons & 0x01) {
      break;
    }
    psxButtons >>= 1U;
  }
  return i;
}

FlashStr getButtonName (PsxButtons psxButton) {
  FlashStr ret = F("");
  byte b = psxButtonToIndex (psxButton);
  if (b < PSX_BUTTONS_NO) {
    PGM_BYTES_P bName = reinterpret_cast<PGM_BYTES_P> (pgm_read_ptr (&(psxButtonNames[b])));
    ret = PSTR_TO_F (bName);
  }
  return ret;
}

void dumpButtons (PsxButtons psxButtons) {
  static PsxButtons lastB = 0;
  if (psxButtons != lastB) {
    lastB = psxButtons;     // Save it before we alter it
    Serial.print (F("Pressed: "));
    for (byte i = 0; i < PSX_BUTTONS_NO; ++i) {
      byte b = psxButtonToIndex (psxButtons);
      if (b < PSX_BUTTONS_NO) {
        PGM_BYTES_P bName = reinterpret_cast<PGM_BYTES_P> (pgm_read_ptr (&(psxButtonNames[b])));
        Serial.print (PSTR_TO_F (bName));
      }
      psxButtons &= ~(1 << b);
      if (psxButtons != 0) {
        Serial.print (F(", "));
      }
    }
    Serial.println ();
  }
}

void dumpAnalog (const char *str, const byte x, const byte y) {
  Serial.print (str);
  Serial.print (F(" analog: x = "));
  Serial.print (x);
  Serial.print (F(", y = "));
  Serial.println (y);
}

const char ctrlTypeUnknown[] PROGMEM = "Unknown";
const char ctrlTypeDualShock[] PROGMEM = "Dual Shock";
const char ctrlTypeDsWireless[] PROGMEM = "Dual Shock Wireless";
const char ctrlTypeGuitHero[] PROGMEM = "Guitar Hero";
const char ctrlTypeOutOfBounds[] PROGMEM = "(Out of bounds)";

const char* const controllerTypeStrings[PSCTRL_MAX + 1] PROGMEM = {
  ctrlTypeUnknown,
  ctrlTypeDualShock,
  ctrlTypeDsWireless,
  ctrlTypeGuitHero,
  ctrlTypeOutOfBounds
};

PsxControllerBitBang<PIN_PS2_ATT, PIN_PS2_CMD, PIN_PS2_DAT, PIN_PS2_CLK> psx;

boolean haveController = false;

void setup () {
  fastPinMode (PIN_BUTTONPRESS, OUTPUT);
  fastPinMode (PIN_HAVECONTROLLER, OUTPUT);

  delay (300);

  Serial.begin (115200);
  Serial.println (F("Ready!"));

}

void loop () {
  static byte slx, sly, srx, sry;

  fastDigitalWrite (PIN_HAVECONTROLLER, haveController);

  if (!haveController) {
    if (psx.begin ()) {
      Serial.println (F("Controller found!"));
      delay (300);
      if (!psx.enterConfigMode ()) {
        Serial.println (F("Cannot enter config mode"));
      } else {
        PsxControllerType ctype = psx.getControllerType ();
        PGM_BYTES_P cname = reinterpret_cast<PGM_BYTES_P> (pgm_read_ptr (&(controllerTypeStrings[ctype < PSCTRL_MAX ? static_cast<byte> (ctype) : PSCTRL_MAX])));
        Serial.print (F("Controller Type is: "));
        Serial.println (PSTR_TO_F (cname));

        if (!psx.enableAnalogSticks ()) {
          Serial.println (F("Cannot enable analog sticks"));
        }

        //~ if (!psx.setAnalogMode (false)) {
        //~ Serial.println (F("Cannot disable analog mode"));
        //~ }
        //~ delay (10);

        if (!psx.enableAnalogButtons ()) {
          Serial.println (F("Cannot enable analog buttons"));
        }

        if (!psx.exitConfigMode ()) {
          Serial.println (F("Cannot exit config mode"));
        }
      }

      haveController = true;
    }
  } else {
    if (!psx.read ()) {
      Serial.println (F("Controller lost :("));
      haveController = false;
    } else {
      fastDigitalWrite (PIN_BUTTONPRESS, !!psx.getButtonWord ());
      dumpButtons (psx.getButtonWord ());

      byte lx, ly;
      psx.getLeftAnalog (lx, ly);
      if (lx != slx || ly != sly) {
        dumpAnalog ("Left", lx, ly);
        slx = lx;
        sly = ly;
      }

      byte rx, ry;
      psx.getRightAnalog (rx, ry);
      if (rx != srx || ry != sry) {
        dumpAnalog ("Right", rx, ry);
        srx = rx;
        sry = ry;
      }
    }
  }
  delay (1000 / 60);
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.