-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsp32.ino
139 lines (116 loc) · 3.1 KB
/
sp32.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
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
131
132
133
134
135
136
137
138
139
#include <math.h>
#include <ESP32Servo.h>
Servo sortServo;
//port: COM4 (Elecrow CrowPanel 7.0P)
const int trigPin = 2;
const int echoPin = 4;
const int pirPin = 16;
const int buttonPin = 21;
float duration, distance;
double averageUltraReading;
int count = 0;
int counterTimeoutUltrasonic = 0;
int counterTimeoutPIR = 0;
int garbageCount = 0;
int recyclingCount = 0;
int previousPIRstate = 0;
int servoRotationState = 0;
int incomingByte;
void setup()
{
sortServo.attach(23);
sortServo.write(90);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(pirPin, INPUT);
pinMode(buttonPin, INPUT);
Serial.begin(115200);
}
void loop()
{
if(Serial.available() > 0){
incomingByte = Serial.read();
Serial.println(incomingByte);
if(incomingByte == 65){ //2, 50
sortServo.write(150);
delay(2000);
sortServo.write(90);
delay(2000);
}
if(incomingByte == 66){ //3, 51
sortServo.write(30);
delay(2000);
sortServo.write(90);
delay(2000);
}
}
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(5);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration * .0343)/2;
if(count <= 10){
averageUltraReading = ((averageUltraReading * count) + distance)/(count+1);
count++;
counterTimeoutUltrasonic++;
}else if(count > 10){
if(fabs(averageUltraReading - distance) < 5){
averageUltraReading = ((averageUltraReading * count) + distance)/(count+1);
if(count < 20){
count++;
}
counterTimeoutUltrasonic++;
// if(sortServo.read() != 90){
// sortServo.write(90);
// }
}else{
if(counterTimeoutUltrasonic > 10){
garbageCount++;
Serial.println("garbage");
counterTimeoutUltrasonic = 0;
}
// Serial.print("Detected Garbage, Garbage count: ");
// Serial.println(garbageCount);
// if(sortServo.read() != 180){
// sortServo.write(180);
// }
// if(servoRotationState < 10){
// Serial.println("Tilting servo left");
// sortServo.write(180);
// servoRotationState++;
// }else if(servoRotationState <= 50){
// servoRotationState++;
// }else if(servoRotationState > 50){
// Serial.println("Tilting servo middle");
// sortServo.write(90);
// }
}
}
int pirRead = digitalRead(pirPin);
// Serial.println(pirRead);
if(pirRead == 0){
counterTimeoutPIR++;
previousPIRstate = 0;
// if(sortServo.read() != 90){
// sortServo.write(90);
// }
}else if (previousPIRstate == 0 && pirRead == 1){
previousPIRstate = 1;
if(counterTimeoutPIR > 10){
Serial.println("recycling");
recyclingCount++;
counterTimeoutPIR = 0;
}
// Serial.print("Detected Recycling, Recycling count: ");
// Serial.println(recyclingCount);
// if(sortServo.read() != 0){
// sortServo.write(0);
// }
}
// Serial.print(distance);
// Serial.println(averageUltraReading);
// Serial.println(digitalRead(buttonPin));
delay(100);
}