Go Down

Topic: controlling Parallax continous servos using joystick(pot) (Read 457 times) previous topic - next topic

erlens04

Hello fellow Arduino users.

I`m kinda new to C++ programming, but quite interested in this programming language.
Normally I use PLC in my work, so this is something new for me.

The thing I wanna do with my program is to control the direction of 2 ea Parallax continuos servos, using two pot meters.

I want tor be able to:

-1. Start, and run both servos in opposite direction using the joystick intput-value from 600-1024
-2. Stop the servos when the joystick is in the mid-position (input value fluctuates 520-530)
-3. Start, and run both servos in inverted direction relative to statement "1" using the joystick intput-value from 0-400

The problem I run into is when using "if" functionality.. I cannot get the servos to run as desired. :~


Code: [Select]

#include <Servo.h>            //intialize servoSUBprogram

Servo servoX;        // Create Servo object to control the servo
Servo servoY;        // Create Servo object to control the servo

int X = A0;                   //map variabel x to A0
int Y = A1;                   //map variabel y to A1

int pos = 650;                //variable
int neg = 350;                //variable
int mid = (450,550);          //variable

void setup(){
  Serial.begin(9600);         //initialize serial com
 
}

void loop(){
 
  int X = analogRead(A0);       //reads the value of the potentiometer (value between 0 and 1023)
  int Y = analogRead(A1);       //reads the value of the potentiometer (value between 0 and 1023)

  servoX.attach(7);  // Servo is connected to digital pin 7
  servoY.attach(8);  // Servo is connected to digital pin 8

  Serial.print("pot X");        // unique header to identify start of message
  Serial.print(",");            // comma to differenciate
  Serial.print( X ,DEC);        // print variable in DEC
  Serial.print("     ");        // space
  Serial.print("pot Y");        // unique header to identify start of message
  Serial.print(",");            // comma to differenciate
  Serial.print( Y ,DEC);        // print variable in DEC
  Serial.print(",");            // note that a comma is sent after the last field
  Serial.println();             // send a cr/lf
  delay(1);                     // wait ms
 
  if (Y>pos){
    servoX.writeMicroseconds(1300);  // servo direction
    servoY.writeMicroseconds(1700);  // opposite direction
    }
   
  if (Y<neg){
    servoX.writeMicroseconds(1700);  // servo direction
    servoY.writeMicroseconds(1300);  // opposite direction
  }
  if (Y=mid){
    servoX.writeMicroseconds(1500);  // servo stop
    servoY.writeMicroseconds(1500);  // servo stop
  }
 
}

AWOL

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

ash901226

Code: [Select]
if (Y=mid)
suppose to be
Code: [Select]
if (Y==mid)

single "=" assign
double "=" compare

PeterH

If there are only three actions then you don't need all three conditions:

Code: [Select]

  if (Y>pos)
  {
    // forwards
    servoX.writeMicroseconds(1300);  // servo direction
    servoY.writeMicroseconds(1700);  // opposite direction
  }
  else if (Y<neg)
  {
    // reverse
    servoX.writeMicroseconds(1700);  // servo direction
    servoY.writeMicroseconds(1300);  // opposite direction
  }
  else
  {
    // stop
    servoX.writeMicroseconds(1500);  // servo stop
    servoY.writeMicroseconds(1500);  // servo stop
  }

I only provide help via the forum - please do not contact me for private consultancy.

AWOL

#4
Dec 13, 2012, 07:20 am Last Edit: Dec 13, 2012, 07:21 am by AWOL Reason: 1
Code: [Select]
int mid = (450,550);
I missed that one.
I'm a bit surprised the compiler did too
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

PeterH


Code: [Select]
int mid = (450,550);
I missed that one.
I'm a bit surprised the compiler did too



The value of a comma-separated list of values is the last value in the list, so it looks like a legitimate assignment - although unlikely to do what was intended.
I only provide help via the forum - please do not contact me for private consultancy.

AWOL

Yeah, I posted that from my phone, and couldn't see properly whether they were braces or parentheses.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up