SlideShare a Scribd company logo
1 of 29
Download to read offline
Arduinoで遊ぼう 
#1 ArduinoとProcessingを連携する 
2014.10.09 
Created by Sannomiy Yasunori
今回やること 
センサ値 
Arduino Processing 
センサ値表現
今回やること 
センサ値 
Arduino Processing 
センサ値表現 
ArduinoとProcessingの連携を行います
データ 
Arduino Processing 
データ 
シリアル通信 
作戦 
今回はシリアル通信を使った同期を行います
シリアル通信とは? 
Arduino USBケーブルProcessing 
データ 
USBケーブル上でデータを送受信してるような 
イメージで良いと思います
注意! 
Arduino USBケーブルProcessing 
データデータ 
同時にデータを送るとデータが衝突します
シリアル通信とは? 
Arduino Processing 
データ 
Arduino Processing 
データ 
データは一歩通行。交互に送るようにしましょう。
A STEP1. P 
通信開始の合図 
A P 
A P 
A P 
STEP2. 
STEP3. 
STEP4. 
USB 
データを空にする 
センサの値 
USB 
データを空にする 
ProcessingがArduino 
送信するように合図を送ります 
合図を受信したら、シリアルポート上 
のデータを空にします 
その後、センサ値を送信します 
センサ値を受信したら、データを 
空にします 
アルゴリズムの解説
A STEP1. P 
通信開始の合図 
A P 
A P 
A P 
STEP2. 
STEP3. 
STEP4. 
USB 
データを空にする 
センサの値 
USB 
データを空にする 
ProcessingがArduinoにセンサ値を 
送信するように合図を送ります 
合図を受信したら、シリアルポート上 
のデータを空にします 
その後、センサ値を送信します 
センサ値を受信したら、データを 
空にします
A STEP1. P 
通信開始の合図 
A P 
A P 
A P 
STEP2. 
STEP3. 
STEP4. 
USB 
データを空にする 
センサの値 
USB 
データを空にする 
ProcessingがArduino 
送信するように合図を送ります 
合図を受信したら、シリアルポート上 
のデータを空にします 
その後、センサ値を送信します 
センサ値を受信したら、データを 
空にします
A STEP1. P 
通信開始の合図 
A P 
A P 
A P 
STEP2. 
STEP3. 
STEP4. 
USB 
データを空にする 
センサの値 
USB 
データを空にする 
ProcessingがArduino 
送信するように合図を送ります 
合図を受信したら、シリアルポート上 
のデータを空にします 
その後、センサ値を送信します 
センサ値を受信したら、データを 
空にします
A STEP1. P 
通信開始の合図 
A P 
A P 
A P 
STEP2. 
STEP3. 
STEP4. 
USB 
データを空にする 
センサの値 
USBケーブル上の 
データを空にする 
ProcessingがArduino 
送信するように合図を送ります 
合図を受信したら、シリアルポート上 
のデータを空にします 
その後、センサ値を送信します 
センサ値を受信したら、データを 
空にします
A STEP1. P 
通信開始の合図 
以下繰り返し。これを超高速にやって同期通信をするよ。 
A P 
A P 
A P 
STEP2. 
STEP3. 
STEP4. 
USB 
データを空にする 
センサの値 
USB 
データを空にする 
ProcessingがArduinoにセンサ値を 
送信するように合図を送ります 
合図を受信したら、シリアルポート上 
のデータを空にします 
その後、センサ値を送信します 
センサ値を受信したら、データを 
空にします
ソースコード解説 
あまり長くないから頑張れ!
int val = 0; // 送信したい値 
!void setup(){ 
Serial.begin(9600); // シリアル通信を開始する 
} 
!void loop(){ 
if(Serial.available() > 0){ // データが到着していれば 
Serial.read(); // シリアルポート上のデータを空にする 
Serial.write(val++); // シリアルポートにデータを送信 
} 
delay(100); 
} 
① 
②③④
覚えてほしい4つのメソッド 
Serial.begin(9600); シリアル通信を開始するよっていう命令。 
9600は通信速度。基本いじらなくてよい。 
void setup()に記述しよう。 
その1
覚えてほしい4つのメソッド 
Serial.available() シリアルポート上に何バイトのデータが到着して 
いるかを調べる命令。 
if(Serial.available()>0) で1バイト以上のデータが 
到着したかを判断できる。 
合図がきたかどうかを判断するために使う。 
その2
覚えてほしい4つのメソッド 
Serial.read(); シリアルポート上にあるデータを取得して 
空にする命令。 
シリアルポート上のデータを空にするために使う。 
その3
覚えてほしい4つのメソッド 
Serial.write(値) 引数の値をシリアルポート上に送信する命令。 
数値を送信するときは1バイトづつ送信する。 
データを送信するために使う。 
0~255までの数って意味だぜ
int val = 0; // 送信したい値 
!void setup(){ 
Serial.begin(9600); // シリアル通信を開始する 
} 
!void loop(){ 
if(Serial.available() > 0){ // データが到着していれば 
Serial.read(); // シリアルポート上のデータを空にする 
Serial.write(val++); // シリアルポートにデータを送信 
} 
delay(100); 
} 
① 
②③④
import processing.serial.*; // ライブラリのインポート 
Serial myPort; 
int val=0; 
!void setup(){ 
size(300,400); 
myPort = new Serial(this, "/dev/tty.usbmodem1411", 9600); 
} 
void draw(){ 
if(keyPressed) if(key=='s') myPort.write(255); 
background(255); 
fill(0); 
text(val, 10,20); 
} 
void serialEvent(Serial p){ 
if(myPort.available()>0){ 
val = myPort.read(); 
myPort.write(255); 
} 
} 
① 
② 
③ 
④
Processingでのポイント 
import processing.serial.*; 
Serial myPort; 
シリアル通信をするライブラリがあるので 
インポートする。 
シリアル通信系の命令を使えるようにする 
ために必要な記述。コピペでいいよ。 
インスタンス名は 
何でも良いよ 
その1
Processingでのポイント 
その2 
Windowsの例 
myPort = new Serial(this, "COM4", 9600); 
Macの例 
myPort = new Serial(this, "/dev/tty.usbmodem1411", 9600); 
シリアル通信をするための設定。 
ポートの名前が人によって違うため、 
各自で名前を確認する必要があります。
Processingでのポイント 
if(keyPressed) if(key=='s') myPort.write(255); 
書き方は任せる。最初の合図を送る必要があります。 
1バイトのデータならなんでもいいよ
Processingでのポイント 
void serialEvent(Serial p){ 
} 
シリアルイベントを管理する関数。 
シリアル通信のための命令はこの関数の 
中に記述する。
import processing.serial.*; // ライブラリのインポート 
Serial myPort; 
int val=0; 
!void setup(){ 
size(300,400); 
myPort = new Serial(this, "/dev/tty.usbmodem1411", 9600); 
} 
void draw(){ 
if(keyPressed) if(key=='s') myPort.write(255); 
background(255); 
fill(0); 
text(val, 10,20); 
} 
void serialEvent(Serial p){ 
if(myPort.available()>0){ 
val = myPort.read(); 
myPort.write(255); 
} 
} 
① 
② 
③ 
④
A P 
通信開始の合図 
A P 
USBケーブル上の 
データを空にする 
A P 
センサの値 
A P 
USBケーブル上の 
データを空にする 
まとめ 
Serial.write(255); 
1.Processingから何かデータを送信 
if(Serial.available()>0){ 
2.Arduino側に1バイト以上のデータがきたら 
! 
Serial.Read(); 
4.シリアルポート上のデータを空にして 
! 
Serial.Write(センサ値); 
} 
5.データを送信する 
int val = Serial.Read(); 
if(Serial.available()>0){ 
6.受信したデータを変数に保存し、 
 シリアルポートを空にする
Arduinoで遊ぼう 
#1 ArduinoとProcessingを連携する 
終わり
次回予告? 
Arduinoで遊ぼう 
#2 Arduinoでフォトリフレクタを使おう 
#3 リクエスト受付中

More Related Content

Recently uploaded

Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介Yuma Ohgami
 
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)Hiroki Ichikura
 
