-
Notifications
You must be signed in to change notification settings - Fork 0
/
3D-scanner.ino
149 lines (137 loc) · 2.75 KB
/
3D-scanner.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
140
141
142
143
144
145
146
147
148
149
#include <Stepper.h>
#include <Wire.h>
#include "Adafruit_VL6180X.h"
Adafruit_VL6180X vl = Adafruit_VL6180X();
const int BLUE = 12;
const int GREEN = 7;
const int RED = 6;
const int motorPin1 = 8;
const int motorPin2 = 9;
const int motorPin3 = 10;
const int motorPin4 = 11;
const int in1 = 4;
const int in2 = 3;
const int ena = 2;
const int buttonPin = 5;
const int stepsPerRevolution = 200;
int buttonState = 0;
int lastButtonState = 0;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
int buttonPresses = 0;
Stepper stepper(stepsPerRevolution, motorPin1, motorPin3, motorPin2, motorPin4);
void setup() {
Serial.begin(9600);
vl.begin();
pinMode(buttonPin, INPUT_PULLUP);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(ena, OUTPUT);
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
digitalWrite(RED, HIGH);
digitalWrite(GREEN, LOW);
digitalWrite(BLUE, LOW);
setupScan();
digitalWrite(RED, LOW);
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
digitalWrite(GREEN, HIGH);
}
void motor1() {
Backward();
delay(500);
analogWrite(ena, 0);
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
delay(500);
}
void motor1null() {
Forward();
delay(19975);
digitalWrite(RED, LOW);
analogWrite(ena, 0);
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
digitalWrite(GREEN, HIGH);
}
void xrange() {
digitalWrite(BLUE, HIGH);
const int range = vl.readRange();
Serial.print(range);
Serial.print(" ");
digitalWrite(BLUE, LOW);
}
void motor2() {
for (int m = 0; m < 45; m++) {
Backward2();
xrange();
}
Forward2();
}
void scan() {
digitalWrite(GREEN, LOW);
digitalWrite(RED, HIGH);
for (int s = 0; s < 35; s++) {
motor2();
Serial.println();
motor1();
}
motor2();
Serial.println();
motor1null();
}
void Forward() {
analogWrite(ena, 255);
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
}
void Backward() {
analogWrite(ena, 255);
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
}
void Backward2() {
for (int i = 0; i < 45; i++) {
stepper.step(stepsPerRevolution / 8);
delay(5);
}}
void Forward2() {
for (int f = 0; f < 45; f++) {
for (int i = 0; i < 45; i++) {
stepper.step(-stepsPerRevolution / 8);
delay(5);
}}
}
void setupScan() {
for (int m = 0; m < 45; m++) {
Backward2();
delay(5);
}
Forward2();
delay(5);
}
void loop() {
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == LOW) {
buttonPresses++;
scan();
buttonPresses = 0;
}}}
lastButtonState = reading;
}