I'am having problem while using Stepper Motor with PS2 controller

Hi, it is my first time in this forum. I'm sorry if choose wrong category. I am working on arduino project which includes Arduino UNO, Nema 23, TB6600 driver and PS2 Controller. When I try to rotate steppers they don't rotate because of ps.read_gamepad() line, it is blocking loop cycle. What can I do?
It is my code:

//Libraries-----------------
#include <PS2X_lib.h>
#include <AccelStepper.h>
#include <MultiStepper.h>
//--------------------------


//Objects of STEPPER MOTORS---------
AccelStepper stepper1(1, 9, 10);
AccelStepper stepper2(1, 12, 13);
//----------------------------------


//Object of PS2 Controller-
PS2X ps;
//-------------------------

//Flags----
bool isMotorsActivated = false;
//-----------

void setup() {

  //Serial monitor-----
  Serial.begin(9600);
  //-------------------

  //Setup of the First Stepper motor----
  pinMode(8, OUTPUT);
  digitalWrite(8, HIGH);
  stepper1.setMaxSpeed(1000);
  stepper1.setSpeed(0);
  //-------------------------------------

  //stepper2
  pinMode(11, OUTPUT);
  digitalWrite(11, HIGH);
  stepper2.setMaxSpeed(1000);
  stepper2.setSpeed(0);
  //-----------------------------------

  //Setup of PS2 Controller---------------------------------------------------------------
  int error = ps.config_gamepad(4, 3, 5, 2, true, true);
  if (error == 0) {
    Serial.println("Found Controller, configured successful");
  }

  else if (error == 1)
    Serial.println("No controller found, check wiring or reset the Arduino");

  else if (error == 2)
    Serial.println("Controller found but not accepting commands");

  else if (error == 3)
    Serial.println("Controller refusing to enter Pressures mode, may not support it.");
  //---------------------------------------------------------------------------------------

}

void loop() {

  ps.read_gamepad();
  
  int Left_y = map(ps.Analog(PSS_LY), 0, 255, 1000, -1000);
  
  if (Left_y != 4)
  {
    stepper1.setSpeed(Left_y);
    stepper2.setSpeed(Left_y);
    stepper1.runSpeed();
    stepper2.runSpeed();
  }


  //Serial.println(Left_y);

  //Activate and De-Activate motors with GREEN------------
  if (isMotorsActivated)
  {
    if (ps.ButtonPressed(PSB_GREEN))
    {
      isMotorsActivated = false;
      Serial.println("Motors de-activated");
      digitalWrite(8, HIGH);
      digitalWrite(11, HIGH);
    }
  }
  else
  {
    if (ps.ButtonPressed(PSB_GREEN) && !isMotorsActivated)
    {
      isMotorsActivated = true;
      Serial.println("Motors activated");
      digitalWrite(8, LOW);
      digitalWrite(11, LOW);
    }
  }
  //------------------------------------------------------------

  delay(50);

}

Welcome to the forum and thanks for using code tags on your first post.

Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advise on) your project.

1 Like

Does the stepper code run if You comment out that line?
Use serial monitor and serial.print to debug the code, sending messages from strategic places in the code. "I am here", "I am there", maybe some critical variable data...

Yes, the stepper part works when I comment it out, but this time I cant get signals from controller(

What do think of? Confusing.

Study the ps library, how it can be used. I don't know it.
It looks like a "wait until action" but You need a "check for action".

1 Like

This library has only one function to get signal read_gamepad() and i cant find another library :frowning:

there is even one in the library-manager

best regards Stefan

So test the reading the controller on its own by simply reading the controller and print the read values to the serial monitor.

Here is a testcode for doing this

// MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START * 

// a detailed explanation how these macros work is given in this tutorial
// https://forum.arduino.cc/t/comfortable-serial-debug-output-short-to-write-fixed-text-name-and-content-of-any-variable-code-example/888298

#define dbg(myFixedText, variableName) \
  Serial.print( F(#myFixedText " "  #variableName"=") ); \
  Serial.println(variableName);
// usage: dbg("1:my fixed text",myVariable);
// myVariable can be any variable or expression that is defined in scope

#define dbgi(myFixedText, variableName,timeInterval) \
  do { \
    static unsigned long intervalStartTime; \
    if ( millis() - intervalStartTime >= timeInterval ){ \
      intervalStartTime = millis(); \
      Serial.print( F(#myFixedText " "  #variableName"=") ); \
      Serial.println(variableName); \
    } \
  } while (false);
// usage: dbgi("2:my fixed text",myVariable,1000);
// myVariable can be any variable or expression that is defined in scope
// third parameter is the time in milliseconds that must pass by until the next time a 
// Serial.print is executed
// end of macros dbg and dbgi
// MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * 


#include <PS2X_lib.h>

//Object of PS2 Controller-
PS2X ps;
//-------------------------

//Flags----
bool isMotorsActivated = false;
//-----------

void PrintFileNameDateTime() {
  Serial.println( F("Code running comes from file ") );
  Serial.println( F(__FILE__) );
  Serial.print( F("  compiled ") );
  Serial.print( F(__DATE__) );
  Serial.print( F(" ") );
  Serial.println( F(__TIME__) );  
}


// easy to use helper-function for non-blocking timing
boolean TimePeriodIsOver (unsigned long &startOfPeriod, unsigned long TimePeriod) {
  unsigned long currentMillis  = millis();
  if ( currentMillis - startOfPeriod >= TimePeriod ) {
    // more time than TimePeriod has elapsed since last time if-condition was true
    startOfPeriod = currentMillis; // a new period starts right here so set new starttime
    return true;
  }
  else return false;            // actual TimePeriod is NOT yet over
}

unsigned long MyTestTimer =  0;                   // Timer-variables MUST be of type unsigned long
const byte    OnBoard_LED = 13;


void BlinkHeartBeatLED(int IO_Pin, int BlinkPeriod) {
  static unsigned long MyBlinkTimer;
  pinMode(IO_Pin, OUTPUT);
  
  if ( TimePeriodIsOver(MyBlinkTimer,BlinkPeriod) ) {
    digitalWrite(IO_Pin,!digitalRead(IO_Pin) ); 
  }
}


void setup() {
  Serial.begin(115200);
  Serial.println("Setup-Start");
  PrintFileNameDateTime();

  //Setup of PS2 Controller---------------------------------------------------------------
  int error = ps.config_gamepad(4, 3, 5, 2, true, true);
  if (error == 0) {
    Serial.println("Found Controller, configured successful");
  }

  else if (error == 1)
    Serial.println("No controller found, check wiring or reset the Arduino");

  else if (error == 2)
    Serial.println("Controller found but not accepting commands");

  else if (error == 3)
    Serial.println("Controller refusing to enter Pressures mode, may not support it.");
  //---------------------------------------------------------------------------------------

 
}

unsigned long TimeStampBeforeRead;
unsigned long TimeStampAfterRead;

void loop() {
  BlinkHeartBeatLED(OnBoard_LED,100);

  TimeStampBeforeRead = millis();
  ps.read_gamepad();
  TimeStampAfterRead = millis();
  
  int Left_y = map(ps.Analog(PSS_LY), 0, 255, 1000, -1000);

  dbg("1:",Left_y);
  dbg("time",TimeStampAfterRead - TimeStampBeforeRead);
  
}

best regards Stefan

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