スマートフォンを用いた新生児あやし動作の教示システム
スマートフォンを用いた新生児あやし動作の教示システムスマートフォンを用いた新生児あやし動作の教示システム
スマートフォンを用いた新生児あやし動作の教示システムsugiuralab
 
SOPを理解する 2024/04/19 の勉強会で発表されたものです
SOPを理解する       2024/04/19 の勉強会で発表されたものですSOPを理解する       2024/04/19 の勉強会で発表されたものです
SOPを理解する 2024/04/19 の勉強会で発表されたものですiPride Co., Ltd.
 
論文紹介:Semantic segmentation using Vision Transformers: A survey
論文紹介:Semantic segmentation using Vision Transformers: A survey論文紹介:Semantic segmentation using Vision Transformers: A survey
論文紹介:Semantic segmentation using Vision Transformers: A surveyToru Tamaki
 
TSAL operation mechanism and circuit diagram.pdf
TSAL operation mechanism and circuit diagram.pdfTSAL operation mechanism and circuit diagram.pdf
TSAL operation mechanism and circuit diagram.pdftaisei2219
 
論文紹介:Automated Classification of Model Errors on ImageNet
論文紹介:Automated Classification of Model Errors on ImageNet論文紹介:Automated Classification of Model Errors on ImageNet
論文紹介:Automated Classification of Model Errors on ImageNetToru Tamaki
 
