Go Down

Topic: Making a joystick control for two servos to hold position (Read 14078 times) previous topic - next topic

Robin2

That shouldn't make any difference.

What pin is the Joystick wiper connected to - indeed what pins are all the joystick connection on?

I just tested my code. It works fine. When the pot is in the centre nothing happens - turn it one way and the servo starts moving. Turn it back to centre and the servo stays at its last position. Turn it the other way and the servo reverses.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

msdhillon

I have x axis of axis connected to A0, y axis of joystick connected to A1, servo 1 connected to pin 7, servo 2 connected to pin 6.

I will try it again.

msdhillon

It Works !!!
It Works!!!

I just changed the dead zone values from 200 to 550. Now it works.

Apparently the middle value of the joystick on the x axis is around 214 while on the y axis is around 515 which is wierd. It should both be around 515. So when I changed the lower limit to 200 and upper limit to 550 it started working.

Thanks very much. Now I will try to understand what is happening in the code and learn something from it. :)

Robin2

Two or three hours spent thinking and reading documentation solves most programming problems.

raschemmel

Would you mind posting the revised code for others to benefit from ?

msdhillon

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

Code: [Select]
#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

zoomkat

The old discussion.

http://forum.arduino.cc/index.php/topic,157967.0.html
Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

Robin2

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

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

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

adam_lumpkins

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

pocukrani

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:
Code: [Select]
#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]);
 }
}


Code: [Select]
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?

Robin2

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.
Two or three hours spent thinking and reading documentation solves most programming problems.

pocukrani

I think https://forum.arduino.cc/index.php?topic=269071.0 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.

Robin2

I think https://forum.arduino.cc/index.php?topic=269071.0 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
Two or three hours spent thinking and reading documentation solves most programming problems.

pocukrani

maybe it was https://forum.arduino.cc/index.php?topic=231242.0

Robin2

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
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up