-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTurrertFirmware
193 lines (150 loc) · 8.02 KB
/
TurrertFirmware
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/***********************************************************************************
* ___________
* | __ | RG 101 - Servo
* | / \ | Joystick Servo
* | | | |
* | \ __ / |
* | |
* | |
* |___________|
*
* This code will incrementally set the position on a servo based on a joystick.
* The servo will move in the direction that the joystick moves, but it will stay
* at its positon when the joystick is centered, allowing you to slowly adjust the
* overall position of the servo.
*
* Digital 3 - RobotGeek Servo
* Analog 0 - RobotGeek Joystick (Horizontal or Vertical)
*
* For a more in depth explanation of wiring, this code and more information on using
* Digital outputs and Arduino check out our Servo Tutorial
* http://learn.robotgeek.com/robotgeek-101-1/233-servo-primer.html
*
*
* Important Functions
* analogRead()
* servo.writeMicroseconds()
* map()
*****************************************************************************************/
#include <Servo.h>
const int ANALOG_PIN_HORZ = 0; //analog pin for joystick
const int ANALOG_PIN_VERT = 1; //analog pin for joystick
const int SERVO_PIN_HORZ = 10; //pin number for the led pin - this is 'const' since it will not change during code, and saves Arduino memory
const int SERVO_PIN_VERT = 11; //pin number for the led pin - this is 'const' since it will not change during code, and saves Arduino memory
const int SERVO_PIN_FIRE = 9;
const int FIRE_BUTTON = 4;
const int LASER = 2; //Laser Digital Pin
const int SERVO_MIN_PULSE = 600; //minimum servo pulse in microceconds (0 degrees)
const int SERVO_MAX_PULSE = 2400; //maximum servo pulse in microceconds (180 degrees)
const int SERVO_CENTER_PULSE = 1500; //centered servo pulse in microceconds (90 degrees)
//deadband values for the joysticks - values between DEADBANDLOW and DEADBANDHIGH will be ignored
const int DEADBANDLOW = 480; //lower deadband value for the joysticks
const int DEADBANDHIGH = 540; //upper deadband value for the joysticks
int analogSensorValueHorz; //the raw value read from the analog sensor
int analogSensorValueMappedHorz; //the value read from the analog sensor, mapped to the degree
int servoValueHorz = 1592; //holds the current PWM value (0-255)
int analogSensorValueVert; //the raw value read from the analog sensor
int analogSensorValueMappedVert; //the value read from the analog sensor, mapped to the degree
int servoValueVert = 1386; //holds the current PWM value (0-255)
int buttonState = 0; // variable for reading the pushbutton status
int serialData;
int servoSpeed = 10; //alter this value to change the speed of the system. Higher values mean higher speeds 5-500 approximate recommended range
Servo servoHorz; //create servo object, 'servo1'
Servo servoVert; //create servo object, 'servo1'
Servo servoFire;
Servo servoTrig;
void fire(Servo trigger)
{
int trigger_rest =1258;
int trigger_fire =1644;
trigger.writeMicroseconds(trigger_rest);
delay(1000);
trigger.writeMicroseconds(trigger_fire);
delay(1000);
trigger.writeMicroseconds(trigger_rest);
delay(1000);
}
//setup runs once when the Geekduino/Arduino is turned on
void setup()
{
servoHorz.attach(SERVO_PIN_HORZ); //attach the servo on pin SERVO_PIN
servoVert.attach(SERVO_PIN_VERT); //attach the servo on pin SERVO_PIN
servoTrig.attach(SERVO_PIN_FIRE); //attach the servo on pin SERVO_PIN
pinMode(LASER, OUTPUT); // set pin to input
digitalWrite(LASER, HIGH);
Serial.begin(9600);
pinMode(FIRE_BUTTON, INPUT); // set pin to input
} //setup() is done, go to loop()
//loop runs forever once setup is complete
void loop()
{
analogSensorValueHorz = analogRead(ANALOG_PIN_HORZ); //read the analog sensor and store it in 'analogSensorValue'
analogSensorValueVert = analogRead(ANALOG_PIN_VERT); //read the analog sensor and store it in 'analogSensorValue'
//check that the joystick is outisde of the deadband. Movements in the deadband should not register
if(analogSensorValueHorz < DEADBANDLOW || analogSensorValueHorz > DEADBANDHIGH)
{
//analogSensorValue will hold a value between 0 and 1023 that correspods to the location of the joystick. The map() function will convert this value
//into a value between speed and -speed. This value can then be added to the current servoValue to incrementley move ths servo
analogSensorValueMappedHorz = map(analogSensorValueHorz, 0, 1023, -1*servoSpeed, servoSpeed) ;
servoValueHorz = servoValueHorz + analogSensorValueMappedHorz; //add the analogSensorValueMapped to servoValue to slowly increment/decrement the tiltValue
// Serial.write(servoValueHorz);
// We need to keep the servo value between SERVO_MIN_PULSE and SERVO_MAX_PULSE - the contstrain function takes care of this.
//Values below SERVO_MIN_PULSE are set to SERVO_MIN_PULSE and above SERVO_MAX_PULSE are set to SERVO_MAX_PULSE
servoValueHorz = constrain(servoValueHorz, SERVO_MIN_PULSE, SERVO_MAX_PULSE); //
// Serial.write(servoValueVert);
}
//check that the joystick is outisde of the deadband. Movements in the deadband should not register
if(analogSensorValueVert < DEADBANDLOW || analogSensorValueVert > DEADBANDHIGH)
{
//analogSensorValue will hold a value between 0 and 1023 that correspods to the location of the joystick. The map() function will convert this value
//into a value between speed and -speed. This value can then be added to the current servoValue to incrementley move ths servo
analogSensorValueMappedVert = map(analogSensorValueVert, 0, 1023, -1*servoSpeed, servoSpeed) ;
servoValueVert = servoValueVert + analogSensorValueMappedVert; //add the analogSensorValueMapped to servoValue to slowly increment/decrement the tiltValue
// We need to keep the servo value between SERVO_MIN_PULSE and SERVO_MAX_PULSE - the contstrain function takes care of this.
//Values below SERVO_MIN_PULSE are set to SERVO_MIN_PULSE and above SERVO_MAX_PULSE are set to SERVO_MAX_PULSE
servoValueVert = constrain(servoValueVert, SERVO_MIN_PULSE, SERVO_MAX_PULSE); //
}
servoHorz.writeMicroseconds(servoValueHorz); //send the servo to the value in 'servoValue', thus adjusting the servo based on the analog input
servoVert.writeMicroseconds(servoValueVert); //send the servo to the value in 'servoValue', thus adjusting the servo based on the analog input
delay(10); //short delay to account for servo movement
buttonState = digitalRead(FIRE_BUTTON);
Serial.print(servoValueHorz);
Serial.print("\n");
if (buttonState == HIGH){
fire(servoTrig);
}
/////////////////////////////////Serial//////////////////////////////////////////////////
if (Serial.available() > 0){
serialData=Serial.read();
if (serialData == '0'){//left
servoValueHorz-=10;
servoHorz.writeMicroseconds(servoValueHorz);
}
if (serialData == '1'){//right
servoValueHorz+=10;
servoHorz.writeMicroseconds(servoValueHorz);
}
if (serialData == '2'){//up
servoValueVert+=10;
servoVert.writeMicroseconds(servoValueVert);
}
if (serialData == '3'){//down
servoValueVert-=10;
servoVert.writeMicroseconds(servoValueVert);
}
if (serialData == '4'){
int pinread =digitalRead(LASER);
if (pinread == HIGH){
digitalWrite(LASER, LOW);
}
if (pinread == LOW){
digitalWrite(LASER, HIGH);
}
}
if (serialData == '5'){
fire(servoTrig);
}
}
servoHorz.writeMicroseconds(servoValueHorz); //send the servo to the value in 'servoValue', thus adjusting the servo based on the analog input
servoVert.writeMicroseconds(servoValueVert); //send the servo to the value in 'servoValue', thus adjusting the servo based on the analog input
}//go back to the first line in loop()