Go Down

Topic: continuos servo+joystick+potentiometer? (Read 787 times) previous topic - next topic

PaulS

I posted my code above! with this code i can control the 360 servo with a joystick, my question about the code of PaoulS is: where shoud i have to add his code with mine? and also i don't understand wich pin pot i have to use!


You have one line of code that controls the servo. Replace that one line with the 4 lines of code I posted.

Connect the pot to whichever analog pin makes your heart go pitter-patter. Just use that same pin number in the analogRead() call.
The art of getting good answers lies in asking good questions.

robyr1

You have one line of code that controls the servo. Replace that one line with the 4 lines of code I posted.

Connect the pot to whichever analog pin makes your heart go pitter-patter. Just use that same pin number in the analogRead() call.
Thanks PaulS, tried to follow your suggestions, can you please take a look to this code?

Code: [Select]

// Arduino 07 -  Servo, Serial Monitor, and Joystick
//
//This program controls a servo motor continuously
//according to the input provided from a joystick
//and shows joystick input and output to servo on the screen
//of an attached computer, via the Arduino Serial Monitor.
// ---------------------------------------------------------------------------

#include <Servo.h>
#define SERVO_PIN 9
#define GROUND_JOY_PIN A3            //joystick ground pin will connect to Arduino analog pin A3
#define VOUT_JOY_PIN A2              //joystick +5 V pin will connect to Arduino analog pin A2
#define XJOY_PIN A1                  //X axis reading from joystick will go into analog pin A1
Servo myservo ;

void setup()
{
  Serial.begin(9600);
  pinMode(VOUT_JOY_PIN, OUTPUT) ;    //pin A3 shall be used as output
  pinMode(GROUND_JOY_PIN, OUTPUT) ;  //pin A2 shall be used as output
  digitalWrite(VOUT_JOY_PIN, HIGH) ; //set pin A3 to high (+5V)
  digitalWrite(GROUND_JOY_PIN,LOW) ; //set pin A3 to low (ground)
  myservo.attach(9);
}

void loop()
{
  delay(300);                    
  int joystickXVal = analogRead(XJOY_PIN) ;  //read joystick input on pin A1
  Serial.print(joystickXVal);                //print the value from A1
  Serial.println(" = input from joystick");  //print "=input from joystick" next to the value
  Serial.print((joystickXVal+520)/10);       //print a from A1 calculated, scaled value
  Serial.println(" = output to servo");      //print "=output to servo" next to the value
  Serial.println() ;
   int aVal = analogRead(A0);
   float rat = aVal / 1023;

   myservo.write((aVal*((joystickXVal+570)/10)-13));
 }



robyr1

but doesn't work good because the servo now turns continuosly..and i lost the control of it!

slipstick

Maybe try changing the aVal in the write() to rat instead.

Steve

robyr1

Nothing! i tried to change into "rat", but i still have the same problem: the servo turns it self!

Code: [Select]

#include <Servo.h>



// Arduino 07 -  Servo, Serial Monitor, and Joystick
//
//This program controls a servo motor continuously
//according to the input provided from a joystick
//and shows joystick input and output to servo on the screen
//of an attached computer, via the Arduino Serial Monitor.
// ---------------------------------------------------------------------------

#include <Servo.h>
#define SERVO_PIN 9
#define GROUND_JOY_PIN A3            //joystick ground pin will connect to Arduino analog pin A3
#define VOUT_JOY_PIN A2              //joystick +5 V pin will connect to Arduino analog pin A2
#define XJOY_PIN A1                  //X axis reading from joystick will go into analog pin A1
Servo myservo ;

void setup()
{
  Serial.begin(9600);
  pinMode(VOUT_JOY_PIN, OUTPUT) ;    //pin A3 shall be used as output
  pinMode(GROUND_JOY_PIN, OUTPUT) ;  //pin A2 shall be used as output
  digitalWrite(VOUT_JOY_PIN, HIGH) ; //set pin A3 to high (+5V)
  digitalWrite(GROUND_JOY_PIN,LOW) ; //set pin A3 to low (ground)
  myservo.attach(9);
}

void loop()
{
  delay(300);                    
  int joystickXVal = analogRead(XJOY_PIN) ;  //read joystick input on pin A1
  Serial.print(joystickXVal);                //print the value from A1
  Serial.println(" = input from joystick");  //print "=input from joystick" next to the value
  Serial.print((joystickXVal+520)/10);       //print a from A1 calculated, scaled value
  Serial.println(" = output to servo");      //print "=output to servo" next to the value
  Serial.println() ;
   int aVal = analogRead(A0);
   float rat = aVal / 1023;

   myservo.write((rat*((joystickXVal+570)/10)-13));
 }




  

PaulS

