這一個範例是想將Arduino上的資料傳送到Processing上去做圖形顯示,

雖然有LabVIEW來顯示圖形是很方便,不過畢竟不是每一台電腦都能灌,LabVIEW需要的程式空間大,有時候筆電灌了會影響其處理速度,

況且我也不想在一台專處理文書的筆電上灌 LabVIEW,因此使用程式體積小又可以跨平台的Processing也許是一種不錯的選擇,

實作的Arduino code:

首先我將參考資料1的部分自己做了一次,使用的是Arduino Uno版,

其Arduino code中的Digital Pin 9是PWM的輸出,

這個範例是利用PWM輸出Sin的波的dutycycle數位訊號,然後借助加一個low pass filter (1k ohm + 1uF)產生Sin波的訊號,

訊號由Analog input端接收,在經由Serial port傳到Processing上呈現,結果如下:

Graph (補)

其中Arduino code要解說的是:

(1)  ISR(TIMER2_OVF_vect)

ISR(Vector) :Interrupt Service Routine 中斷處理

Vector 說明
INT0_vect External Interrupt Request 0
INT1_vect External Interrupt Request 1
PCINT0_vect Pin Change Interrupt Request 0
PCINT1_vect Pin Change Interrupt Request 1
PCINT2_vect Pin Change Interrupt Request 2
WDT_vect Watchdog Time-out Interrupt
TIMER2_COMPA_vect Timer/Counter2 Compare Match A
TIMER2_COMPB_vect Timer/Counter2 Compare Match B
TIMER2_OVF_vect Timer/Counter2 Overflow
TIMER1_CAPT_vect Timer/Counter1 Capture Event
TIMER1_COMPA_vect Timer/Counter1 Compare Match A
TIMER1_COMPB_vect Timer/Counter1 Compare Match B
TIMER1_OVF_vect Timer/Counter1 Overflow
TIMER0_COMPA_vect TimerCounter0 Compare Match A
TIMER0_COMPB_vect TimerCounter0 Compare Match B
TIMER0_OVF_vect Timer/Couner0 Overflow
SPI_STC_vect SPI Serial Transfer Complete
USART_RX_vect USART Rx Complete
USART_UDRE_vect USART, Data Register Empty
USART_TX_vect USART Tx Complete
ADC_vect ADC Conversion Complete
EE_READY_vect EEPROM Ready
ANALOG_COMP_vect Analog Comparator
TWI_vect Two-wire Serial Interface
SPM_READY_vect Store Program Memory Read
(2)資料更新速度
ISR(TIMER2_OVF_vect) {
  TCNT2 = 0x06; // reload timer at 1kHz
  analogWrite(analogOutPin, outputValue[z]);
  sensorValue[z]=analogRead(analogInPin)/8;
  z++;  
  if(z>499) {z=0;frame++; }
}

每500筆資料update一次。

在Processing code中要解說的是:

(1)  fontA = loadFont("SansSerif-10.vlw");

SansSerif-10.vlw 可以google下載,

(2) 將接收的資料用Line()畫出

for (int n=1;n<n_max;n++){
      line(((n-1)*timescaling)+20,255-((2*amplitude*diffValue)-offset),(n*timescaling)+20,255-((2*amplitude*buff.charAt(n))-offset));
      diffValue=buff.charAt(n);
    }

其中 buff.charAt(n)) 的charAt()為回傳字串(string)中指定指標(index)的character,可參考https://processing.org/reference/String_charAt_.html

(3) 座標標示:

text(nf(2.5/amplitude,1,1),2,127+offset);

其中nf()為小數點前後補0的函式,請參考: https://processing.org/reference/nf_.html

接下來我會稍作修改,讀SpO2 sensor的值。

Arduino code:

(To be continued)

Processing code:

(To be continued)

參考資料:

1. Oscilloscope (http://wiki.edwindertien.nl/doku.php?id=software:signalprocessing)

2. Arduino graph software (https://boolscott.wordpress.com/2010/10/23/sneak-preview-of-my-open-source-project/)

3. Realtime Graphing of Accelerometer/Gyroscope Data (http://www.glacialwanderer.com/hobbyrobotics/?p=261)

4. A simple graphing program using Processing (https://barrettsprojects.wordpress.com/2013/01/22/graphing-with-processing/)

5. Graph (https://www.arduino.cc/en/Tutorial/Graph)

創作者介紹
創作者 fishark 的頭像
fishark

魚的方舟 * 圖形語言的世界* - LabVIEW 工作室 & APP開發研究室 & Arduino整合開發 -

fishark 發表在 痞客邦 留言(0) 人氣()