blob: 2d06af9679e72c3f2deffe9821a15d3db0149dc1 (
plain)
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
|
int incomingbyte1 = 0; //the byte that it is reciefing
bool iswriting1 = false; //true if writing else fals
String tlocdat1 = ""; //temporary location data
String locdat1 = ""; //location data
int nir1 = "-99"; // which ir sensor it is
int pir1 = "-99"; // amount of pulses it had
int incomingbyte2 = 0; //same but for seeed xiao N 2
bool iswriting2 = false;
String tlocdat2 = "";
String locdat2 = "";
int nir2 = "-99";
int pir2 = "-99";
float degwhere = -99.0; // where the ball is in degrees
void setup() {
Serial.begin(9600);
Serial1.begin(9600);
Serial2.begin(9600);
}
void loop() {
if (Serial1.available() > 0) {
incomingbyte1 = Serial1.read();
//Serial.print(char(incomingbyte1));
if (incomingbyte1 == 's'){
iswriting1 = true;
tlocdat1 = "";
}else if(incomingbyte1 == 'e'){
iswriting1 = false;
locdat1 = tlocdat1;
}else if(iswriting1 == true){
//Serial.print(char(incomingbyte1));
tlocdat1 += (char)incomingbyte1;
}
}
if (Serial2.available() > 0) {
incomingbyte2 = Serial2.read();
//Serial.print(char(incomingbyte2));
if (incomingbyte2 == 's'){
iswriting2 = true;
tlocdat2 = "";
}else if(incomingbyte1 == 'e'){
iswriting2 = false;
locdat2 = tlocdat1;
}else if(iswriting2 == true){
//Serial.print(char(incomingbyte1));
tlocdat2 += (char)incomingbyte1;
}
}
//Serial.println(locdat);
sscanf(locdat1.c_str(), "%d, %d", &nir1, &pir1);
sscanf(locdat2.c_str(), "%d, %d", &nir2, &pir2);
if(pir1 > pir2){
if(nir1 = 1){ //not good looking code because posbile wrong wireing
degwhere = 0;
}else if(nir1 = 2){
degwhere = 22.5;
}else if(nir1 = 3){
degwhere = 45;
}else if(nir1 = 4){
degwhere = 67.5;
}else if(nir1 = 5){
degwhere = 90;
}else if(nir1 = 6){
degwhere = 112.5;
}else if(nir1 = 7){
degwhere = 135;
}else if(nir1 = 8){
degwhere = 157.5;
}
}else{
if(nir2 = 1){
degwhere = 180;
}else if(nir2 = 2){
degwhere = 202.5;
}else if(nir2 = 3){
degwhere = 225;
}else if(nir2 = 4){
degwhere = 247.5;
}else if(nir2 = 5){
degwhere = 270;
}else if(nir2 = 6){
degwhere = 292.5;
}else if(nir2 = 7){
degwhere = 315;
}else if(nir2 = 8){
degwhere = 337.5;
}
}
Serial.print("IR sensor N: ");
Serial.print(nir1);
Serial.print(", with ");
Serial.print(pir1);
Serial.println(" pulses.");
Serial.print("That is ");
Serial.print(degwhere);
Serial.println(" degrees.");
delay(10);
}
|