Oops. aVal/1023 will always be 0 or 1. You want aVal/1023.0.
The art of getting good answers lies in asking good questions.

robyr1

Thanks again PaulS, but nothing change..same problem!

Code: [Select]


// Arduino 07 -  Servo, Serial Monitor, and Joystick
//
//This program controls a servo motor continuously
//according to the input provided from a joystick
//and shows joystick input and output to servo on the screen
//of an attached computer, via the Arduino Serial Monitor.
// ---------------------------------------------------------------------------

#include <Servo.h>
#define SERVO_PIN 9
#define GROUND_JOY_PIN A3            //joystick ground pin will connect to Arduino analog pin A3
#define VOUT_JOY_PIN A2              //joystick +5 V pin will connect to Arduino analog pin A2
#define XJOY_PIN A1                  //X axis reading from joystick will go into analog pin A1
Servo myservo ;

void setup()
{
  Serial.begin(9600);
  pinMode(VOUT_JOY_PIN, OUTPUT) ;    //pin A3 shall be used as output
  pinMode(GROUND_JOY_PIN, OUTPUT) ;  //pin A2 shall be used as output
  digitalWrite(VOUT_JOY_PIN, HIGH) ; //set pin A3 to high (+5V)
  digitalWrite(GROUND_JOY_PIN,LOW) ; //set pin A3 to low (ground)
  myservo.attach(9);
}

void loop()
{
  delay(300);                   
  int joystickXVal = analogRead(XJOY_PIN) ;  //read joystick input on pin A1
  Serial.print(joystickXVal);                //print the value from A1
  Serial.println(" = input from joystick");  //print "=input from joystick" next to the value
  Serial.print((joystickXVal+520)/10);       //print a from A1 calculated, scaled value
  Serial.println(" = output to servo");      //print "=output to servo" next to the value
  Serial.println() ;
   int aVal = analogRead(A0);
   float rat = aVal/10230.0;

   myservo.write((aVal*((joystickXVal+570)/10)-13));
 }






   

slipstick

That's because you still aren't using rat (with its improved calculation) in the write() statement.

Steve

robyr1

I tried even with "rat", nothing change!!

Code: [Select]



// Arduino 07 -  Servo, Serial Monitor, and Joystick
//
//This program controls a servo motor continuously
//according to the input provided from a joystick
//and shows joystick input and output to servo on the screen
//of an attached computer, via the Arduino Serial Monitor.
// ---------------------------------------------------------------------------

#include <Servo.h>
#define SERVO_PIN 9
#define GROUND_JOY_PIN A3            //joystick ground pin will connect to Arduino analog pin A3
#define VOUT_JOY_PIN A2              //joystick +5 V pin will connect to Arduino analog pin A2
#define XJOY_PIN A1                  //X axis reading from joystick will go into analog pin A1
Servo myservo ;

void setup()
{
  Serial.begin(9600);
  pinMode(VOUT_JOY_PIN, OUTPUT) ;    //pin A3 shall be used as output
  pinMode(GROUND_JOY_PIN, OUTPUT) ;  //pin A2 shall be used as output
  digitalWrite(VOUT_JOY_PIN, HIGH) ; //set pin A3 to high (+5V)
  digitalWrite(GROUND_JOY_PIN,LOW) ; //set pin A3 to low (ground)
  myservo.attach(9);
}

void loop()
{
  delay(300);                   
  int joystickXVal = analogRead(XJOY_PIN) ;  //read joystick input on pin A1
  Serial.print(joystickXVal);                //print the value from A1
  Serial.println(" = input from joystick");  //print "=input from joystick" next to the value
  Serial.print((joystickXVal+520)/10);       //print a from A1 calculated, scaled value
  Serial.println(" = output to servo");      //print "=output to servo" next to the value
  Serial.println() ;
   int aVal = analogRead(A0);
   float rat = aVal/10230.0;

   myservo.write((rat*((joystickXVal+570)/10)-13));
 }






   






   

robyr1

Here attached the wiring diagram!

slipstick

Why did you decide that 10230.0 would be better than the recommended value?

Steve

robyr1

i tried to change to 1023.0 but the servo continue to rotate..the potentiometer can regulate the speed but the servo must be remoted by the joystick

robyr1


PaulS

Code: [Select]
   int aVal = analogRead(A0);
   float rat = aVal/10230.0;

   myservo.write((rat*((joystickXVal+570)/10)-13));

What value is in aVal? What value is in rat?

Debugging by guessing sucks. Stop doing that.
The art of getting good answers lies in asking good questions.

PaulS

Here attached the wiring diagram!
Fritzing DOES let you position components so the lines don't all cross like that. Fritzing lets you route lines. They do NOT all need to be straight lines.

That picture is useless.

The joystick is NOT connected correctly.
The art of getting good answers lies in asking good questions.

Go Up