Hi,
I've done some research but still struggling trying to make the pan/tilt motion with servos. I've made one move as it should, played around and don't seem to be getting anywhere. Maybe my connections are off, maybe I've overcomplicated the code (is that possible?)
Materials:
Hextronik HX5010 Twin Bearing Servo
TowerPro MG995 DIGI HI-SPEED Servo
Arduino Duemilanove
Breadboard (Came with starter kit, I guess it's medium size as I've seen longer?)
Smaller breadboard that fits perfectly inside the Arduino (I got one servo moving perfectly with this, have struggled with 2)
9V Battery with adaptor to Arduino
Joystick: Keyes_SJoys (Ebay Search: Black Thumb Joystick Module w 5 Pin Replacement for Arduino High Quality)
I have the Joystick pins hooked up for Analog 3, 4 & 0 respectively.
The Servos hooked up to Digital 4 & 10.
(And Ground and 5V's)
Code:
#include <Servo.h>
//CONSTANTS
const int servo1 = 4; // first servo
const int servo2 = 10; // second servo
//VARIABLE
int VRx = 3; // VRx Thumbstick
int VRy = 4; // VRy Thumbstick
int SW = 0; // SW Thumbstick
int VRxPos = 90;
int VRyPos = 90;
int SWPos = 90;
boolean logging = false;
Servo myservo1; // create servo object to control a servo
Servo myservo2; // create servo object to control a servo
void setup() {
// Servo
myservo1.attach(servo1); // attaches the servo
myservo2.attach(servo2); // attaches the servo
// Inizialize Serial
Serial.begin(9600);
}
void loop(){
// Display Joystick values using the serial monitor
outputJoystick();
// Read the horizontal joystick value (value between 0 and 1023)
VRxPos = analogRead(VRx);
VRxPos = map(VRxPos, 0, 1023, 0, 180); // scale it to use it with the servo (result between 0 and 180)
myservo2.write(VRxPos); // sets the servo position according to the scaled value
// Read the horizontal joystick value (value between 0 and 1023)
VRyPos = analogRead(VRy);
VRyPos = map(VRyPos, 0, 1023, 0, 180); // scale it to use it with the servo (result between 70 and 180)
myservo1.write(VRyPos); // sets the servo position according to the scaled value
delay(50); // waits for the servo to get there
}
/**
- Display joystick values
*/
void outputJoystick(){
Serial.print(VRxPos);
Serial.print ("---");
Serial.print(VRyPos);
Serial.println ("----------------");
Serial.print(SWPos);
Serial.println("------------");
}
Only question about the code is about the SW... do you assign it to anything in the loop?
Any help at all would be appreciated.