-
Notifications
You must be signed in to change notification settings - Fork 0
/
bluetooth_with_oled_display.ino
209 lines (190 loc) · 5.74 KB
/
bluetooth_with_oled_display.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include <AFMotor.h>
#include <SoftwareSerial.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "eyes.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)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
SoftwareSerial bluetoothSerial(9, 10); // RX, TX
AF_DCMotor motor3(3, MOTOR34_1KHZ); // initial motors pin
AF_DCMotor motor4(4, MOTOR34_1KHZ);
char command;
void setup() {
bluetoothSerial.begin(9600); // Set the baud rate to your Bluetooth module
Serial.begin(9600);
Serial.println("Booting");
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3c)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for (;;) {
}
}
display.clearDisplay();
display.display();
}
unsigned char readkey(char input) {
unsigned char ret = 0;
if (input == 'm') {
ret = 1;
} else if (input == 'l') {
ret += 2;
} else if (input == 'r') {
ret += 4;
}
return ret;
}
int xp = 16;
int mood = 0;
void forward() {
motor3.setSpeed(255); // Define maximum velocity
motor3.run(FORWARD); // rotate the motor clockwise
motor4.setSpeed(255); // Define maximum velocity
motor4.run(FORWARD); // rotate the motor clockwise
}
void back() {
motor3.setSpeed(255); // Define maximum velocity
motor3.run(BACKWARD); // rotate the motor anti-clockwise
motor4.setSpeed(255); // Define maximum velocity
motor4.run(BACKWARD); // rotate the motor anti-clockwise
}
void left() {
motor3.setSpeed(255); // Define maximum velocity
motor3.run(BACKWARD); // rotate the motor clockwise
motor4.setSpeed(255); // Define maximum velocity
motor4.run(FORWARD); // rotate the motor clockwise
}
void right() {
motor3.setSpeed(255); // Define maximum velocity
motor3.run(FORWARD); // rotate the motor anti-clockwise
motor4.setSpeed(255); // Define maximum velocity
motor4.run(BACKWARD); // rotate the motor anti-clockwise
}
void topleft() {
motor3.setSpeed(180); // Define maximum velocity
motor3.run(FORWARD); // rotate the motor clockwise
motor4.setSpeed(255); // Define maximum velocity
motor4.run(FORWARD); // rotate the motor clockwise
}
void topright() {
motor3.setSpeed(255); // Define maximum velocity
motor3.run(FORWARD); // rotate the motor clockwise
motor4.setSpeed(180); // Define maximum velocity
motor4.run(FORWARD); // rotate the motor clockwise
}
void bottomleft() {
motor3.setSpeed(180); // Define maximum velocity
motor3.run(BACKWARD); // rotate the motor anti-clockwise
motor4.setSpeed(255); // Define maximum velocity
motor4.run(BACKWARD); // rotate the motor anti-clockwise
}
void bottomright() {
motor3.setSpeed(255); // Define maximum velocity
motor3.run(BACKWARD); // rotate the motor anti-clockwise
motor4.setSpeed(180); // Define maximum velocity
motor4.run(BACKWARD); // rotate the motor anti-clockwise
}
void Stop() {
motor3.setSpeed(0); // Define minimum velocity
motor3.run(RELEASE); // stop the motor when releasing the button
motor4.setSpeed(0); // Define minimum velocity
motor4.run(RELEASE); // stop the motor when releasing the button
}
void loop() {
int n;
if (bluetoothSerial.available() > 0) {
command = bluetoothSerial.read();
Stop(); // initialize with motors stopped
Serial.println(command);
switch (command) {
case 'F':
forward();
break;
case 'B':
back();
break;
case 'L':
left();
break;
case 'R':
right();
break;
case 'G':
topleft();
break;
case 'I':
topright();
break;
case 'J':
bottomright();
break;
case 'H':
bottomleft();
break;
}
static int xd = 4;
static int wait = 0;
static int step = 0;
int x1, x2;
if (wait > 0) {
wait--;
delay(1);
} else {
x1 = xd + (xp > 16 ? (16 + 2 * (xp - 16)) : xp);
x2 = 64 + xd + (xp < 16 ? (-16 + (xp * 2)) : xp);
switch (step) {
case 0:
display.clearDisplay();
if (xp < 6) { // if eyes to the left
display.drawBitmap(x1, 18, peyes[mood][2][0], 32, 32, WHITE);
display.drawBitmap(x2, 18, peyes[mood][1][1], 32, 32, WHITE);
} else if (xp < 26) { // if eyes to the right
display.drawBitmap(x1, 18, peyes[mood][0][0], 32, 32, WHITE);
display.drawBitmap(x2, 18, peyes[mood][0][1], 32, 32, WHITE);
} else { // if normal
display.drawBitmap(x1, 18, peyes[mood][1][0], 32, 32, WHITE);
display.drawBitmap(x2, 18, peyes[mood][2][1], 32, 32, WHITE);
}
display.display();
wait = random(250, 1000);
n = random(0, 7);
if (n == 6) {
step = 1;
} else {
step = 2;
}
break;
case 1:
display.clearDisplay();
display.drawBitmap(x1, 8, eye0, 32, 32, WHITE);
display.drawBitmap(x2, 8, eye0, 32, 32, WHITE);
display.display();
wait = 100;
step = 0;
break;
case 2:
n = random(0, 10);
if (n < 5) xd--;
if (n >= 5) xd++;
if (xd < -4) xd = -3;
if (xd > 4) xd = 3;
wait = 0;
step = 0;
break;
}
}
if (command == 'L') n = readkey('l');
if (command == 'R') n = readkey('r');
if (command == 'W') n = readkey('m');
if (n == 2) xp = (xp <= 0 ? 0 : xp - 1);
if (n == 4) xp = (xp >= 32 ? 32 : xp + 1);
if (command == 'W') {
mood = (mood >= 5 ? 0 : mood + 1);
}
if (n != 0) {
wait = 0;
step = 0;
}
}
}