-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
130 lines (115 loc) · 3.77 KB
/
main.cpp
File metadata and controls
130 lines (115 loc) · 3.77 KB
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
#include <Arduino.h>
#include <Wifi.h>
#include <AsyncUDP.h>
int pinX = 36; //Here is the pin which the Xaxis from the joystick has been put in to, defined.
int pinY = 39; //Here is the pin which the Yaxis from the joystick has been put in to, defined.
int buttonClick = 13; //Here is the pin which the click button from the joystick has been put in to, defined.
int potPin = 34; //Here is the pin which the potentiometer has been put in to, defined.
int potVal;
int potOutVal;
int prevX;
int prevY;
int tolerance = 20;
int port = 7000; //Here is the port which the udp message is send over, defined.
const char * ssid = "ssid"; //Here is the name for the network defined
const char * password = "password"; // here is the password for the network defined
AsyncUDP udp; //Here an udp object is being made from the AsyncUDP library
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
pinMode(pinX, INPUT);
pinMode(pinY, INPUT);
pinMode(buttonClick, INPUT_PULLUP);
pinMode(potPin,INPUT);
prevX = analogRead(pinX);
prevY = analogRead(pinY);
}
void direction(int horizontal, int vertical){// heres a class for the joystick which defines whether the joysticks all the way to the right, left, up, down or if its in the middle.
if(horizontal == 0){
udp.broadcastTo("moveleft", 7000);
Serial.println("left");
} if(horizontal ==4095){
udp.broadcastTo("moveright", 7000);
Serial.println("Right");
} if(vertical == 0){
udp.broadcastTo("moveup", 7000);
Serial.println("Up");
} if(vertical == 4095){
udp.broadcastTo("movedown", 7000);
Serial.println("Down");
} else{
Serial.println("Stop");
udp.broadcastTo("stop", 7000);
}
}
void speed(int potOutVal){ //heres a class over the potentiometer which defines the speed through 4 stages.
/*if (Speed < 1000){
udp.broadcastTo("speed 1", 7000);
} (Speed >1000 || Speed < 2000){
udp.broadcastTo("speed 4", 7000);
} (Speed > 2000 || Speed < 3000){
udp.broadcastTo("speed 7", 7000);
}Speed > 3000 || Speed <= 4095){
udp.broadcastTo("speed 9", 7000);
}*/
switch(potOutVal){
case 0:
udp.broadcastTo("s1", 7000);
Serial.println("s1");
break;
case 1:
udp.broadcastTo("s2", 7000);
Serial.println("s2");
break;
case 2:
udp.broadcastTo("s3", 7000);
Serial.println("s3");
break;
case 3:
udp.broadcastTo("s4", 7000);
Serial.println("s4");
break;
/*
default:
udp.broadcastTo("s2", 7000);
Serial.println("speed2");*/
/*case 2001 ... 3000:
udp.broadcastTo("speed 7", 7000);
Serial.println("speed3");
case 3001 ... 4095:
udp.broadcastTo("speed 9", 7000);
Serial.println("speed4");
*/
}
}
void loop() {
/*if(abs(analogRead(pinX)-prevX) > tolerance || abs(analogRead(pinY)-prevY) > tolerance){
Serial.println(map(analogRead(pinX), 0, 4095, -100, 100));
Serial.println(map(analogRead(pinY), 0, 4095, -100, 100));
prevX = analogRead(pinX);
}*/
direction(analogRead(pinX), analogRead(pinY));
if(digitalRead(buttonClick) == LOW){
udp.broadcastTo("init 9 9", 7000);
Serial.println("Clicked");
}
potVal = analogRead(potPin);// here it reads the input from the potentiometer and puts it in the speed class
Serial.println(potVal);
potOutVal = map (potVal, 0, 4095, 0, 3);
speed(potOutVal);
//Serial.println(speed(potOutVal));
/*if(potVal < 1000){
speed(1);
}
if(potVal >= 1000 || potVal < 2000){
speed(2);
}
if(potVal >= 2000 || potVal < 3000){
speed(3);
}
if(potVal >= 3000 || potVal <= 4095){
speed(4);
}*/
}