-
Notifications
You must be signed in to change notification settings - Fork 0
/
project_module_1st_attempt.ino
159 lines (144 loc) · 3.45 KB
/
project_module_1st_attempt.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
#include <Wire.h>
#include <LiquidCrystal.h>
#define BUTTON_PIN 19
#define OUT_PIN 24
#define IN_PIN 25
#define LED_PIN 13
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
int ledValue;
volatile int buttonValue;
int prev_x = -1;
int prev_s = -1;
int initial = 1;
int readed = 0;
int gameOn = 1;
int direct = 1;
int line = 0;
int dlay = 200;
int x = 7;
int s = 0;
int next_x = 0;
int next_s = 0;
char block[8];
int enable = 0;
void setup() {
Serial.begin(9600);
Wire.begin(9);
Wire.onReceive(receiveEvent);
lcd.begin(16, 2);
lcd.clear();
for(int i = 0; i < 8; i++)
block[i] = 'z'+144;
pinMode(OUT_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(IN_PIN, INPUT_PULLUP);
digitalWrite(OUT_PIN, LOW);
ledValue = 0;
attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), buttonInterrupt, FALLING);
enable = digitalRead(IN_PIN);
}
void loop() {
if(enable == 1 || readed == 1){
if (initial == 1){
buttonValue = 0;
if(readed == 1){
Serial.print("Was here!");
x = prev_x;
s = prev_s;
dlay = 50;
}
else
dlay = 200;
initial = 0;
line = 0;
}
if(buttonValue == 0 && gameOn == 1){
lcd.setCursor(x, line);
lcd.print(" ");
x += direct;
if(x + 8 - s== 16){
direct = -1;
}
if(x == 0){
direct = 1;
}
lcd.setCursor(x, line);
lcd.print(block + s);
delay(dlay);
}
else if (buttonValue == 1 && gameOn == 1)
{
buttonValue = 0;
if(line == 0)
{
dlay = 100;
line = 1;
if(prev_x != -1 && prev_s != -1){
if(x != prev_x){
if(x + (8-s) <= prev_x || prev_x + (8-prev_s) <= x){
lcd.setCursor(x, line);
lcd.print("Game Over!");
gameOn = 0;
}
else{
if(x > prev_x)
s = s + x - prev_x;
else
s = s + prev_x - x;
}
prev_x = x;
prev_s = s;
}
}
}
else
{
gameOn = 0;
if(x != prev_x){
if(x + (8-s) <= prev_x || prev_x + (8-prev_s) <= x){
lcd.setCursor(0, line);
lcd.print(" ");
Serial.print("Game Over!");
lcd.setCursor(0, line);
lcd.print("Game Over!");
gameOn = 0;
}
else{
if(x > prev_x)
s = s + x - prev_x -2;
else
s = s + prev_x - x -2;
next_x = x;
next_s = s;
Serial.print("Game On!");
sendEvent();
delay(1000);
digitalWrite(OUT_PIN, HIGH);
lcd.setCursor(0, line);
lcd.print(" ");
lcd.setCursor(x, line);
lcd.print(block + s);
}
}
}
}
}
}
void buttonInterrupt()
{
if(buttonValue == 0)
buttonValue = 1;
}
void receiveEvent(int bytes) {
prev_s = Wire.read(); //val_x_s & 15;
prev_x = Wire.read(); //val_x_s >> 4;
readed = 1;
}
void sendEvent(){
int val_x_s_2 = next_x << 4 + next_s & 15;
Wire.beginTransmission(9);
Wire.write(next_s);
Wire.write(next_x);
Wire.endTransmission();
}