-
Notifications
You must be signed in to change notification settings - Fork 0
/
Analog_Joystick.h
58 lines (49 loc) · 1.34 KB
/
Analog_Joystick.h
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
#ifndef ANALOG_JOYSTICK_H
#define ANALOG_JOYSTICK_H
#include "math.h"
class Analog
{
public:
const int analogMax = 128;
int hori = 0, vert = 0;
int lengths;
float angle;
int mappedHori, mappedVert;
unsigned int lasttime = 0;
int getHori(){return hori;}
int getVert(){return vert;}
int getLengths(){return lengths;}
float getAngle(){return angle;}
int getMappedHori(){return mappedHori;}
int getmappedVert(){return mappedVert;}
void updateValue(int8_t horizontal, int8_t vertical)
{
hori = horizontal;
vert = vertical;
mappedHori = map(hori, -128, 127, -255, 255);
mappedVert = map(vert, -128, 127, -255, 255);
lengths = sqrt(mappedHori*mappedHori + mappedVert*mappedVert);
if(lengths > 255)
lengths = 255;
angle = atan2((float)mappedVert, (float)mappedHori);
}
float radToDeg(float rad)
{
return rad/(2*PI) * 360;
}
void printInfo()
{
if(millis()-lasttime > 2000)
{
Serial.println("========================");
Serial.print("Analog angle: ");
Serial.println(radToDeg(angle));
Serial.print("Length: ");
Serial.println(lengths);
lasttime = millis();
}
}
};
//Declare Global Variable
Analog leftAnalog, rightAnalog;
#endif