-
Notifications
You must be signed in to change notification settings - Fork 0
/
_3D_control_2.ino
109 lines (86 loc) · 2.08 KB
/
_3D_control_2.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
//
// By Kyle McDonald
// Modified by Dallin Briggs
// From the instructables project at:
// http://www.instructables.com/id/DIY-3D-Controller/
#define resolution 8
#define mains 60 // 60: north america, japan; 50: most other places
#include <AFMotor.h>
AF_DCMotor motor4(4);
AF_DCMotor motor3(3);
AF_DCMotor motor2(2);
#define refresh 2 * 1000000 / mains
void setup()
{
Serial.begin(9600);
for(int i = 9; i < 12; i++)
pinMode(i, INPUT);
startTimer();
motor4.setSpeed(200);
}
int i;
double median, upper, lower;
long starttime, t, minimum = 100000, maximum;
void loop()
{
starttime = millis();
// Prints time since program started.
Serial.print(starttime);
Serial.print(" ");
// Serial.print(time(9, B00000010), DEC);
// Serial.print(" ");
Serial.print(time(10, B00000100), DEC);
Serial.print(" ");
Serial.print(t);
Serial.print(" ");
Serial.print(minimum);
Serial.print(" ");
Serial.print(lower);
Serial.print(" ");
Serial.print(median);
Serial.print(" ");
Serial.print(upper);
Serial.print(" ");
Serial.println(maximum);
// Serial.print(" ");
// Serial.println(time(11, B00001000), DEC);
t = time(10, B00000100);
if(t > maximum)
maximum = t;
if(minimum > t)
minimum = t;
median = (maximum + minimum)/2;
upper = median + .25*(maximum - minimum);
lower = median - .25*(maximum - minimum);
if (t > upper)
motor4.run(FORWARD);
else
motor4.run(RELEASE);
if (t < lower)
motor4.run(BACKWARD);
}
long time(int pin, byte mask)
{
unsigned long count = 0, total = 0;
while(checkTimer() < refresh)
{
// pinMode is about 6 times slower than assigning
// DDRB directly, but that pause is important
pinMode(pin, OUTPUT);
PORTB = 0;
pinMode(pin, INPUT);
while((PINB & mask) == 0)
count++;
total++;
}
startTimer();
return (count << resolution) / total;
}
extern volatile unsigned long timer0_overflow_count;
void startTimer() {
timer0_overflow_count = 0;
TCNT0 = 0;
}
unsigned long checkTimer() {
return ((timer0_overflow_count << 8) + TCNT0) << 2;
}