質問するログイン新規登録

質問編集履歴

1

書式の改善

2018/05/28 08:00

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -67,4 +67,206 @@
67
67
  コードのつもりです、、、
68
68
 
69
69
  本当に慣れてないのでコードがめちゃくちゃだと思います、、、
70
- すみませんがどなたかお力を貸していただけないでしょうか・・・。
70
+ すみませんがどなたかお力を貸していただけないでしょうか・・・。
71
+
72
+ _____________________________
73
+
74
+ y_waiwai様 、MasahikoHirata様
75
+ 本当にご返答ありがとうございます!
76
+
77
+ MasahikoHirata様のコードをお借りして、実行してみたのですが 
78
+ なかなか上手くいきません、、。
79
+ もう少し調べてみようと思います、、
80
+
81
+ ↓ 現在のコードです。
82
+
83
+ 追記
84
+ ```C++
85
+ //Arduino
86
+ #define in A0
87
+ #define H 1
88
+ #define L 0
89
+ int mode;
90
+
91
+ void setup() {
92
+ Serial.begin(115200);
93
+ pinMode(in, INPUT);
94
+ mode = L;
95
+ }
96
+
97
+ void loop() {
98
+ int status;
99
+ status = digitalRead(in);
100
+ if (status == H) {
101
+ if (mode == L) {
102
+ mode = H;
103
+ Serial.print("L");
104
+ } else {
105
+ if (mode == H) {
106
+ mode = L;
107
+ Serial.print("H");
108
+ }
109
+
110
+ }
111
+ }
112
+
113
+ }
114
+ ```
115
+
116
+
117
+ ```C#
118
+ //unity側 、ボタンを押すとアニメーションが変わる
119
+ using UnityEngine;
120
+ using System.Collections;
121
+ using System.Collections.Generic;
122
+
123
+
124
+ public class animation : MonoBehaviour
125
+ {
126
+
127
+ private Animator animator;
128
+
129
+ private const string key_isRun = "isRun";
130
+ private const string key_isJump = "isJump";
131
+
132
+ // Use this for initialization
133
+
134
+ void Start()
135
+ {
136
+ this.animator = GetComponent<Animator>();
137
+ }
138
+
139
+ // Update is called once per frame
140
+ void Update()
141
+ {
142
+ if (Input.GetKey(KeyCode.H))
143
+ {
144
+ this.animator.SetBool(key_isRun, true);
145
+
146
+
147
+ }
148
+ else
149
+ {
150
+ this.animator.SetBool(key_isRun, false);
151
+
152
+ }
153
+
154
+ if (Input.GetKey(KeyCode.L))
155
+ {
156
+
157
+ this.animator.SetBool(key_isJump, true);
158
+
159
+ }
160
+ else
161
+ {
162
+ this.animator.SetBool(key_isJump, false);
163
+
164
+
165
+ }
166
+ }
167
+ }
168
+ ```
169
+ ```C#
170
+ //unity側でシリアル通信をするためのコード
171
+ using UnityEngine;
172
+ using System.Collections;
173
+ using System.IO.Ports;
174
+ using System.Threading;
175
+
176
+ public class SerialHandler : MonoBehaviour
177
+ {
178
+ public delegate void SerialDataReceivedEventHandler(string message);
179
+ public event SerialDataReceivedEventHandler OnDataReceived;
180
+
181
+ public string portName = "/dev/cu.usbmodem14141";
182
+ public int baudRate = 115200;
183
+
184
+ private SerialPort serialPort_;
185
+ private Thread thread_;
186
+ private bool isRunning_ = false;
187
+
188
+ private string message_;
189
+ private bool isNewMessageReceived_ = false;
190
+
191
+ void Awake()
192
+ {
193
+ Open();
194
+ }
195
+
196
+ void Update()
197
+ {
198
+ if (isNewMessageReceived_)
199
+ {
200
+ OnDataReceived(message_);
201
+ }
202
+ isNewMessageReceived_ = false;
203
+ }
204
+
205
+ void OnDestroy()
206
+ {
207
+ Close();
208
+ }
209
+
210
+ private void Open()
211
+ {
212
+ serialPort_ = new SerialPort(portName, baudRate, Parity.None, 8, StopBits.One);
213
+
214
+ serialPort_.Open();
215
+
216
+ isRunning_ = true;
217
+
218
+ thread_ = new Thread(Read);
219
+ thread_.Start();
220
+ }
221
+
222
+ private void Close()
223
+ {
224
+ isNewMessageReceived_ = false;
225
+ isRunning_ = false;
226
+
227
+ if (thread_ != null && thread_.IsAlive)
228
+ {
229
+ thread_.Join();
230
+ }
231
+
232
+ if (serialPort_ != null && serialPort_.IsOpen)
233
+ {
234
+ serialPort_.Close();
235
+ serialPort_.Dispose();
236
+ }
237
+ }
238
+
239
+ private void Read()
240
+ {
241
+ while (isRunning_ && serialPort_ != null && serialPort_.IsOpen)
242
+ {
243
+ try
244
+ {
245
+ message_ = serialPort_.ReadLine();
246
+ isNewMessageReceived_ = true;
247
+ }
248
+ catch (System.Exception e)
249
+ {
250
+ Debug.LogWarning(e.Message);
251
+ }
252
+ }
253
+ }
254
+
255
+ public void Write(string message)
256
+ {
257
+ try
258
+ {
259
+ serialPort_.Write(message);
260
+ }
261
+ catch (System.Exception e)
262
+ {
263
+ Debug.LogWarning(e.Message);
264
+ }
265
+ }
266
+ }
267
+
268
+
269
+ ```
270
+
271
+ unity側でアニメーションを再生するコードとarduinoからの信号を受け取るコードを
272
+ 別に分けて書きました。