-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNPG_Arduino.ino
180 lines (147 loc) · 4.49 KB
/
NPG_Arduino.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
//////////////////////////////////////////////
/// The project for No pain gun.
/// Edit by Yi-chun Hung, 2016/12/23
///
//////////////////////////////////////////////
#include <ArduinoJson.h>
#include <PCD8544.h>
///////////////////////////
/// Define the input and output
///
///////////////////////////
#define trigger A0
#define LED A1
#define Photopin A2
#define DataRequest A3
//////////////////////////
/// LCD module and parameter
/// DO NOT CHANGE!!!!
//////////////////////////
static const byte plyph[] = { B00010000, B00110100, B00110000, B00110100, B00010000 };
static PCD8544 lcd;
///////////////////////////////////////
/// When been shot, photovalue is high
///////////////////////////////////////
int photovalue;
////////////////////////////////////////
/// To control duration of one pulse,
/// Trigger buttom and data request button
/////////////////////////////////////////
int last_time_trigger= 0;
int now_time_trigger = 0;
int last_time_data = 0;
int now_time_data = 0;
/////////////////////////
/// Realize one pulse.
///
/////////////////////////
int DataRequestState = 0;
int NowDataRequestPush = 0;
int LastDataRequestPush = 0;
int push = 0;
int push_last = 0;
////////////////////////////
/// The initial value of JSON
///
///////////////////////////
int const Name = 0;
int Blood = 100;
int Bullet = 100;
int Request = 0;
///////////////////////////
/// Json encoding memory.
/// DO NOT CHANGE
///////////////////////////
StaticJsonBuffer<200> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
void setup()
{
Serial.begin(9600);
Serial1.begin(57600);
pinMode(trigger,INPUT);
pinMode(LED,OUTPUT);
pinMode(DataRequest,OUTPUT);
lcd.begin(84, 48);
lcd.createChar(0,plyph);
////////////////////
/// Structure of Json
///
///////////////////
root["name"] = Name;
root["blood"] = Blood;
root["bullet"] = Bullet;
root["request"] = Request;
}
void loop()
{
push_last = push;
push = digitalRead(trigger);
now_time_trigger = millis();
///////////////////////////
/// Trigger function
///
///////////////////////////
if(push == 0 && push_last == 1){ ///Falling edge trigger
if((now_time_trigger - last_time_trigger) > 300){
///Duration of tigger one pulse
digitalWrite(LED,1); ///Trigger the bullet(lazer).
Bullet--;
root["bullet"] = Bullet; ///Update the bullet number to server
root["request"] = -1; ///Do not request any info
last_time_trigger = now_time_trigger; ///Reset the state of trigger
root.printTo(Serial1); ///Send Json to MT7688
Serial1.print("\n");
}
}
else{
digitalWrite(LED,0); ///No trigger, no bullet
}
//////////////////////////
/// Data request button function
///
//////////////////////////
LastDataRequestPush = NowDataRequestPush;
NowDataRequestPush = digitalRead(DataRequest);
now_time_data = millis();
if(NowDataRequestPush ==0 && LastDataRequestPush ==1){
///Falling edge trigger
if((now_time_data - last_time_data) > 300){ ///Duration of changing request
DataRequestState = (DataRequestState + 1) % 6; ///Update the request
last_time_data = now_time_data;
root["request"] = DataRequestState ; ///Change data of Json
root.printTo(Serial1); ///Send to MT7688
Serial1.print("\n");
////////////////////
/// Encode the Json from MT7688
///
/////////////////////
String str = Serial1.readString();
StaticJsonBuffer<200> jsonBufferLCD;
JsonObject& rootLCD = jsonBufferLCD.parseObject(str);
///////////////////////////
/// Update the LCD info
///
///////////////////////////
lcd.setCursor(0,0); ///First line
lcd.print("Name:");
String nameprint = rootLCD["name"];
lcd.print(nameprint);
lcd.setCursor(0,1); ///Second line
lcd.print("Blood:");
String bloodprint = rootLCD["blood"];
lcd.print(bloodprint);
lcd.setCursor(0,2); ///Third line
lcd.print("Bullet:");
String bulletprint = rootLCD["bullet"];
lcd.print(bulletprint);
delay(10);
}
else{
DataRequestState = DataRequestState;
}
}
else{
DataRequestState = DataRequestState;
}
delay(10);
}