|
本帖最后由 幻生幻灭 于 2012-9-13 23:24 编辑
新手发帖请多指教。
以前不会写Android程序,直到我找到了这个: Amarino
http://www.amarino-toolkit.net/index.php/amarino-api.html
就用这个库写成了Android客户端,使用蓝牙通讯。当然这是写给和我一样不会写Android的人用的,高手就不用看了。
该客户端可以发送 上下左右方向指令、控制速度指令、两个动作指令,界面是6个按钮一个滑动条。
文件在这里:(一个Android源码,一个Android安装程序,还有一个Arduino端程序示范)
http://code.google.com/p/arduino-android-boxz/downloads/list
使用起来很简单,下载Android 的apk文件安装,打开后输入你的Arduino蓝牙模块的mac地址,确认,就开始控制了。
Arduino端的程序程序可以参考这个:(你只要把你的控制速度,转向,动作的命令在对应的地方加进去就行了)
- /*
- MarioBox (works with Amarino and the MarioBox app)
-
- - based on the Amarino Multicolor Lamp tutorial
- - receives events from Android.
-
- author: Bert Zhang - August 2012
- */
-
- #include <MeetAndroid.h>
- // declare MeetAndroid so that you can call functions with it
- MeetAndroid meetAndroid;
- // set the speed of the motor
- int speed = 0;
- int action;
- // currentstate reflects the current direction of the box
- int currentstate = 1;
- const int forwardstate = 1;
- const int leftstate = 2;
- const int rightstate = 3;
- const int backstate = 9;
- void setup()
- {
- // my bluetooth module is configured to this baud
- Serial.begin(9600);
-
- // register callback functions, which will be called when an associated event occurs.
- meetAndroid.registerFunction(changespeed, 'p');
- meetAndroid.registerFunction(goforward, 'w');
- meetAndroid.registerFunction(turnleft, 'a');
- meetAndroid.registerFunction(turnright, 'd');
- meetAndroid.registerFunction(goback, 's');
- meetAndroid.registerFunction(actionA, 'A');
- meetAndroid.registerFunction(actionB, 'B');
- // set led 13 as an indicator light
- pinMode(13,OUTPUT);
- }
- void loop()
- {
- // you need to keep this in your loop() to receive events
- meetAndroid.receive();
- }
- /*
- * Whenever the MarioBox app changes the speed
- * this function will be called
- */
- void changespeed(byte flag, byte numOfValues)
- {
- //get speed value from Anriod
- speed = meetAndroid.getInt();
- //indicate
- flash();
- }
- /*
- * Whenever the MarioBox app changes the direction
- * these functions will be called
- */
- void goforward(byte flag, byte numOfValues)
- {
- action = meetAndroid.getInt();
- switch(action){
- case 1:
- /*
- 向前走的命令
- */
- break;
- case 0:
- /*
- 停止命令
- */
- break;
- default:break;
- }
- currentstate = forwardstate;
- //indicate
- flash();
- }
- void turnleft(byte flag, byte numOfValues)
- {
- action = meetAndroid.getInt();
- switch(action){
- case 1:
- /*
- 向左拐的命令
- */
- break;
- case 0:
- /*
- 停止命令
- */
- break;
- default:break;
- }
- //update state
- currentstate = leftstate;
- //indicate
- flash();
- }
- void turnright(byte flag, byte numOfValues)
- {
- action = meetAndroid.getInt();
- switch(action){
- case 1:
- /*
- 向右转的命令
- */
- break;
- case 0:
- /*
- 停止命令
- */
- break;
- default:break;
- }
- //update state
- currentstate = rightstate;
- //indicate
- flash();
- }
- void goback(byte flag, byte numOfValues)
- {
- action = meetAndroid.getInt();
- switch(action){
- case 1:
- /*
- 向后退的命令
- */
- break;
- case 0:
- /*
- 停止命令
- */
- break;
- default:break;
- }
- //update state
- currentstate = backstate;
- //indicate
- flash();
- }
- void actionA(byte flag, byte numOfValues)
- {
- /*
- 动作1命令
- */
- flash();
- }
- void actionB(byte flag, byte numOfValues)
- {
- /*
- 动作2命令
- */
- flash();
- }
- //LED onboard flashes indicates event received
- void flash()
- {
- digitalWrite(13,HIGH);
- delay(100);
- digitalWrite(13,LOW);
- delay(100);
- }
复制代码 |
评分
-
查看全部评分
|