Controls for a project


I have a project that I am working on, I know very little about arduino, and any guidance or help will be greatly appreciated.

Here is the background on the project I am undertaking. I have designed and built a 1/9 scale hydraulic excavator, now the mechanical and fabrication part was quite easy, but when it comes to programming I am very new and lost. I am more than willing to learn how to program I just don't know where to start. so here is what I have to work with and want to accomplish. I have purchased a botboarduino, and lynxmotions playstation 2 controller. I have 3 motor controllers currently and 3 on order. There are 2 drive motors for the tracks and 1 rotational motor all 3 independently controlled and need to go both forward and reverse. The 3 motors on the 3 pumps will only need to run forward, but they must run independent of one another. Each pump will be paired up with a servo that controls the distributer valve, each servo only needs to move about 10 degrees off centre in either direction to shift my oil direction accordingly. This is the tricky part, where I was told arduino would shine; when I send a signal from the PS2 controller analog stick to move my main lift rams, pump 1 will turn on, at the same time servo 1 will swing 10 degrees off centre to the right ( or left ), both the pump and servo will be running as long as I hold the analog stick, when I take my finger off the control and the signal stops both the pump and servo will then stop. OR the servo could be moved just the 10 degrees when first given signal, then stop trying to move so long as the control signal isn't changing direction, and the pump can keep running until the analog stick is put back to neutral. when I move the analog stick in the opposite direction the servo1 will move left ( or right) to direct oil in the opposite direction, and pump1 will fire up again.

I don't know how to accomplish this, arduino was not part of my vocabulary 2 weeks ago, I know very little about it but am willing to learn. I have uploaded the PS2 program to my botboarduino. I do not know or yet understand how to take a control from the remote and put it in code form to activate a response or action from the arduino board to move a servo or how much to move it, or a motor. The how to guides I have been reading are hard to understand, and very few have any direct relation to PS2 controller directing the arduino boards. I am more of a hands on, show me the path type of learner, if I can see how a command works and why it works.

Thanks for your time and any advice or guidance.

Here is a code to get you started, but you need to download the library in the Arduino Playground, link HERE.

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

PS2X ps2x; // create PS2 Controller Class

int error = 0; 
byte type = 0;
byte vibrate = 0;

int speed1 =128, speed2 = 128;

byte M1L = 5;// PWM
byte M1R = 3;// PWM
byte M2L = 6;// PWM
byte M2R = 9;// PWM

