Random command appearing

Hello

I am just starting to learn arduino, and i have a PS2 gamepad from lynxmotion connected to a botboarduino. I am just learning how code works and have been able to get my gamepad to “talk” to my board. I have an issue with my code, or possibly my gamepad. here is my code so far …

#include <PS2X_lib.h>  //for v1.6

PS2X ps2x; // create PS2 Controller Class


int error = 0; 

#define PS2_DAT      6        
#define PS2_CMD      7
#define PS2_SEL      8
#define PS2_CLK      9


void setup(){
  
 Serial.begin(57600);
 delay(5000);  // give time to open monitor
  int error = ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_SEL, PS2_DAT);  // Setup gamepad (clock, command, attention, data) pins
  Serial.print("PS2 Init: ");
  Serial.println(error, DEC);
}

void loop(){
  
  if(error == 1) //skip loop if no controller found
  return; 
  
 ps2x.read_gamepad();          //read controller 
    
 
 // left control pad     
         
     if(ps2x.Button(PSB_PAD_UP))          
       Serial.print("Up held  ");
     if(ps2x.Button(PSB_PAD_RIGHT))
       Serial.print("Right held  ");
     if(ps2x.Button(PSB_PAD_LEFT))
       Serial.print("LEFT held  ");
     if(ps2x.Button(PSB_PAD_DOWN))
       Serial.print("DOWN held  ");
       
  // L1, L2, R1, R2 buttons
    
     if(ps2x.Button(PSB_L1))
         Serial.println("L1 pressed");
        if(ps2x.Button(PSB_R1))
         Serial.println("R1 pressed");
        if(ps2x.Button(PSB_L2))
         Serial.println("L2 pressed");
        if(ps2x.Button(PSB_R2))
         Serial.println("R2 pressed");
      
     // circle, square, x, and triangle
    
     if(ps2x.Button(PSB_RED))             
         Serial.println("Circle pressed");
     if(ps2x.Button(PSB_PINK))             
         Serial.println("Square pressed");     
     if(ps2x.Button(PSB_BLUE))            
         Serial.println("X pressed");    
     if(ps2x.Button(PSB_GREEN))           
         Serial.println("Triangle pressed");
         

 delay(500);
     
}

The issue I have is I keep getting the message " Right held" every cycle of the loop, if I hold down any colored buttons ( circle, square, triangle, x ), R1, R2, L1, or L2 they are displayed, but once I let go of other buttons the “right held” comes back. If I just press and release the buttons the proper text appears, but on the next loop cycle “right held” is back. If I hold down or push on my control pad in any direction I get the proper text back, but with “right held” attached to it. Does this mean my controller has a problem ? like the control pad right side is broken or somehow stuck in the “on” position
any advice or help will be greatly appreciated

I don't know how that library works but IIRC PS2 controllers are fussy about how often they should be read, which I think is something like 50 times a second. Try changing the delay to 20 or various other values. Also you really should be checking the ACK pin too since its a 5-wire protocol.

 delay(5000);  // give time to open monitor

Rubbish. Opening the serial monitor resets the Arduino. It isn't necessary to wait for the serial monitor to be opened.

  Serial.println(error, DEC);

Just to be sure that the serial monitor doesn't print in base 7? The default is to output numbers in base 10. Specifying the default value just screams clueless. You might want your code to shut up about that.

Why does your code run all over the place?

Can't you indent consistently? Or, use Tools + Auto Format?

MarkT I changed the delay to 20, and tried running every multiple of 5 from 5 to 100 in the delay, the "right held" still appears it averages every 10th loop. I held down just the circle button , and on average every 10th line the "right held" would appear, sometimes less than 10 lines, sometimes more. I even put delay to 1000, and timed how often it would appear without pressing anything, on average it was every 10 seconds. I did take apart the gamepad and check and clean it, there was no debris found that would be causing the gamepad to think i was pressing the right button on the controls. I checked the ACK pin, it appears to be connected to nothing on the reciever, if i was to solder the breaks in its path it would tie into the 7.2 volt pin. I don't know what I should be looking for ? I can post a picture of the reciever if that would help

PaulS I removed the opening delay, and used the auto format. I am NEW at arduino, so I don't know a lot, I am trying to learn. I have posted here to gain some experience. In the future I will auto format before posting.

So far the errant "right held" still pops into my serial monitor, if anyone has an idea on what I can do to fix the problem any help would be appreciated

Hello again.

Ok what happens if you hold the left Dpad button and release, do you still get the same message? By any chance the right Dpad button feel looser then the rest? It is possibly the right Dpad touch pad (the rubber part inside) is stuck, in which case you would need to open up the controller to find out.

If I hold the left Dpad and release the "right" comes back, or puts itself in on average at the 10th line. The controller Dpad feels normal, I took it apart again this morning and it looked fine. I checked the rubber pads and they are all sitting in place, there was some resin from factory soldering but I removed it carefully with a toothpick, the resin was under the colored buttons ( the ones that work just fine) Also when I add the lines if code to show when my start and select buttons are used they give off errant codes just like the Dpad . with the gamepad controller ON my serial monitor comes to life with " select held " and "right held", but the print out is at a slower pace, every 20th or so line ,when you hold down some other button, not every 10th. if I let the gamepad sit idle, the errant responces come at a longer interval between popping up on screen than they did before I added the start and select section of code. I started small and just put in the colored buttons into the code, they worked fine, then I added the L1, L2, R1, R2 buttons they worked fine aswell, then I added the directional pad, is works fine in responding to what I am pressing other than the "right held" issue. Now with "select" added in, and errant responces from them I am thinking is might be a reciever or board issue. Any advice or ideas on how to test the reciever/board, or fix the problem I have would be greatly appreciated. Thank you for your time