-
Notifications
You must be signed in to change notification settings - Fork 1
/
Functions.pde
110 lines (99 loc) · 3.47 KB
/
Functions.pde
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
// improved code - does not underflow
// may want to set a minimum pwm for moptors so they don't stall
void pwm (int motorPin, int dutyCycle, int repeats)
{
int i;
// limit dutyCycle to 1 - 100 range
int cycle1 = max (1, min (dutyCycle, 1023)); //(dutyCycle > 1023) ? 1023 : dutyCycle;
int cycle2 = 1024 - cycle1; // (cycle1 < 1) ? 1 : 1024 - cycle1;
for (i = 1; i <= repeats; i++) {
digitalWrite ( motorPin, HIGH);
delayMicroseconds (cycle1);
digitalWrite ( motorPin, LOW);
delayMicroseconds (cycle2);
}
}
// run both motors in parallel using PWM
// add a noBump arg to enable (0) or disable (1) bump detection
// use a minimum pulst of 10 us (TBD) to get motors turning over
void pwm2 (int motorPin1, int dutyCycle1, int motorPin2, int dutyCycle2, int repeats, boolean doBump)
{
// limit dutyCycle to 1 - 100 range
int i;
int pulse1On = max (10, min (dutyCycle1, 1023)); //(dutyCycle > 1023) ? 1023 : dutyCycle;
int pulse1Off = 1024 - dutyCycle1; // (cycle1 < 1) ? 1 : 1024 - cycle1;
int pulse2On = max (10, min (dutyCycle2, 1023)); //(dutyCycle > 1023) ? 1023 : dutyCycle;
int pulse2Off = 1024 - dutyCycle2; // (cycle1 < 1) ? 1 : 1024 - cycle1;
for (i = 1; i <= repeats; i++) {
if (doBump && readBump()) {
bumpDetected = true; // remember the bump
break; }
digitalWrite ( motorPin1, HIGH);
digitalWrite ( motorPin2, HIGH);
if ( pulse1On < pulse2On ) {
delayMicroseconds (pulse1On);
digitalWrite ( motorPin1, LOW);
delayMicroseconds ( pulse2On - pulse1On +1 );
digitalWrite ( motorPin2, LOW);
delayMicroseconds ( 1025 - pulse1On); }
else {
delayMicroseconds (pulse2On);
digitalWrite ( motorPin2, LOW);
delayMicroseconds ( pulse1On - pulse2On +1);
digitalWrite ( motorPin1, LOW);
delayMicroseconds ( 1025 - pulse2On); }
}
}
// utility function for vibrator motor
void vib (int power, int duration) {
pwm (vibPin, power, duration);
}
// utility function for vibrator motor
void vibLoud (int duration) {
digitalWrite (vibPin, HIGH);
delay (duration * 4);
digitalWrite (vibPin, LOW);
}
// utility pulse function
void pulse (int pin, int time) {
int halfPulse = time >>1;
digitalWrite (pin, HIGH);
delay (halfPulse);
digitalWrite (pin, LOW);
delay (halfPulse);
}
boolean readBump () {
if (digitalRead(bumpPin) == 0) {
restCounter = 0; // global
return true;
}
else return false;
}
boolean readPhone () {
if ((analogRead(phonePin) > phoneSensitivity))
return true;
else return false;
}
void step (int stepNumber) {
for (int i=0; i<stepNumber; i++) {pulse (ledPinRed, 1000);} // STEP
}
// Piezo Buzz
void piezo (int period, int duration) {
int i;
// piezo 500Hz buzz test
for (i = 0; i < duration; i++) {
digitalWrite(piezoPin, HIGH); // sets the LED on
delayMicroseconds(period);
digitalWrite(piezoPin, LOW); // sets the LED on
delayMicroseconds(period);
}
}
//
void coo () {
for (int i = 200; i < 500; i += 5) piezo (i, 5);
for (int i = 500; i > 200; i -= 5) piezo (i, 5);
for (int i=0; i<3; i++) {
delay (100);
for (int i = 100; i < 500; i += 20) piezo (i, 5);
}
}