-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServoSensor.ino
100 lines (80 loc) · 2.63 KB
/
ServoSensor.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
#include <ESP32Servo.h>
#include "Adafruit_VL53L0X.h"
Adafruit_VL53L0X lox = Adafruit_VL53L0X();
Servo myservo; // create servo object to control a servo
int pos = 0; // variable to store the servo position
int timeDelay = 10;
int angle = 120; // angle it twists to
int new_angle = 0;
bool currentOpen = false;
int minDistance = 1000;
int reading = 0;
void setup() {
Serial.begin(115200);
// servo setup:
myservo.attach(15); // attaches the servo on pin 9 to the servo object
myservo.writeMicroseconds(1500);
myservo.write(0);
pinMode(LED_BUILTIN, OUTPUT);
// wait until serial port opens for native USB devices
while (! Serial) {
delay(1);
}
// sensor setup:
if (!lox.begin()) {
Serial.println(F("Failed to boot VL53L0X"));
while(1);
}
// power
Serial.println(F("cultivating communities lotti"));
}
void loop() {
VL53L0X_RangingMeasurementData_t measure;
lox.rangingTest(&measure, false);
reading = measure.RangeMilliMeter;
if (int(reading) < int(minDistance)){
if (currentOpen == false) {
Serial.println("Trigger Open");
FlowerOpen(angle);
}
} else if (int(reading) > int(minDistance)) {
if (currentOpen == true) {
Serial.println("Trigger Close");
FlowerClose(angle);
}
} else {
Serial.println("We fuckd up!");
}
Serial.print("Distance: ");
Serial.println(measure.RangeMilliMeter);
delay(100);
//Serial.println("Main Loop");
//Serial.println(currentOpen);
}
void FlowerOpen(int targetAngle) {
Serial.println("Opening");
new_angle = map(targetAngle,0,90,0,116);
for (pos = 0; pos <= new_angle; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(timeDelay); // waits 15ms for the servo to reach the position
}
currentOpen = true;
Serial.println("End of flower open");
Serial.print("Open?: ");
Serial.println(currentOpen);
}
void FlowerClose(int targetAngle){
Serial.println("Closing");
new_angle = map(targetAngle,0,90,0,116);
for (pos = new_angle; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
// Serial.print("Current Pos: ");
//Serial.println(pos);
delay(timeDelay); // waits 15ms for the servo to reach the position
}
currentOpen = false;
Serial.println("End of flower close");
Serial.print("Open?: ");
Serial.println(currentOpen);
}