Making a joystick control for two servos to hold position

Sure I would love to.
It is not my code. Kudos to Robin2 fro providing me the code.

#include <Servo.h>

#define mainArm 0
#define jib 1
#define bucket 2
#define slew 3

Servo servo[4];      //code used for attaching upto 4 servos

byte angle[4] = {90, 90, 90, 90};       // middle point for servo angle

byte potPin[4] = {A0, A1, A2, A3};  // input pins to attach your potentiometers
byte servoPin[4] = {7, 6, 10, 9};    // input pins to attach servos

void setup() {

  Serial.begin(9600);
  Serial.println("Starting DiggerCode.ino");


  for (byte n = 0; n < 4; n++) {
    servo[n].attach(servoPin[n]);
  }
}

void loop() {
  readPotentiometers();
  moveServos();
  delay(10);
}

void readPotentiometers() {
  int potVal;
  for (byte n = 0; n < 4; n++) {
    potVal = analogRead(potPin[n]);
    
    if (potVal < 200) {         // dead zone for the joystick I used is 200 to 550.
      angle[n] += 1;
      if (angle[n] > 170) {
        angle[n] = 170;
      }
    }
    
    if (potVal > 550) {         // deadzone upper value
      angle[n] -= 1;
      if (angle[n] < 10) {
        angle[n] = 10;
      }
    }

  }
}

void moveServos() {
  for (byte n = 0; n < 4; n++) {
    servo[n].write(angle[n]);
  }
}

Courtesy: Robin2

The old discussion.

http://forum.arduino.cc/index.php/topic,157967.0.html

@Zoomkat's link is not the one I contributed to.

I have now found it (courtesy of Google) Excavator control project

...R

It worked fine for me I just used A2,A3 pins and ~10,~9 "i left the code as is" works Fine on UNO

Hello, im looking for some help here, im new to arduino and programming.I found this thread very useful and also Robins2 code for "Excavator". i just edit code to work like i want - normal movement of servo with joystick, but now i want to add this functionality to my code - holding position of servo arm while joystick get back to neutral. Original code of Robin2 works perfect but i dont know to implement it to my code. Here is my piece or art:

#include <Servo.h>
Servo servo[1];  // create servo object to control a servo
int potPin[1]={A0}; 
int potValue;     
int servodt=10; 
int servoPin[1]={9};
int pushbutton=1;
int servoValue[1]={1472};
void setup() {
 for (byte n = 0; n < 1; n++){
 servo[n].attach(servoPin[n]); 
 }
}
void loop() {
 readPotentiometers();
 moveServos();
 delay(servodt);
}
void readPotentiometers(){
   for (byte n = 0; n < 1; n++){
     potValue = analogRead(potPin[1]);                 
     potValue = map(potValue, 0, 1023, 544, 2400);
     
   if (potValue >= 1372 && potValue <= 1572 ) {           
       servoValue[1] = 1472;
       }
     
   if (potValue <= 1372 ) {
       servoValue[1] = potValue + 100;
       }
 
   if (potValue >= 1572 ) {
       servoValue[1] = potValue - 100;
       }
   
   if (pushbutton = 1 && potValue < 1372) {
       
   }
   }
     
}  
 void moveServos() {
 for (byte n = 0; n < 1; n++) {
   servo[n].writeMicroseconds(servoValue[1]);
 }
}

if (pushbutton = 1 && potValue < 1372) .... here i want to add code for hold position functionality, but i cannot figure it out how.
Can someone help me?

Why don't you use the code from my example and just substitute your values?

...R

PS ... if you have a link to the "excavator" example I would appreciate it as I can't find it.

I think DC motor Position control with joystick - Project Guidance - Arduino Forum is the right one?
currently im at work, i will take a look later to your link and see if i will can manage it to get both functionalitys of servo movings.Thanks for now.

pocukrani:
I think DC motor Position control with joystick - Project Guidance - Arduino Forum is the right one?

Thanks. It's not quite the right one - it seems that guy had a few Threads about the same project, but he is no longer a member of the Forum so I can't get a link to the other ones.

...R

maybe it was Re: Controls for a project - Project Guidance - Arduino Forum

Thanks very much - that's probably the correct one.

I think the newer code in the link in Reply #25 is a better starting point.