void setup(){

  //**************PAY ATTENTION*************
  pinMode(M1L, OUTPUT);                                // Establishes LEDPin as an output so the LED can be seen
  pinMode(M1R, OUTPUT);
  pinMode(M2L, OUTPUT);                                // Establishes LEDPin as an output so the LED can be seen
  pinMode(M2R, OUTPUT);
  error = ps2x.config_gamepad(13,11,10,12, true, true);   //setup pins and settings:  GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error

  if(error == 0)
    Serial.println("Controller found! You may now send commands");

  else if(error == 1)
    Serial.println("No controller found, check wiring, see readme.txt to enable debug. visit for troubleshooting tips");

  else if(error == 2)
    Serial.println("Controller found but not accepting commands. see readme.txt to enable debug. Visit for troubleshooting tips");

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

  type = ps2x.readType(); 
  case 0:
    Serial.println("Unknown Controller type");
  case 1:
    Serial.println("DualShock Controller Found");

void loop()
  if(error == 1) //skip loop if no controller found

  else { //DualShock Controller

    ps2x.read_gamepad(false, vibrate);          //read controller and set large motor to spin at 'vibrate' speed

      // rotate turret left
    else if(ps2x.Button(PSB_R1))
      // rotate turret right
      // do nothing

    if(ps2x.Analog(PSS_LY) >= 136 && ps2x.Analog(PSS_LY) <= 255)//real center value is 128, but 140 is needed because controller is HIGHLY sensitive
      speed1 = map(ps2x.Analog(PSS_LY),136 , 255, 0 , 255);
      analogWrite(M1L, speed1);
      digitalWrite(M1R, LOW);
    else if(ps2x.Analog(PSS_LY) >= 0 && ps2x.Analog(PSS_LY) <= 120) //Same as above
      speed1 = map(ps2x.Analog(PSS_LY),0 ,120 , 255 , 0);// create a set range for values and set output values respectively
      digitalWrite(M1L, LOW);
      analogWrite(M1R, speed1);
      digitalWrite(M1L, LOW);// all off
      digitalWrite(M1R, LOW);
    //--------------------Right side motor-----------------------  
    if(ps2x.Analog(PSS_RY) >= 136 && ps2x.Analog(PSS_RY) <= 255)
      speed2 = map(ps2x.Analog(PSS_RY),136 , 255, 0 , 255);
      analogWrite(M2L, speed2);
      digitalWrite(M2R, LOW);

    else if(ps2x.Analog(PSS_RY) >= 0 && ps2x.Analog(PSS_RY) <= 120)
      speed2 = map(ps2x.Analog(PSS_RY),0 ,120 , 255 , 0);
      digitalWrite(M2L, LOW);
      analogWrite(M2R, speed2);

      digitalWrite(M2L, LOW);
      digitalWrite(M2R, LOW);

  delay(50); // not needed, but give the robot some time to finish inputted commands   

The trick is to break the project into several small pieces that you can develop and test easily. Later, it will be easy to integrate them into a single sketch.

@HazardMind's sketch seems to use a PS2 library which will interpret inputs from the controller.

I suggest you start with a simple sketch that reads the input from the PS2 and shows the value on the SerialMonitor. That way you can easily learn what happens when you do things with the PS2 controller.

Then you can extend that sketch so that it controls a servo or two. Servos are easier to control than motors as you only need to connect the signal and ground pins to the Arduino. The servos and motors must have their own power supply as the Arduino can't supply enough current.

Then modify the sketch to control one of the the drive motors.

etc etc.

As you accomplish each step be sure to keep a copy of the working code so you can go back to it and start again if your modifications don't work and it gets all messed up.

In principle what you want to do is perfectly achievable - but if you look at it as a single task it will be impossible to develop and debug.

Also, it is much easier to get help here if your question only involves a small piece of code.

Have fun.

Can you post a photo of the excavator?


Thank you for the download information

I installed the library and uploaded the code you supplied, the PS2 receiver on the board lights up when the PS2 controller is turned on so it must be working. I have looked at other peoples PS2 code and it is over my head, turning on a LED or telling a servo to swing back and forth with a delay is my level of understanding right now, but adding the PS2 controller and using it to control the board is a big step up for me. I know I picked a very hard project for my first go at arduino, its a lot of learning just to make a big RC toy really.

I agree completely with breaking it down into small pieces. I will looking for such a program to start small.

I have a beginner question, if I find a program to read inputs from the PS2 controller do I add it in the same code “box” or load it as a new code ?
In general I will always be adding anything new to my existing code ? not making multiple codes

here is a couple pictures, its 4 feet long when stretched out, the tracks are 24 inches long.

Cool pictures.

I'm not sure what you mean by "the same code box".

If you mean the editing window in the Arduino IDE then each separate sketch should be separate. However there will be times when you need to add to an existing sketch. So the answer is NO/YES.

Play around with some of the examples that come with the Arduino IDE and learn how to save your own versions of a sketch. Even if you are adding/extending a sketch it is vital to save the current working version so that you can go back to it.

At this early stage you will probably find it useful to make several separate sketches to learn about different parts of your project before you start to combine the ideas.

You will have to find out what methods the PS2 library accepts - such as ps2x.read_gamepad(false, vibrate) and ps2x.Button(PSB_L1) (from @HazardMind's reply).


The library comes with an example sketch that shows you all the possibilities the library has to offer. Try it out.

Thank you both for your input it has been helpful

as for the "same code box" yes I was referring to the editing window, I am still getting used to proper terms. what I was asking, is as I learn to write code that is of use for the project, do I add it to the main PS2x editing window, or place it in a new editing window to load it to the board ? I would hate to overwrite the PS2x program with some simple code that is meant to work with the PS2x program. I guess I was just wanting to get clarification on if more than one program can be loaded onto the board at once.

Now the next thing I can't get to function, maybe I missed something along the way. I loaded the PS2x from the website and the code has an issue "core.a(main.cpp.o): In function main': D:\Arduino\hardware\arduino\cores\arduino/main.cpp:11: undefined reference tosetup' D:\Arduino\hardware\arduino\cores\arduino/main.cpp:14: undefined reference to `loop'" This appears when I try to load the program. I have tried the debug file that comes with the download, and nothing has changed.

If I load the code supplied by HazardsMind it loads without error, but when I open the Serial monitor I get the flowing message " No controller found, check wiring, see readme.txt to enable debug. visit for troubleshooting tips Unknown Controller type"

I don't know what is happening in either situation, If there is something I am missing or doing wrong ? is my playstation controller or receiver not working properly ? I did set the baud rate to 57600 to match what the programs both call for.

If anyone has a suggestion of what to do I would gladly take it, as getting the controller to talk to the board and seeing the communication on the serial monitor is the first major step I think I will need to take toward understanding how to get these two key pieces of the project working together. Keep in mind I am not experienced in arduino, this is all very new to me.

Thanks again for your time

How is everything wired, can you provide pictures?

I have the wiring setup according to Lynxmotions website, since it is their controller and receiver, going to the botboarduino.

I will attach a photo, the receiver does light up on one LED, with the second LED flashing. When I turn the controller power on the second LED goes on. I am just using the power from the USB plug to run the board and controller.

What voltage are you supplying to it, 3.3 or 5? My wireless based only works at 3.3 so maybe yours does too.


I checked the voltage and there is a drop down built into the receiver board, it take the 5 V supplied from the arduino board and drops it to 3.3 in the receiver board. do you have a picture of how your board and receiver are wired together, after looking at the lynxmotion page I noticed in the literature they have this setup but .... Pin 6 DAT , 5V PWR, GND Pin 7 CMD Pin 8 ATT Pin 9 CLK wiring diagram has different arrangement Pin 6 DAT Pin 7 CMD Pin 8 ATT Pin 9 CLK, 5V PWR, GND Does moving the ground and PWR inputs really effect anything ?

I have tried wiring the board both ways that are provided on the webpage, always being careful to turn off the power ( usb cable ) and to have everything moved to new plugs before repowering the board. Both ways of wiring turn the same result, the receiver board has 1 LED on solid and 1 blinking, once I turn on the PS2 controller both LED's go solid. I have loaded each set of code I have one at a time and tried the serial monitor with no results. I am at a loss so far, after going thru the code on the PS2x program I did see that the printout in the serial monitor that says no controller detected etc. is caused by : else if(error == 1) . I don't know if this is any help, I hope it is.

Green wire is pin 13. Red = pin 10

Well I have gone over the wiring a dozen times to make sure it is all proper, and put new batteries in the PS2 control pad. still getting the same result of error = 0 , which then prints me off the " no controller check everything, call for help" response in the serial monitor. I just don't know enough about arduino to fix the problem. I have spent most my day going thru this forum and trying solutions that were suggested in other posts hoping to get this board and controller to talk. any new ideas or suggestions would be appreciated.

Thanks for your time

This might not help but it's worth a try.

I found out that my module does not like it when 5 volts is supplied, it too gives the same error. However I noticed that my module does not even need 3.3V to work. I could have the V+ wire off and it will still function correctly. If you can and dont break your current one, but if you can, make your own adapter the exact same way I made mine. Buy a PS2 extension cable and make your own. Or try different controllers. Mine is an INTEC and it took me a long while to figure out what my issue was, and it was only when I started to take apart the adapter (remove the V+ wire) did it finally work.

Good morning

Success, after a lot of tinkering, and a lot of help from members of this forum, I have communication between the board and gamepad, and I am in the process of assigning the proper buttons names to appear in the serial monitor. It is a good step in the right direction. Thank you to everyone who has offered their input so far, I am glad to have this forum here to help people like me who didn't know what arduino was 2 weeks ago to now have some idea what I am doing.

Thanks again for your time and input.

How did you finally get it to work?

Well being a real novice at this I never thought to assign the pins on the board to the reciever pins. after much browsing other peoples code, I could see that I had to assign the pins. I assumed the board would know what to do, now I know .