forked from hadefuwa/elegoo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Analog_Joystick.ino
49 lines (43 loc) · 1.02 KB
/
Analog_Joystick.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
//www.elegoo.com
//2016.12.09
// Arduino pin numbers
const int SW_pin = 2; // digital pin connected to switch output
const int X_pin = 0; // analog pin connected to X output
const int Y_pin = 1; // analog pin connected to Y output
void setup() {
pinMode(SW_pin, INPUT);
digitalWrite(SW_pin, HIGH);
Serial.begin(9600);
}
void loop() {
Serial.print("Switch: ");
Serial.print(digitalRead(SW_pin));
Serial.print("\n");
Serial.print("X-axis: ");
Serial.print(analogRead(X_pin));
Serial.print("\n");
Serial.print("Y-axis: ");
Serial.println(analogRead(Y_pin));
Serial.print("\n\n");
if (digitalRead(SW_pin) == 0)
{
Serial.print("\nFLIP!!\n");
}
if (analogRead(X_pin) > 800)
{
Serial.print("\nRIGHT!!\n");
}
else if(analogRead(X_pin) < 400)
{
Serial.print("LEFT!!\n");
}
if (analogRead(Y_pin) < 100)
{
Serial.print("\nFORWARD!!\n");
}
else if(analogRead(Y_pin) > 600)
{
Serial.print("REVERSE!!\n");
}
delay(1000);
}