-
Notifications
You must be signed in to change notification settings - Fork 2
/
motor+sensor
98 lines (71 loc) · 2.18 KB
/
motor+sensor
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
#include <VL6180X.h>
#include <Wire.h>
VL6180X sensor;
int leftWheelB = 23; //A1
int leftWheelA = 22; //A2
int rightWheelA = 20; //B1
int rightWheelB = 21; //B2
int onboardLED = 13;
int speed = 50;
int SenVal;
void setup() {
// Pin configuration
pinMode(leftWheelB, OUTPUT);
pinMode(leftWheelA, OUTPUT);
pinMode(rightWheelB, OUTPUT);
pinMode(rightWheelA, OUTPUT);
pinMode(onboardLED, OUTPUT);
// Show when robot is on
digitalWrite(onboardLED, HIGH);
Serial.begin(9600);
Wire.begin();
sensor.init();
sensor.configureDefault();
// Reduce range max convergence time and ALS integration
// time to 30 ms and 50 ms, respectively, to allow 10 Hz
// operation (as suggested by Table 6 ("Interleaved mode
// limits (10 Hz operation)") in the datasheet).
sensor.writeReg(VL6180X::SYSRANGE__MAX_CONVERGENCE_TIME, 30);
sensor.writeReg16Bit(VL6180X::SYSALS__INTEGRATION_PERIOD, 50);
sensor.setTimeout(500);
// stop continuous mode if already active
sensor.stopContinuous();
// in case stopContinuous() triggered a single-shot
// measurement, wait for it to complete
delay(3000);
// start interleaved continuous mode with period of 100 ms
sensor.startInterleavedContinuous(100);
}
void setDriveSpeed(int reading) {
if (reading < 20){
analogWrite(leftWheelA, speed);
analogWrite(leftWheelB, 0);
analogWrite(rightWheelA, speed);
analogWrite(rightWheelB, 0);
}
else {
analogWrite(leftWheelA, 0);
analogWrite(leftWheelB, 0);
analogWrite(rightWheelA, 0);
analogWrite(rightWheelB, 0);
}
//analogWrite(leftWheelA, 0);
//analogWrite(leftWheelB, speed);
//analogWrite(rightWheelA, 0);
//analogWrite(rightWheelB, speed);
//delay(1000);
}
void loop() {
SenVal = sensor.readRangeContinuousMillimeters();
setDriveSpeed(SenVal);
Serial.println(SenVal);
/*
Serial.print("Ambient: ");
Serial.print(sensor.readAmbientContinuous());
if (sensor.timeoutOccurred()) { Serial.print(" TIMEOUT"); }
Serial.print("\tRange: ");
Serial.print(sensor.readRangeContinuousMillimeters());
if (sensor.timeoutOccurred()) { Serial.print(" TIMEOUT"); }
Serial.println();
*/
}