Xbox Controller + Stepper motors

So far not, because I found that when I send ( compile) the program from processing it goes really slow. I am not sure if because I used in my case a stepper motors? What I found difficult right now is to make proper lines for the button option on the joystick. This is starting like for J5 motor ( Dir5 and Pul6 ).I was thinking that using if statement and than implement already the created code for J1 or J3 on the same side of the joystick it should work. But when I press the button it do nothing…
I tried to keep it as much simple as i can at first without using additional libraries and code that i didn’t create by myself and somewhat not corresponding to my knowledge. I am beginner in programing so… not in a hurry to make it work.
However my plan after I finish the first steps of moving the motors assigned to each button and sticks is to create an Array. I am currently study and I do believe it will serve very well for my idea. Make steps ( movements ) store them in Array and repeat.
As soon as I figure out how to repair my program and make it work properly I will post again.
Thank you.

Hello everyone.
I have found my mistake and with very small changes now works great.
I share below the code, however, I am planning to continue expand the program with more functions and options. But for a beginner, I do believe it will help anyone to have a good start.
Thank you.

ps. It was a long quarantine time, so it’s good to have someone to talk to : ) (blush)

int driverPUL1 = 22; // driver slot for Pulses
int driverDIR1 = 24; // driver slot for Directon
int driverPUL2 = 26;
int driverDIR2 = 28;
int driverPUL3 = 30;
int driverDIR3 = 32;
int driverPUL4 = 34;
int driverDIR4 = 36;
int driverPUL5 = 38;
int driverDIR5 = 40;
int driverPUL6 = 42;
int driverDIR6 = 44;
int buttonPos=0;


// Anlog Pins:
//A8=right button
//A9=right Y
//A10=right X
//A11=left button
//A12=left Y
//A13=left X


int pd = 100; //pulse delay

void setup() {

  Serial.begin(9600);

  Serial.println("waiting ....");
  delay(5000);
  Serial.println("start");
  
  pinMode(driverPUL1, OUTPUT); // Driver PULl ON
  pinMode(driverDIR1, OUTPUT); // Driver DIR1 ON

  pinMode(driverPUL2, OUTPUT); // Driver PUL2 ON
  pinMode(driverDIR2, OUTPUT); // Driver DIR2 ON

  pinMode(driverPUL3, OUTPUT); // Driver PUL3 ON
  pinMode(driverDIR3, OUTPUT); // Driver DIR3 ON

  pinMode(driverPUL4, OUTPUT); // Driver PUL4 ON
  pinMode(driverDIR4, OUTPUT); // Driver DIR4 ON

  pinMode(A11, INPUT);
  pinMode(driverPUL5, OUTPUT); // Driver PUL5 ON
  pinMode(driverDIR5, OUTPUT); // Driver DIR5 ON

  pinMode(A11, INPUT);
  pinMode(driverPUL6, OUTPUT); // Driver PUL5 ON
  pinMode(driverDIR6, OUTPUT); // Driver DIR5 ON
 

  

}

