Good morning / afternoon:
My BOM for the project:
Arduino UNO
4 servos (TowerPro SG-5010 double ball bearing)
2 Parallax joysticks (# 27800 ver B)
2 Bunkerhill camera.
My objective: To build a pan tilt mechanism, that will move the cameras that are mounted in the direction I request for the purpose of weather monitoring, utilizing the 2 listed joysticks.
What I have been able to do so far.
I was able to obtain a sketch from instructables which was originally written for only 1 joystick and 2 servos. I was able to add to that code to include the additional 2 servos and additional joystick. Credit for the pre-additions of programming can be found here: http://www.instructables.com/id/Arduino-2-Servos-Thumbstick-joystick/
The new programming (aka my additional information), works as required.
Now, what I thought I could simply achieve:
I thought that once I moved the joystick to a certain direction, the servo would stay at that location until I resent another signal.
For example: The servo for panning was looking North (this would be the 90 degree or zero mark of the servo). I want to see what is coming in from the northwest so I would then engage the joystick to pan the camera in that direction, release the joystick and the pan stays locked in that position (again being northwest), until I engaged the joystick to move the servo in a different direction.
What I am finding out:
My “thought” isn’t working, and believe that it is either a coding issue, a joystick issue or both.
Would you please take a look at the following code, again as it is written it works, its just not “locking” the servo in that last position.
Now the code:
#include <Servo.h>
const int servo1 = 3; // first servo
const int servo2 = 11; // second servo
const int servo3 = 6; // third servo
const int servo4 = 10; // fourth servo
const int joyH = 3; // L/R Parallax Thumbstick
const int joyV = 4; // U/D Parallax Thumbstick
const int joyI = 1; // L/R Parallax Thumbstick
const int joyW = 2; // U/D Parallax Thumbstick
int servoVal; // variable to read the value from the analog pin
Servo myservo1; // create servo object to control a servo
Servo myservo2; // create servo object to control a servo
Servo myservo3; // create servo object to control a servo
Servo myservo4; // create servo object to control a servo
void setup() {
// Servo
myservo1.attach(servo1); // attaches the servo
myservo2.attach(servo2); // attaches the servo
myservo3.attach(servo3); // attaches the servo
myservo4.attach(servo4); // 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)
servoVal = analogRead(joyH);
servoVal = map(servoVal, 0, 1023, 0, 180); // scale it to use it with the servo (result between 0 and 180)
myservo2.write(servoVal); // sets the servo position according to the scaled value
// Read the horizontal joystick value (value between 0 and 1023)
servoVal = analogRead(joyV);
servoVal = map(servoVal, 0, 1023, 70, 180); // scale it to use it with the servo (result between 70 and 180)
myservo4.write(servoVal); // sets the servo position according to the scaled value
// Read the horizontal joystick value (value between 0 and 1023)
servoVal = analogRead(joyI);
servoVal = map(servoVal, 0, 1023, 70, 180); // scale it to use it with the servo (result between 70 and 180)
myservo1.write(servoVal); // sets the servo position according to the scaled value
// Read the horizontal joystick value (value between 0 and 1023)
servoVal = analogRead(joyW);
servoVal = map(servoVal, 0, 1023, 70, 180); // scale it to use it with the servo (result between 70 and 180)
myservo3.write(servoVal); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there
}
/**
- Display joystick values
*/
void outputJoystick(){
Serial.print(analogRead(joyH));
Serial.print ("---");
Serial.print(analogRead(joyV));
Serial.println ("----------------");
Serial.print(analogRead(joyI));
Serial.print ("---");
Serial.print(analogRead(joyW));
Serial.println ("----------------");
}
Thank you in advance for your assistance.
Tony