-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsenderCodeIPD.ino
More file actions
160 lines (132 loc) · 4.06 KB
/
senderCodeIPD.ino
File metadata and controls
160 lines (132 loc) · 4.06 KB
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
150
151
152
153
154
155
156
157
158
159
160
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
char prevChrState = 'm'; //determines direciton "eyes" look
// 'r' for right, 'l' for left, 'm' for middle
//this is used to give instrucitons to servo motors
char chrState;
// proximity sensor parameters
int trigPin = 3;
int echoPin = 2;
int trigPin2 = 6;
int echoPin2 = 5;
int distance;
int distance2;
int duration;
int duration2;
int prevDist;
int handDistance;
int handDistance2;
const int buttonPin = 10; //button pin
void setup() {
Serial.begin(57600);
Serial.println('Ready');
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
pinMode(buttonPin, INPUT);
// put your setup code here, to run once:
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Don't proceed, loop forever
}
}
void loop() {
if (digitalRead(buttonPin) == HIGH) {
Serial.write('m');
display.clearDisplay();
display.fillRoundRect(16, 0, 96, 96, 16, WHITE);
display.display();
delay(100);
display.clearDisplay();
display.fillRoundRect(16, 12, 96, 96, 16, WHITE);
display.display();
delay(100);
display.clearDisplay();
display.fillRoundRect(16, 24, 96, 96, 16, WHITE);
display.display();
delay(100);
display.clearDisplay();
display.fillRoundRect(16, 36, 96, 96, 16, WHITE);
display.display();
delay(100);
display.clearDisplay();
display.fillRoundRect(16, 48, 96, 96, 16, WHITE);
display.display();
delay(100);
display.clearDisplay();
display.fillRoundRect(16, 60, 96, 96, 16, WHITE);
display.display();
delay(100);
display.clearDisplay();
display.display();
delay(5000);
}
handDistance = calculateDist();
handDistance2 = calculateDist2();
//Serial.println(handDistance);
//Serial.println(handDistance2);
if (handDistance <= 10 and handDistance2 <= 10) {
display.clearDisplay();
display.drawRoundRect(24, 0 , 80, 80, 16, WHITE);
display.display();
Serial.write('m');
}
else if ( handDistance <= 10 and handDistance2 > 10) {
display.clearDisplay();
display.fillRoundRect(0, 0, 80, 80, 16, WHITE);
display.display();
Serial.write('l');
}
else if (handDistance > 10 and handDistance2 <= 10) {
display.clearDisplay();
display.fillRoundRect(48, 0, 80, 80, 16, WHITE);
display.display();
Serial.write('r');
}
else {
display.clearDisplay();
display.fillRoundRect(16, 0, 96, 96, 16, WHITE);
display.display();
Serial.write('m');
}
}
char receiveChar() {
if (Serial.available() > 0) {
char receivedChar = Serial.read();
return receivedChar;
}
}
int calculateDist()
{
// Clears the trigPin condition
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)
return distance;
}
int calculateDist2()
{
// Clears the trigPin condition
digitalWrite(trigPin2, LOW);
delayMicroseconds(2);
// Sets the trigPin HIGH (ACTIVE) for 10 microseconds
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin2, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration2 = pulseIn(echoPin2, HIGH);
// Calculating the distance
distance2 = duration2 * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)
return distance2;
}