Hello everyone!
Today I created a project that I have been greatly looking forward to, I created a "Cat Toy," a laser pointer on top of two servos, X- and Y-axis, both controlled via joystick.
I am using an Arduino UNO R3.
As of a couple hours ago, my project is up and working, but I now have another idea to improve it. The problem is I am having trouble coding out exactly what I want.
What I want is for my 'work envelope' (The space that my laser can reach) to be incrementally controlled, i.e. more like a FPS game, where you can move left, stop, let the joystick re-center, then move more left without the dot re-centering. In short, I am seeking to get rid of the snap-back movement that occurs when the joystick is released. Incremental vs. Absolute coordinates/movement.
Here is my code that is running with two HI-TEC HS-422 digital servos and the sparkfun "retail kit" joystick. The laser is an old .22lr cheap laser sight connected to a digital pin on the UNO.
#include <Servo.h>
Servo servoX;
Servo servoY;
int laser = 2;
int potpinX = A4;
int potpinY = A5;
const int servoPause = 1;
void setup()
{
servoX.attach(9);
servoY.attach(11);
pinMode(laser, OUTPUT);
pinMode(potpinX, INPUT);
pinMode(potpinY, INPUT);
digitalWrite(laser, HIGH);
Serial.begin(9600);
}
void loop()
{
int XpotVal = analogRead(potpinX);
int YpotVal = analogRead(potpinY);
int servoAngleX = map(XpotVal, 0, 1023, 180, 0);
int servoAngleY = map(YpotVal, 0, 1023, 180, 0);
servoX.write(servoAngleX);
servoY.write(servoAngleY);
Serial.print(XpotVal);
Serial.print(" ");
Serial.println(YpotVal);
delay(servoPause);
}
Thanks in advance!