Postman LT Fukuoka_Quick Prototype_By Daniel
Postman LT Fukuoka_Quick Prototype_By DanielPostman LT Fukuoka_Quick Prototype_By Daniel
Postman LT Fukuoka_Quick Prototype_By Danieldanielhu54
 
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...Toru Tamaki
 
[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略
[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略
[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略Ryo Sasaki
 

Recently uploaded (10)

Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介
 
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
 
スマートフォンを用いた新生児あやし動作の教示システム
スマートフォンを用いた新生児あやし動作の教示システムスマートフォンを用いた新生児あやし動作の教示システム
スマートフォンを用いた新生児あやし動作の教示システム
 
SOPを理解する 2024/04/19 の勉強会で発表されたものです
SOPを理解する       2024/04/19 の勉強会で発表されたものですSOPを理解する       2024/04/19 の勉強会で発表されたものです
SOPを理解する 2024/04/19 の勉強会で発表されたものです
 
論文紹介:Semantic segmentation using Vision Transformers: A survey
論文紹介:Semantic segmentation using Vision Transformers: A survey論文紹介:Semantic segmentation using Vision Transformers: A survey
論文紹介:Semantic segmentation using Vision Transformers: A survey
 
TSAL operation mechanism and circuit diagram.pdf
TSAL operation mechanism and circuit diagram.pdfTSAL operation mechanism and circuit diagram.pdf
TSAL operation mechanism and circuit diagram.pdf
 
論文紹介:Automated Classification of Model Errors on ImageNet
論文紹介:Automated Classification of Model Errors on ImageNet論文紹介:Automated Classification of Model Errors on ImageNet
論文紹介:Automated Classification of Model Errors on ImageNet
 
Postman LT Fukuoka_Quick Prototype_By Daniel
Postman LT Fukuoka_Quick Prototype_By DanielPostman LT Fukuoka_Quick Prototype_By Daniel
Postman LT Fukuoka_Quick Prototype_By Daniel
 
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...
 
[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略
[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略
[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略
 

Featured

Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...DevGAMM Conference
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationErica Santiago
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellSaba Software
 

Featured (20)

Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
 

Arduinoで遊ぼう #1Processingと連携する

  • 1. Arduinoで遊ぼう #1 ArduinoとProcessingを連携する 2014.10.09 Created by Sannomiy Yasunori
  • 2. 今回やること センサ値 Arduino Processing センサ値表現
  • 3. 今回やること センサ値 Arduino Processing センサ値表現 ArduinoとProcessingの連携を行います
  • 4. データ Arduino Processing データ シリアル通信 作戦 今回はシリアル通信を使った同期を行います
  • 5. シリアル通信とは? Arduino USBケーブルProcessing データ USBケーブル上でデータを送受信してるような イメージで良いと思います
  • 6. 注意! Arduino USBケーブルProcessing データデータ 同時にデータを送るとデータが衝突します
  • 7. シリアル通信とは? Arduino Processing データ Arduino Processing データ データは一歩通行。交互に送るようにしましょう。
  • 8. A STEP1. P 通信開始の合図 A P A P A P STEP2. STEP3. STEP4. USB データを空にする センサの値 USB データを空にする ProcessingがArduino 送信するように合図を送ります 合図を受信したら、シリアルポート上 のデータを空にします その後、センサ値を送信します センサ値を受信したら、データを 空にします アルゴリズムの解説
  • 9. A STEP1. P 通信開始の合図 A P A P A P STEP2. STEP3. STEP4. USB データを空にする センサの値 USB データを空にする ProcessingがArduinoにセンサ値を 送信するように合図を送ります 合図を受信したら、シリアルポート上 のデータを空にします その後、センサ値を送信します センサ値を受信したら、データを 空にします
  • 10. A STEP1. P 通信開始の合図 A P A P A P STEP2. STEP3. STEP4. USB データを空にする センサの値 USB データを空にする ProcessingがArduino 送信するように合図を送ります 合図を受信したら、シリアルポート上 のデータを空にします その後、センサ値を送信します センサ値を受信したら、データを 空にします
  • 11. A STEP1. P 通信開始の合図 A P A P A P STEP2. STEP3. STEP4. USB データを空にする センサの値 USB データを空にする ProcessingがArduino 送信するように合図を送ります 合図を受信したら、シリアルポート上 のデータを空にします その後、センサ値を送信します センサ値を受信したら、データを 空にします
  • 12. A STEP1. P 通信開始の合図 A P A P A P STEP2. STEP3. STEP4. USB データを空にする センサの値 USBケーブル上の データを空にする ProcessingがArduino 送信するように合図を送ります 合図を受信したら、シリアルポート上 のデータを空にします その後、センサ値を送信します センサ値を受信したら、データを 空にします
  • 13. A STEP1. P 通信開始の合図 以下繰り返し。これを超高速にやって同期通信をするよ。 A P A P A P STEP2. STEP3. STEP4. USB データを空にする センサの値 USB データを空にする ProcessingがArduinoにセンサ値を 送信するように合図を送ります 合図を受信したら、シリアルポート上 のデータを空にします その後、センサ値を送信します センサ値を受信したら、データを 空にします
  • 15. int val = 0; // 送信したい値 !void setup(){ Serial.begin(9600); // シリアル通信を開始する } !void loop(){ if(Serial.available() > 0){ // データが到着していれば Serial.read(); // シリアルポート上のデータを空にする Serial.write(val++); // シリアルポートにデータを送信 } delay(100); } ① ②③④
  • 16. 覚えてほしい4つのメソッド Serial.begin(9600); シリアル通信を開始するよっていう命令。 9600は通信速度。基本いじらなくてよい。 void setup()に記述しよう。 その1
  • 17. 覚えてほしい4つのメソッド Serial.available() シリアルポート上に何バイトのデータが到着して いるかを調べる命令。 if(Serial.available()>0) で1バイト以上のデータが 到着したかを判断できる。 合図がきたかどうかを判断するために使う。 その2
  • 18. 覚えてほしい4つのメソッド Serial.read(); シリアルポート上にあるデータを取得して 空にする命令。 シリアルポート上のデータを空にするために使う。 その3
  • 19. 覚えてほしい4つのメソッド Serial.write(値) 引数の値をシリアルポート上に送信する命令。 数値を送信するときは1バイトづつ送信する。 データを送信するために使う。 0~255までの数って意味だぜ
  • 20. int val = 0; // 送信したい値 !void setup(){ Serial.begin(9600); // シリアル通信を開始する } !void loop(){ if(Serial.available() > 0){ // データが到着していれば Serial.read(); // シリアルポート上のデータを空にする Serial.write(val++); // シリアルポートにデータを送信 } delay(100); } ① ②③④
  • 21. import processing.serial.*; // ライブラリのインポート Serial myPort; int val=0; !void setup(){ size(300,400); myPort = new Serial(this, "/dev/tty.usbmodem1411", 9600); } void draw(){ if(keyPressed) if(key=='s') myPort.write(255); background(255); fill(0); text(val, 10,20); } void serialEvent(Serial p){ if(myPort.available()>0){ val = myPort.read(); myPort.write(255); } } ① ② ③ ④
  • 22. Processingでのポイント import processing.serial.*; Serial myPort; シリアル通信をするライブラリがあるので インポートする。 シリアル通信系の命令を使えるようにする ために必要な記述。コピペでいいよ。 インスタンス名は 何でも良いよ その1
  • 23. Processingでのポイント その2 Windowsの例 myPort = new Serial(this, "COM4", 9600); Macの例 myPort = new Serial(this, "/dev/tty.usbmodem1411", 9600); シリアル通信をするための設定。 ポートの名前が人によって違うため、 各自で名前を確認する必要があります。
  • 24. Processingでのポイント if(keyPressed) if(key=='s') myPort.write(255); 書き方は任せる。最初の合図を送る必要があります。 1バイトのデータならなんでもいいよ
  • 25. Processingでのポイント void serialEvent(Serial p){ } シリアルイベントを管理する関数。 シリアル通信のための命令はこの関数の 中に記述する。
  • 26. import processing.serial.*; // ライブラリのインポート Serial myPort; int val=0; !void setup(){ size(300,400); myPort = new Serial(this, "/dev/tty.usbmodem1411", 9600); } void draw(){ if(keyPressed) if(key=='s') myPort.write(255); background(255); fill(0); text(val, 10,20); } void serialEvent(Serial p){ if(myPort.available()>0){ val = myPort.read(); myPort.write(255); } } ① ② ③ ④
  • 27. A P 通信開始の合図 A P USBケーブル上の データを空にする A P センサの値 A P USBケーブル上の データを空にする まとめ Serial.write(255); 1.Processingから何かデータを送信 if(Serial.available()>0){ 2.Arduino側に1バイト以上のデータがきたら ! Serial.Read(); 4.シリアルポート上のデータを空にして ! Serial.Write(センサ値); } 5.データを送信する int val = Serial.Read(); if(Serial.available()>0){ 6.受信したデータを変数に保存し、  シリアルポートを空にする
  • 29. 次回予告? Arduinoで遊ぼう #2 Arduinoでフォトリフレクタを使おう #3 リクエスト受付中