質問編集履歴

1

書式の改善

2018/05/28 08:00

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -137,3 +137,407 @@
137
137
  本当に慣れてないのでコードがめちゃくちゃだと思います、、、
138
138
 
139
139
  すみませんがどなたかお力を貸していただけないでしょうか・・・。
140
+
141
+
142
+
143
+ _____________________________
144
+
145
+
146
+
147
+ y_waiwai様 、MasahikoHirata様
148
+
149
+ 本当にご返答ありがとうございます!
150
+
151
+
152
+
153
+ MasahikoHirata様のコードをお借りして、実行してみたのですが 
154
+
155
+ なかなか上手くいきません、、。
156
+
157
+ もう少し調べてみようと思います、、
158
+
159
+
160
+
161
+ ↓ 現在のコードです。
162
+
163
+
164
+
165
+ 追記
166
+
167
+ ```C++
168
+
169
+ //Arduino
170
+
171
+ #define in A0
172
+
173
+ #define H 1
174
+
175
+ #define L 0
176
+
177
+ int mode;
178
+
179
+
180
+
181
+ void setup() {
182
+
183
+ Serial.begin(115200);
184
+
185
+ pinMode(in, INPUT);
186
+
187
+ mode = L;
188
+
189
+ }
190
+
191
+
192
+
193
+ void loop() {
194
+
195
+ int status;
196
+
197
+ status = digitalRead(in);
198
+
199
+ if (status == H) {
200
+
201
+ if (mode == L) {
202
+
203
+ mode = H;
204
+
205
+ Serial.print("L");
206
+
207
+ } else {
208
+
209
+ if (mode == H) {
210
+
211
+ mode = L;
212
+
213
+ Serial.print("H");
214
+
215
+ }
216
+
217
+
218
+
219
+ }
220
+
221
+ }
222
+
223
+
224
+
225
+ }
226
+
227
+ ```
228
+
229
+
230
+
231
+
232
+
233
+ ```C#
234
+
235
+ //unity側 、ボタンを押すとアニメーションが変わる
236
+
237
+ using UnityEngine;
238
+
239
+ using System.Collections;
240
+
241
+ using System.Collections.Generic;
242
+
243
+
244
+
245
+
246
+
247
+ public class animation : MonoBehaviour
248
+
249
+ {
250
+
251
+
252
+
253
+ private Animator animator;
254
+
255
+
256
+
257
+ private const string key_isRun = "isRun";
258
+
259
+ private const string key_isJump = "isJump";
260
+
261
+
262
+
263
+ // Use this for initialization
264
+
265
+
266
+
267
+ void Start()
268
+
269
+ {
270
+
271
+ this.animator = GetComponent<Animator>();
272
+
273
+ }
274
+
275
+
276
+
277
+ // Update is called once per frame
278
+
279
+ void Update()
280
+
281
+ {
282
+
283
+ if (Input.GetKey(KeyCode.H))
284
+
285
+ {
286
+
287
+ this.animator.SetBool(key_isRun, true);
288
+
289
+
290
+
291
+
292
+
293
+ }
294
+
295
+ else
296
+
297
+ {
298
+
299
+ this.animator.SetBool(key_isRun, false);
300
+
301
+
302
+
303
+ }
304
+
305
+
306
+
307
+ if (Input.GetKey(KeyCode.L))
308
+
309
+ {
310
+
311
+
312
+
313
+ this.animator.SetBool(key_isJump, true);
314
+
315
+
316
+
317
+ }
318
+
319
+ else
320
+
321
+ {
322
+
323
+ this.animator.SetBool(key_isJump, false);
324
+
325
+
326
+
327
+
328
+
329
+ }
330
+
331
+ }
332
+
333
+ }
334
+
335
+ ```
336
+
337
+ ```C#
338
+
339
+ //unity側でシリアル通信をするためのコード
340
+
341
+ using UnityEngine;
342
+
343
+ using System.Collections;
344
+
345
+ using System.IO.Ports;
346
+
347
+ using System.Threading;
348
+
349
+
350
+
351
+ public class SerialHandler : MonoBehaviour
352
+
353
+ {
354
+
355
+ public delegate void SerialDataReceivedEventHandler(string message);
356
+
357
+ public event SerialDataReceivedEventHandler OnDataReceived;
358
+
359
+
360
+
361
+ public string portName = "/dev/cu.usbmodem14141";
362
+
363
+ public int baudRate = 115200;
364
+
365
+
366
+
367
+ private SerialPort serialPort_;
368
+
369
+ private Thread thread_;
370
+
371
+ private bool isRunning_ = false;
372
+
373
+
374
+
375
+ private string message_;
376
+
377
+ private bool isNewMessageReceived_ = false;
378
+
379
+
380
+
381
+ void Awake()
382
+
383
+ {
384
+
385
+ Open();
386
+
387
+ }
388
+
389
+
390
+
391
+ void Update()
392
+
393
+ {
394
+
395
+ if (isNewMessageReceived_)
396
+
397
+ {
398
+
399
+ OnDataReceived(message_);
400
+
401
+ }
402
+
403
+ isNewMessageReceived_ = false;
404
+
405
+ }
406
+
407
+
408
+
409
+ void OnDestroy()
410
+
411
+ {
412
+
413
+ Close();
414
+
415
+ }
416
+
417
+
418
+
419
+ private void Open()
420
+
421
+ {
422
+
423
+ serialPort_ = new SerialPort(portName, baudRate, Parity.None, 8, StopBits.One);
424
+
425
+
426
+
427
+ serialPort_.Open();
428
+
429
+
430
+
431
+ isRunning_ = true;
432
+
433
+
434
+
435
+ thread_ = new Thread(Read);
436
+
437
+ thread_.Start();
438
+
439
+ }
440
+
441
+
442
+
443
+ private void Close()
444
+
445
+ {
446
+
447
+ isNewMessageReceived_ = false;
448
+
449
+ isRunning_ = false;
450
+
451
+
452
+
453
+ if (thread_ != null && thread_.IsAlive)
454
+
455
+ {
456
+
457
+ thread_.Join();
458
+
459
+ }
460
+
461
+
462
+
463
+ if (serialPort_ != null && serialPort_.IsOpen)
464
+
465
+ {
466
+
467
+ serialPort_.Close();
468
+
469
+ serialPort_.Dispose();
470
+
471
+ }
472
+
473
+ }
474
+
475
+
476
+
477
+ private void Read()
478
+
479
+ {
480
+
481
+ while (isRunning_ && serialPort_ != null && serialPort_.IsOpen)
482
+
483
+ {
484
+
485
+ try
486
+
487
+ {
488
+
489
+ message_ = serialPort_.ReadLine();
490
+
491
+ isNewMessageReceived_ = true;
492
+
493
+ }
494
+
495
+ catch (System.Exception e)
496
+
497
+ {
498
+
499
+ Debug.LogWarning(e.Message);
500
+
501
+ }
502
+
503
+ }
504
+
505
+ }
506
+
507
+
508
+
509
+ public void Write(string message)
510
+
511
+ {
512
+
513
+ try
514
+
515
+ {
516
+
517
+ serialPort_.Write(message);
518
+
519
+ }
520
+
521
+ catch (System.Exception e)
522
+
523
+ {
524
+
525
+ Debug.LogWarning(e.Message);
526
+
527
+ }
528
+
529
+ }
530
+
531
+ }
532
+
533
+
534
+
535
+
536
+
537
+ ```
538
+
539
+
540
+
541
+ unity側でアニメーションを再生するコードとarduinoからの信号を受け取るコードを
542
+
543
+ 別に分けて書きました。