昨天由 android 成功送出控制 arduino LED blink , 今天我們要利用 arduino 的 PIN 11 來當作 PWM 輸出,
並拿 PWM 控制 LED 的亮度,首先我們需要:
然後我們看 android java code 吧,首先是要在 UI 上變出 seekbar 方便我們控制 PWM 的輸出數值
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.test.MainActivity" >
<!---
上略
新增宣告 SeekBar ,然後宣告一個 TextView 來顯示 SeekBar 資料,
這邊要注意seekbar 要設定 max 值,剛好為我們 pwm max 255
--->
<SeekBar
android:id="@+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/UpateRate_Orien"
android:layout_centerInParent="true"
android:max="255" />
<TextView
android:id="@+id/seekBarValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/seekBar1"
android:layout_below="@+id/seekBar1"
android:text="" />
</RelativeLayout>
在 MainActivity.java 中:
package com.example.test;
import java.io.IOException;
import org.shokai.firmata.ArduinoFirmata;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.SeekBar; // 使用 SeekBar 必要
import android.widget.SeekBar.OnSeekBarChangeListener; // SeekBar listener 相關
import com.MyTestLibrary.CountingSensorUpateRate;
public class MainActivity extends ActionBarActivity{
private boolean timerHasStarted = false ;
private boolean ArduinoBlink = false ;
private Button startB;
private TestCounting CountDownTimer;
private TextView AccelX;
private TextView AccelY;
private TextView AccelZ;
private TextView OrienX;
private TextView OrienY;
private TextView OrienZ;
private SensorActivity MySensor;
private TextView TimeView;
private TextView ElapsedView;
private TextView CountAccel,CountOrien;
private TextView seekBarValue;
private long timeElapsed;
private long startTime = 60*1000;
private long interval = 1*1000; // 1 sec. per time interval
private CountingSensorUpateRate MyCountingSensorUpdateRate;
private SeekBar PWMValue;
private ArduinoFirmata arduino;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startB = (Button) this.findViewById(R.id.button1);
startB.setOnClickListener(new Button_Listener(this));
AccelX = (TextView) this.findViewById(R.id.AccelX);
AccelY = (TextView) this.findViewById(R.id.AccelY);
AccelZ = (TextView) this.findViewById(R.id.AccelZ);
OrienX = (TextView) this.findViewById(R.id.OrienX);
OrienY = (TextView) this.findViewById(R.id.OrienY);
OrienZ = (TextView) this.findViewById(R.id.OrienZ);
TimeView= (TextView) this.findViewById(R.id.timer);
ElapsedView= (TextView) this.findViewById(R.id.timeElapsed);
CountAccel = (TextView) this.findViewById(R.id.UpateRate_Accel);
CountOrien = (TextView) this.findViewById(R.id.UpateRate_Orien);
seekBarValue = (TextView) this.findViewById(R.id.seekBarValue);
PWMValue = (SeekBar) this.findViewById(R.id.seekBar1);
CountDownTimer = new TestCounting (startTime,interval);
MyCountingSensorUpdateRate = new CountingSensorUpateRate();
MySensor = new SensorActivity();
MySensor.onPause();
this.arduino = new ArduinoFirmata(this);
// 設定 SeekBar 的 onchange event
PWMValue.setOnSeekBarChangeListener( new OnSeekBarChangeListener() {
int progressValue = 0;
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
//今天我們隻需要用到 onProgressChanged 就好,
progressValue = progress;
seekBarValue.setText(String.valueOf(progressValue));
arduino.analogWrite(11, progressValue); // pinNumber, value(0~255) ,把 SeekBar 收到的數值,原封不動的送給 arduino analogWrite 即可,其中 11 是 PIN Number
// TODO Auto-generated method stub
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
//seekBarValue.setText(String.valueOf(progressValue));
}
});
}
/*
下略
*/
}
撰寫完上述的code 之後,我們把它送到 android device 中測試,要記得我們的 AVD 是不支援 USB device connect 的喔,所以一定要準備實體測試機器才行。
把 android 跟 arduino 接上後,會自動啟動對應的 app , 這邊我們按下 [Start Counting] 後,就可以手動調整 SeekBar 來測試 PWM 輸出了
今天到這邊告一段落
我們明天見