...R

Yes i was trying a code from Reply#26 and it works as i want (lets say if button is pressed).
My code previously posted ( Reply#24) also works in different way. Also with those two codes i have two ways of moving servos. Now i neeed help to switch between those two codes.

pocukrani:
Now i neeed help to switch between those two codes.

That does not tell me exactly what you want help with. For example is there some functionality missing from my version?

If this was my project I would simply replace your version of the joystick code with my version and go on from there.

...R

OK, i think i will start again. You posted me 2 links with codes, which one you suggest me to start with?

I make it to work as i want.Here is my code:

#include <Servo.h>

//#define mainArm 0
//#define jib 1
//#define bucket 2
//#define slew 3

Servo servo[4];                                  
int angle[4] = {100, 100, 100, 1472};             

byte potPin[4] = {A0, A1, A2, A3};
byte servoPin[4] = {12, 11, 10, 9};               
                                                   
                                                  
void setup() {

  Serial.begin(9600);
  Serial.println("Starting DiggerCode.ino");

  for (byte n = 0; n < 4; n++) {                  
    servo[n].attach(servoPin[n]);
    servo[n].writeMicroseconds(angle[n]);
  }
}

void loop() {
  readPotentiometers();
  moveServos();
  delay(10);
}

void readPotentiometers() {
  int potVal;
  int button = 1;
  for (byte n = 0; n < 4; n++) {
    potVal = analogRead(potPin[n]);
    potVal = map(potVal, 0, 1023, 544, 2400);
    
    if ( button == 1){
      
      if (potVal > 1572) {
        angle[n] += 10;
        if (angle[n] > 2399) {
          angle[n] = 2400;
        }
      }
   
      if (potVal < 1372) {
        angle[n] -= 10;
        if (angle[n] < 545) {
          angle[n] = 544;
        }
      }
      if (potVal < 1372 && angle[n] > 1572){    //kick off code
        angle[n] = 1472;
        delay(20);
      }
    }
    else if (potVal >= 1372 && potVal <= 1572 ) {           
          angle[n] = 1472;
          }
      
    else if (potVal <= 1372 ) {
          angle[n] = potVal + 100;
          }
  
    else if (potVal >= 1572 ) {
          angle[n] = potVal - 100;
          }
  }
}

void moveServos() {
  for (byte n = 0; n < 4; n++) {
    servo[n].writeMicroseconds(angle[n]);
  }
}

Now im struggling with "kick off" code. it works , but not smooth as i want. Any suggestion about this?

pocukrani:
Now im struggling with "kick off" code. it works , but not smooth as i want. Any suggestion about this?

I don't know what you mean by "kick off" code.

I don't know what you mean by "not smooth" - please describe in detail what it actually does and what you want it to do that is different.

Why have you a delay() in the middle of readPotentiometers() ?

...R

Ok maybe i didnt show you correctly what i want to achieve. Lets say joystick has 3 positions, 1 - 0 - 2: one direction - middle position - second direction. I push it to direction 1 - servo arm moves coresponding to joystick - thats ok. Then i relise joystick to 0 position - servo arm is still in previously position - thats ok. Then i move joystick to opposite direction (2) - servo arm jump fast to middle position - this ia also ok. But there i have problem, when i move joystick to "2" position, servo arm jump to middle position and then
immediately get some more degrees in opposite direction - corresponding to joystick position. Here im trying something with deadband and delay to eliminate this movement of servo arm. Hope you will understand now..

I add some more deadband for joystick, its better now.But im wondering if there is any better solution for this?

if (potVal < 1172) { //instead of 1372
        angle[n] -= 10;
        if (angle[n] < 545) {
          angle[n] = 544;

pocukrani:
Ok maybe i didnt show you correctly what i want to achieve. Lets say joystick has 3 positions, 1 - 0 - 2: one direction - middle position - second direction. I push it to direction 1 - servo arm moves coresponding to joystick - thats ok. Then i relise joystick to 0 position - servo arm is still in previously position - thats ok. Then i move joystick to opposite direction (2) - servo arm jump fast to middle position - this ia also ok. But there i have problem, when i move joystick to "2" position, servo arm jump to middle position and then

And does it behave like that when you use the code from the link I posted in Reply #25 ?

...R