-
Notifications
You must be signed in to change notification settings - Fork 1
/
contol_maze_with_joystick (4).ino
64 lines (59 loc) · 2.29 KB
/
contol_maze_with_joystick (4).ino
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
#include <Servo.h>
Servo myServoX; // define servo motor for X-axis
Servo myServoY; // define servo motor for Y-axis
int ServoXPin = 9; // define X-axis pin
int ServoYPin = 10; // define Y-axis pin
int ServoXHomePos =90; // set home position for servos
int ServoYHomePos =90;
int ServoXPos =103;
int ServoYPos =135;
int XAxlePin = A0; // define X-axis pin control for joystick A0
int YAxlePin = A1; // define Y-axis pin control for joystick A1
int XAxleValue = 0; // set start up value for joystick
int YAxleValue = 0;
int Direction = 0;
int range = 12; // output range of X or Y movement
int center = range/2; // resting position value
int threshold = range/4; // resting threshold
void setup()
{
myServoX.attach(ServoXPin); // attaching servo X
myServoY.attach(ServoYPin); // attaching servo Y
ServoXPos = ServoXHomePos; // update ServoXPos with home position as startup
ServoYPos = ServoYHomePos; // update ServoYPos with home position as startup
myServoX.write(ServoXPos);
myServoY.write(ServoYPos);
Serial.begin(9600);
}
void loop()
{
XAxleValue = readAxis(XAxlePin);
YAxleValue = readAxis(YAxlePin);
Serial.print(XAxleValue,DEC);
Serial.print(" - ");
Serial.println(YAxleValue,DEC);
// check the values of joystick and move the servos smothly with delay of 100 millisecond
if (XAxleValue>0) { ServoXPos++; myServoX.write(ServoXPos); delay(10*(7-XAxleValue)); }
if (XAxleValue<0) { ServoXPos--; myServoX.write(ServoXPos); delay(10*(7+XAxleValue)); }
if (YAxleValue>0) { ServoYPos++; myServoY.write(ServoYPos); delay(10*(7-YAxleValue)); }
if (YAxleValue<0) { ServoYPos--; myServoY.write(ServoYPos); delay(10*(7+YAxleValue)); }
if (ServoXPos>ServoXHomePos+20) { ServoXPos=ServoXHomePos+20; }
if (ServoXPos<ServoXHomePos-20) { ServoXPos= ServoXHomePos-20; }
if (ServoYPos>ServoYHomePos+20) { ServoYPos=ServoYHomePos+20; }
if (ServoYPos<ServoYHomePos-20) { ServoYPos= ServoYHomePos-20; }
delay(10);
}
int readAxis(int thisAxis) {
// read the analog input:
int reading = analogRead(thisAxis);
// map the reading from the analog input range to the output range:
reading = map(reading, 0, 1023, 0, range);
// if the output reading is outside from the
// rest position threshold, use it:
int distance = reading - center;
if (abs(distance) < threshold) {
distance = 0;
}
// return the distance for this axis:
return distance;
}