void loop() {
  int stickPos; // stick postion
  int steps = 0; // steps
  int dir;    // direction
  int leftButton; // Left Button


  leftButton=analogRead(A11); // reading left button position 0 to 1023

if (leftButton>512) {


  // -----------------------------   RIGHT X -----------------------------------------
  stickPos = analogRead(A10); // reading stick position 0 to 1023

  if (stickPos > 520 || stickPos < 480) steps = (stickPos - 500) / 50; // lover divider will make high speed higher


  //Serial.println(steps);

  if (steps > 0) dir = 1; else {
    steps = -steps;
    dir = 0;
  }

  digitalWrite(driverDIR1, dir);

  for (; steps > 0; steps--) {

    digitalWrite(driverPUL1, HIGH);
    delayMicroseconds(pd);
    digitalWrite(driverPUL1, LOW);
    delayMicroseconds(pd);

  }



  // -----------------------------   RIGHT Y -----------------------------------------
  stickPos = analogRead(A9); // reading stick position 0 to 1023

  if (stickPos > 520 || stickPos < 480) steps = (stickPos - 500) / 50; // lover divider will make high speed higher


  //Serial.println(steps);
  //delay(4);    // larger number will make low speed lower


  if (steps > 0) dir = 1; else {
    steps = -steps;
    dir = 0;
  }

  digitalWrite(driverDIR2, dir);

  for (; steps > 0; steps--) {

    digitalWrite(driverPUL2, HIGH);
    delayMicroseconds(pd);
    digitalWrite(driverPUL2, LOW);
    delayMicroseconds(pd);

  }


  // -----------------------------   LEFT X -----------------------------------------
  stickPos = analogRead(A12); // reading stick position 0 to 1023

  if (stickPos > 520 || stickPos < 480) steps = (stickPos - 500) / 50; // lover divider will make high speed higher


  //Serial.println(steps);
  //delay(4);    // larger number will make low speed lower


  if (steps > 0) dir = 1; else {
    steps = -steps;
    dir = 0;
  }

  digitalWrite(driverDIR3, dir);

  for (; steps > 0; steps--) {

    digitalWrite(driverPUL3, HIGH);
    delayMicroseconds(pd);
    digitalWrite(driverPUL3, LOW);
    delayMicroseconds(pd);

  }

  // -----------------------------   LEFT Y -----------------------------------------
  stickPos = analogRead(A13); // reading stick position 0 to 1023

  if (stickPos > 520 || stickPos < 480) steps = (stickPos - 500) / 50; // lover divider will make high speed higher


  //Serial.println(steps);
  //delay(4);    // larger number will make low speed lower


  if (steps > 0) dir = 1; else {
    steps = -steps;
    dir = 0;
  }

  digitalWrite(driverDIR4, dir);

  for (; steps > 0; steps--) {

    digitalWrite(driverPUL4, HIGH);
    delayMicroseconds(pd);
    digitalWrite(driverPUL4, LOW);
    delayMicroseconds(pd);

  }

} else {

  // -----------------------------   RIGHT X -----------------------------------------
  stickPos = analogRead(A10); // reading stick position 0 to 1023

  if (stickPos > 520 || stickPos < 480) steps = (stickPos - 500) / 50; // lover divider will make high speed higher


  //Serial.println(steps);

  if (steps > 0) dir = 1; else {
    steps = -steps;
    dir = 0;
  }

  digitalWrite(driverDIR6, dir);

  for (; steps > 0; steps--) {

    digitalWrite(driverPUL6, HIGH);
    delayMicroseconds(pd);
    digitalWrite(driverPUL6, LOW);
    delayMicroseconds(pd);

  }



  // -----------------------------   RIGHT Y -----------------------------------------
  stickPos = analogRead(A9); // reading stick position 0 to 1023

  if (stickPos > 520 || stickPos < 480) steps = (stickPos - 500) / 100; // lover divider will make high speed higher


  //Serial.println(steps);
  //delay(4);    // larger number will make low speed lower


  if (steps > 0) dir = 1; else {
    steps = -steps;
    dir = 0;
  }

  digitalWrite(driverDIR5, dir);

  for (; steps > 0; steps--) {

    digitalWrite(driverPUL5, HIGH);
    delayMicroseconds(pd);
    digitalWrite(driverPUL5, LOW);
    delayMicroseconds(pd);

  }
}

delay(4);    // a larger number of delay will make low speed lower


}
2 Likes

nice!!! in these days i will try it by myself. Your project seems cool.

Thank you. The robot arm is great although it was kickstarter project and the guy put so much effort. However it is always not the same as it looks like so I need to change some things…many things:)))
But after all is worth it because even a girl a love machines and tools and it gives me really relaxing time.
Cheers
Taniq

2 Likes

Guys I finally found the right library: vsync for arduino and Processing. Synchronize the variables from Arduino to Processing, also vice versa, even simultaneously, and allows you to drastically shorten the two codes !!!

thats the library

For now i use 17 single variables but i will try directly using an array.
Arduino code:

 /*
  Pulsants/ position on the array
  xdx = 0  
  ydx =  1 
  xsx =  2
  ysx = 3
  
  RT  =  4 
  RB  =  5
  LT  =  6
  LB  =  7
  BACK =  8 
  START =  9 
  butY  =  10
  butX  =  11
  butA  =  12
  butB  =   13
  pulssx =  14
  pulsdx =  15
  freccie = 16
  */

#include <VSync.h>

int Led[]={2,3,4,5,6,7,8,9,10,11}; //only a test for understend if i receive something

     

//  Create a new receiver that can receive up to 17 values.
//  Always put the number of values to sync in the pointy brackets.
ValueReceiver<17> receiver;

//the variables that has to be sync (i dunno if i can use directly an Array, i will try)

int Msg0 ,Msg1 , Msg2 , Msg3 , Msg4, Msg5, Msg6, Msg7, Msg8, Msg9, Msg10, Msg11, Msg12, Msg13, Msg14, Msg15, Msg16; 


