The Cobra Attack Helicopter had a gun stow switch in the cockpit. Basically I want switch that puts the turret at 0 degrees azimuth/12 degrees of elevation then, locks out the input from the joystick. I have been trying to code that into the project for hours. It seems like the joystick overrides any other command I input with a switch. Can you only use one input method at a time on a servo?
#include <Servo.h>
//Servo objects created to control the servos
Servo myServo1;
Servo myServo2;
int servo1 = 10; //Digital PWM pin used by the servo 1
int servo2 = 9; //Digital PWM pin used by the servo 2
int joyX = A0; //Analog pin to which the joystick (X) is connected
int joyY = A1; //Analog pin to which the joystick (Y) is connected
int button1 = 3;
boolean IgnoreInput = false;
void setup(){
myServo1.attach(servo1);
myServo2.attach(servo2);
TIMSK0=0;
}
void loop(){
int valX = analogRead(joyX); //Read the joystick X value (value between 0 and 1023)
int valY = analogRead(joyY); //Read the joystick Y value (value between 0 and 1023)
valX = map(valX, 0, 1023, 0, 50); //Scale the joystick X value to use it with the servo
valY = map(valY, 0, 1023, 0, 220); //Scale the joystick X value to use it with the servo
//Sets the servo position according to the scaled values.
myServo1.write(valX);
delay(20);
myServo2.write(valY);
delay(20);
}
Please read the post at the start of any forum , entitled "How to use this Forum".
OR http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
What are the stow positions (X, Y)? Which input pin for the stow switch? How is the stow switch wired? Which Arduino?
OP's code:
#include <Servo.h>
//Servo objects created to control the servos
Servo myServo1;
Servo myServo2;
int servo1 = 10; //Digital PWM pin used by the servo 1
int servo2 = 9; //Digital PWM pin used by the servo 2
int joyX = A0; //Analog pin to which the joystick (X) is connected
int joyY = A1; //Analog pin to which the joystick (Y) is connected
int button1 = 3;
boolean IgnoreInput = false;
void setup(){
myServo1.attach(servo1);
myServo2.attach(servo2);
TIMSK0=0;
}
void loop(){
int valX = analogRead(joyX); //Read the joystick X value (value between 0 and 1023)
int valY = analogRead(joyY); //Read the joystick Y value (value between 0 and 1023)
valX = map(valX, 15, 1023, 0, 50); //Scale the joystick X value to use it with the servo
valY = map(valY, 110, 1023, 0, 110); //Scale the joystick X value to use it with the servo
//Sets the servo position according to the scaled values.
myServo1.write(valX);
delay(20);
myServo2.write(valY);
delay(20);
}
X=0
Y=12
Button is on Digital pin 3
But, I'm trying to figure out how to do the initialize or read a switch pin. As I'm not familiar with the coding part.
also, not to sure how the map function works.
Because I want it to be Xmin=-110/XMax=110 and Ymin= 15/Ymax = 50.
"It seems like the joystick overrides any other command I input with a switch. Can you only use one input method at a time on a servo?"
You might put your action code in conditional "if" conditions. If a button or trigger is active, have the servos follow the joystick commands. If a different button is active, command the servos to go to the storage position.
Didn't notice the IDE program had examples. When off of something I found on the internet. Now that I have looked at the servo sweep example... this makes a lot more sense. but, from that I see the MAP thing isn't necessary. I'm going the correct direction at-least. Since this college project gives me two months to get this thing running correctly.. Then, Ill work on this gun stow idea. I'm going throw out the current code and do it all myself. I know the basics of C++... just needed to see an example.