im making a pan/tilt turret out of a battery powered airsoft gun and using an 8way arcade joystick to control the movement. im very new to programming and im not sure how i go about translating a push of the joystick into servo moment in the right direction. i was planning on hooking each switch on the bottom of the joystick to a separate pin on my arduino (2-5) and all the grounds together, then putting the control wire for the servos on 6/7. what code would i need to make the gun move left or right and also to the diagonals to. i just need a bit of an example to get me started, i like working through the problems myself so i dont need all the code just an idea of how to get started. thanks
what code would i need to make the gun move left or right and also to the diagonals to.
I'm pretty sure that servos can't move left, right, or on the diagonal. Every servo I've ever seen rotates. To make it rotate one way, call Servo.write() with a value higher that last time. To make it rotate the other way, call Servo.write() with a lower value than last time.
Now, determining what that value is is the tricky part. First, you need to know what value you get from the joystick, in each direction. Do you have that?
Previous post that might be of interest.
the servos do only rotate, so i put one as the base turning left and right and the gun mounted to one giving me up and down movement. as for the diagonals, i figured something like this, but code of coarse- if button 1 is pressed (being left), move left until 0, if 2 (right) is pressed, move right until 180, if 1(left) and 3(up), move left to 0 and move up to limit, that is as long as the buttons stay pressed, if after 2 seconds no buttons are pressed return to center for both axis. as for the values of the buttons, its an arcade joystick, not sure if your familiar but it only has 4 micro switches on the bottom so each direction is either on or off, heres a link http://na.suzohapp.com/joysticks/competition_joy_e.htm
i figured something like this, but code of coarse- if button 1 is pressed (being left), move left until 0, if 2 (right) is pressed, move right until 180, if 1(left) and 3(up), move left to 0 and move up to limit, that is as long as the buttons stay pressed, if after 2 seconds no buttons are pressed return to center for both axis.
That can be made to work. If you want to ignore the joystick that you have in your hand.
How often will you move a step to the left or right, if the switch is held down?
Testing that a switch is pressed is easy. Changing the value of a variable if the switch is pressed is easy. Doing so on a regular (and reasonable) basis is the challenging part.
the joystick is what presses the buttons, but to keep things simple, lets ignore the joystick. i would like to have it move fairly smoothly so just a few milliseconds between steeps if possible
The servo sweep example does incremental servo movements and might be worth looking at for starters. If both the x and y axis switches are to be depressed at the same time, the blink without delay example might also be useful.
i dont seem to have nearly enough experience or knowledge of programming to be able to do this on my own. would this be easier to accomplish with an analog joystick instead of using the micro switches? there seems to be alot more info floating around about using a normal joystick i guess thats for a reason
What have you tried so far? I'd start wih writing a code to increase a number by 1 when one button is pushed, and decreasing the number by one when the other button is pushed.
all ive really done so far was modify the servo sweep example to make the turret move left, right then up and down but it didnt go about it the way i thought it was goin to, but i just found an example of how to read how long a button is pressed and now im trying to figure out what else i need to combine with that code to make a joystick movement move the turret
Check some of the post below. I think using a button to fade an LED could be changed from PWM to using the servo library to increase/decrease a position of a servo when a button is pushed.