void setup() {
  //on start, light up all leds, and set up the pins
  for(int i=0; i< 12; i++)pinMode(Led[i],OUTPUT);
  
  for(int i=0; i<11; i++)digitalWrite(Led[i],HIGH);
  
  // start serial port at 9600 bps:
  Serial.begin(9600);

  receiver.observe(Msg0);
  receiver.observe(Msg1);
  receiver.observe(Msg2);
  receiver.observe(Msg3);
  receiver.observe(Msg4);
  receiver.observe(Msg5);
  receiver.observe(Msg6);
  receiver.observe(Msg7);
  receiver.observe(Msg8);
  receiver.observe(Msg9);
  receiver.observe(Msg10);
  receiver.observe(Msg11);
  receiver.observe(Msg12);
  receiver.observe(Msg13);
  receiver.observe(Msg14);
  receiver.observe(Msg15);
  receiver.observe(Msg16);
  

}
 
 void loop() 
 {
    receiver.sync();
    
    //shut down all leds
    for(int i=0; i<11; i++)digitalWrite(Led[i],LOW);
    //light on the correspondant leds
    if(Msg4 ==1) digitalWrite(Led[1],HIGH);
    if(Msg5 ==1) digitalWrite(Led[2],HIGH);
    if(Msg6 ==1) digitalWrite(Led[3],HIGH);
    if(Msg7 ==1) digitalWrite(Led[4],HIGH);
    if(Msg8 ==1) digitalWrite(Led[5],HIGH);
    if(Msg9 ==1) digitalWrite(Led[6],HIGH);
    if(Msg10 ==1) digitalWrite(Led[7],HIGH);
    if(Msg11 ==1) digitalWrite(Led[8],HIGH);
    if(Msg12 ==1) digitalWrite(Led[9],HIGH);
    if(Msg13 ==1) digitalWrite(Led[0],HIGH);
    
}

and Processing part of the code

import vsync.*;
import processing.serial.*;
import net.java.games.input.*;
import org.gamecontrolplus.*;
import org.gamecontrolplus.gui.*;

ControlDevice cont;
ControlIO control;
Serial port;
ValueSender sender;

public int Msg0, Msg1 , Msg2 , Msg3 , Msg4, Msg5, Msg6, Msg7, Msg8, Msg9, Msg10, Msg11, Msg12, Msg13, Msg14, Msg15, Msg16; //list of variables to sync
        // incoming serial byte



void setup()
{
  size(256, 256);  // Stage size
  
  
  control = ControlIO.getInstance(this);
  cont = control.getMatchedDevice("xboxjoy");
  
//  Hint: "/dev/ttyUSB0" is the serial port on my system. It might have
  //  a different name on yours. 
  //  It should be the same one that is checked under Tools->Serial Port in you Arduino IDE
  //  when uploading Arduino sketches.
  //  Look at http://processing.org/reference/libraries/serial/Serial.html if you still have trouble.
  Serial serial = new Serial(this, Serial.list()[0], 9600);
  
  //  Ininialize the ValueSender with this (to hook it to your sketch)
  //  and the serial interface you want to use.
  sender = new ValueSender(this, serial);
  
  sender.observe("Msg0");
  sender.observe("Msg1");
  sender.observe("Msg2");
  sender.observe("Msg3");
  sender.observe("Msg4");
  sender.observe("Msg5");
  sender.observe("Msg6");
  sender.observe("Msg7");
  sender.observe("Msg8");
  sender.observe("Msg9");
  sender.observe("Msg10");
  sender.observe("Msg11");
  sender.observe("Msg12");
  sender.observe("Msg13");
  sender.observe("Msg14");
  sender.observe("Msg15");
  sender.observe("Msg16");
  
}

public void getUserInput() {
   
  Msg0 = (int)map(cont.getSlider("xdx").getValue(), -1, 1, 1, 500);
  Msg1 = (int)map(cont.getSlider("ydx").getValue(), -1, 1, 1, 500);
  Msg2 = (int)map(cont.getSlider("xsx").getValue(), -1, 1, 1, 500);
  Msg3 = (int)map(cont.getSlider("ysx").getValue(), -1, 1, 1, 500);
  if( cont.getButton("RT").pressed() ) Msg4 = 1 ; 
  if( cont.getButton("LT").pressed() ) Msg5 = 1 ; 
  if( cont.getButton("RB").pressed() ) Msg6 = 1 ;
  if( cont.getButton("LB").pressed() ) Msg7 = 1  ; 
  if( cont.getButton("BACK").pressed() ) Msg8 = 1 ;
  if( cont.getButton("START").pressed() ) Msg9 = 1;
  if( cont.getButton("butX").pressed() ) Msg11 = 1 ;
  if( cont.getButton("butA").pressed() ) Msg12 = 1 ;  
  if( cont.getButton("butB").pressed() ) Msg13 = 1 ;
  if( cont.getButton("pulssx").pressed() ) Msg14 = 1 ;
  if( cont.getButton("pulsdx").pressed() ) Msg15 = 1 ;
  Msg16 = (int)map(cont.getHat("freccie").getValue(), -1, 1, 0, 8);

  
}

void draw()
{
 Msg0=0; Msg1 =0 ; Msg2 =0 ; Msg3 =0 ; Msg4=0 ; Msg5=0 ; Msg6=0 ; Msg7=0 ; Msg8=0 ; Msg9=0 ; Msg10=0 ; Msg11=0 ; Msg12=0 ; Msg13=0 ; Msg14=0 ; Msg15=0 ; Msg16=0 ;
  getUserInput();
}

that’s absolutly amazing!!!

1 Like

Hi @Shiginet, thank you for sharing your project, I am trying to implement your code for an xbox controller to control a stepper motor. However I am unable to get processing to communicate with the Arduino. I was wondering do you have any suggestions to fix this problem?

Thank you.