-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArduinoSnakeOLED.ino
More file actions
135 lines (123 loc) · 2.95 KB
/
ArduinoSnakeOLED.ino
File metadata and controls
135 lines (123 loc) · 2.95 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
/*
Name: ArduinoSnakeOLED.ino
Created: 1/4/2021 10:27:51 PM
Author: abiyy
*/
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "SnakeElement.h"
//#define USE_SERIAL 1
#define USE_BUTTONS 1
long lastTime;
int duration = 50;
int Radius = 2;
Adafruit_SSD1306 display(OLED_WIDTH, OLED_HIGH, &Wire, 4);
SnakeElement mSnake(OLED_WIDTH / 2 + 8 * Radius, OLED_HIGH / 2, Radius);
SnakeDirection Direction = left;
//Circle Food(OLED_WIDTH / 2 + 8 * Radius, OLED_HIGH / 2, Radius / 2);
void setup() {
#ifdef USE_BUTTONS
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP);
#endif
randomSeed(analogRead(0));
Serial.begin(9600);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
pinMode(13, OUTPUT);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
digitalWrite(13, HIGH);
for (;;); // Don't proceed, loop forever
}
else {
digitalWrite(13, LOW);
}
// Show initial display buffer contents on the screen --
// the library initializes this with an Adafruit splash screen.
display.display();
delay(1000); // Pause for 1 seconds
display.clearDisplay();
mSnake.draw();
display.display();
for (int i = 0; i < 4; i++) {
mSnake.update(left);
mSnake.elongate();
}
display.clearDisplay();
mSnake.draw();
//randomFoodPosition();
//Food.draw();
display.display();
Direction = stop;
lastTime = millis();
}
void loop() {
checkDirection();
Serial.println(Direction);
if ((millis() - lastTime) >= duration) {
display.clearDisplay();
/*if (mSnake.eating(Food)) {
Serial.println("True");
mSnake.elongate();
//randomFoodPosition();
}*/
mSnake.update(Direction);
mSnake.draw();
//Food.draw();
display.display();
lastTime = millis();
}
}
int checkDirection() {
#ifdef USE_SERIAL
if (Serial.available()) {
char buffer = Serial.read();
switch (buffer) {
case '4':
Direction = left;
break;
case '6':
Direction = right;
break;
case '8':
Direction = up;
break;
case '2':
Direction = down;
break;
case '5':
Direction = stop;
break;
default:
break;
}
}
#endif
#ifdef USE_BUTTONS
int a = digitalRead(4);
int b = digitalRead(5);
int c = digitalRead(6);
int d = digitalRead(7);
if (a == 0) {
Direction = right;
}
if (b == 0) {
Direction = left;
}
if (c == 0) {
Direction = up;
}
if (d == 0) {
Direction = down;
}
#endif
}
/*void randomFoodPosition() {
int x = random(OLED_WIDTH);
int y = random(OLED_HIGH);
Food.setX(x);
Food.setY(y);
}*/