-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJoystick_an_Servo_2__Kurven.ino
75 lines (55 loc) · 1.62 KB
/
Joystick_an_Servo_2__Kurven.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
65
66
67
68
69
70
71
72
73
74
75
#include <Servo.h>
// Arduino pin numbers Joystick
const int SW_pin = 2; // digital pin connected to switch output
const int X_pin = 1; // analog pin connected to X output
const int Y_pin = 0; // analog pin connected to Y output
// Arduino pin numbers Servo
const int servoPinL = 10; // Linker Servo
const int servoPinR = 11; // Rechter Servo
const int center = 3; // Toleranz Servo
// Definition Servos
Servo ServoL;
Servo ServoR;
// Variablenumformung
int valX = 440;
int valY = 440;
int valB = 0;
void setup()
{
// Einrichtung Servo Pins
ServoL.attach(servoPinL);
ServoR.attach(servoPinR);
// Einrichtung Joystick
pinMode(SW_pin, INPUT);
pinMode(13, OUTPUT);
digitalWrite(SW_pin, HIGH);
Serial.begin(9600);
}
void loop()
{
valY = analogRead(Y_pin);
valY = map(valY, 0, 1023, 0, 180); //Konvertierung Analog Joystick -> Gradmaß
valX = analogRead(X_pin);
valX = map(valX, 0, 1023, 0, 180); //Konvertierung Analog Joystick -> Gradmaß
if(valX > 150 && valY > 150){ // MORGEN WEITERMACHEN!!! SCHRÄGFAHREN!
ServoL.write(valX);
ServoR.write(valY);
}
else if(valY > 95 || valY < 85){ //if Schleife für Toleranz +-5° in Y Richtung
ServoL.write(abs(valY-180));
ServoR.write(valY);
}
else if(valX > 95 || valX < 85){ //if Schleife für Toleranz +-5° in X Richtung
ServoL.write(valX);
ServoR.write(valX);
}
else{ //"Stop" Schleife
ServoL.write(90);
ServoR.write(90);
}
// Monitoring von x und y Werten
Serial.print(valX);
Serial.print(" ");
Serial.println(valY);
delay(5);
}