看板 ASM 關於我們 聯絡資訊
請教對這兩個軟體熟悉的大大~ 想請問連接arduino和processing的問題 我想要用兩個感測器去控制processing的圖案 其中一個是超音波感測器,另一個是光敏電阻 而processing也很簡單,只是希望當越接近超音波感測器時,方形會越靠左邊 而當光線值小於某值之後,processing會出現一個圓 但現在問題來了 上網找了連接的程式後 我只知道要怎麼接一個感測器 不知道若要用到兩個感測器時要怎麼寫 以去區分兩種不同的功能@@ ------------------------------------------------- Arduino端如下: const int ping Pin =11; int duration, cm; int photocellPin=2; int photocellVal=0; void setup(){ Serial.begin(9600); } void loop(){ photocellVal=analogRead(photocellPin); //光敏電阻 Serial.write(photocellVal); //這裡要傳一個光敏值 pinMode(pingPin,OUTPUT); //超音波感測器 digitalWrite(pingPin,LOW); delayMicroseconds(2); digitalWrite(pingPin,HIGH); delayMicroseconds(5); digitalWrite(pingPin,LOW); pinMode(pingPin,INPUT); duration=pulseIn(pingPin,HIGH); cm=durartion/74; Serial.write(cm); //這裡也要傳一個超音波值 delay(100); } -------------------------------------------- Processing端如下 import processing.serial.*; Serial.serial; int cm; void setup(){ size(165,200); background(0); serial=new Serial(this,"COM6", 9600); } void draw(){ if(serial.available()>0){ cm=serial.read(); //這裡就出現問題了,好像只能塞得下一種感測器值? background(255); fill(255,0,0); rect(cm,80,50,50); if(photocellVal<20){ fill(0,0,255); ellipse(80,6*cm,60,60); } } } 這樣出來我的Processing的方塊和圓會同時被超音波和光敏值影響@@ 請問要怎麼寫才可以區分@@ 不確定這裡可否問這一類的問題@@ 但我現在真的有點一頭霧水@@ 再請大大幫忙了!感謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.116.32.93
mosquito520:傳送的時候加上特定格式作區別 01/12 04:01
mosquito520:接收的時候根據前面的格式判別現在收的是啥資料 01/12 04:01
p790807:特定格式的意思是.? 有點不是很懂> < 大大可以舉個範例嗎? 01/12 11:11
morewatertw:假設傳送資料時第一筆固定以0x01表示超音波 01/12 16:40
morewatertw:0x02表示光敏電阻,接收端就會知道是哪個感測器的資料 01/12 16:41
morewatertw:根據相對應的資料再分別處理方塊跟圓的動作 01/12 16:43