2019-02-20 10:19:16 jiyotin 阅读数 757
  • arduino实战

    学习如何在arduino中使用各种传感器,包括人体红外传感器,超声波传感器,舵机控制,温湿度传感器,激光接收传感器等,空气质量传感器,wifi模块等....课程内容会不断的更新增加,只要发现比较有趣的传感器就会做对应的实战视频

    420 人正在学习 去看看 陈贤能

有没有想过用智能手机控制任何电子设备?今天我将展示如何做到这一点
在这里插入图片描述
控制实现详情,以及本文Android App下载,Arduino代码,点击此链接关注国外课栈网“平台创客:Android”专栏
在这里插入图片描述在这里插入图片描述

2014-04-06 20:38:47 zgc261 阅读数 18970
  • arduino实战

    学习如何在arduino中使用各种传感器,包括人体红外传感器,超声波传感器,舵机控制,温湿度传感器,激光接收传感器等,空气质量传感器,wifi模块等....课程内容会不断的更新增加,只要发现比较有趣的传感器就会做对应的实战视频

    420 人正在学习 去看看 陈贤能

蓝牙控制arduino的安卓软件(基于app inventor )


本文章软件下载:基于app inventor的蓝牙控制arduino安卓客户端


最近在无线电杂志上看到关于app inventor 的内容,赶脚很厉害,从图书馆回来后就开始尝试,发现真的不错。因为之前在用arduino,所以我想能能不能用app inventor写软件通过WIFI或者蓝牙控制arduino,所以就有了这篇文章,下面开始介绍:

功能描述:1、手机控制arduino上pin13灯的亮灭(可以使其他灯,也可以是其他单片机)。


首先介绍软件的制作过程:

1、登陆 http://appinventor.mit.edu/explore/ 来在线制作软件(也可以下载离线版制作);

我们可以在首页新建应用


然后进行操作。软件的制作分为两部分:界面设计和“组装代码”,我们可以通过右上角(下图)的两个按钮进行切换:

界面设计是我们制作应用时首先看到的界面:


在左侧是常见的按钮,标签什么的(和VB类似),可以拖拉进中间的设计框,拉过来的都将显示在右侧,最右侧是原件的属性,我们也可以删除不用的原件。

代码组装界面如下:


类似的,最左边是代码原件,可以直接拉到框内进行组装代码。

我们在设计应用界面和代码时可以通过三种方式来实时了解功能,其中最常用的是WIFI和安卓手机的搭配,也可以用数据线和手机当然如果没有手机,也可以使用仿真。可以在左上方选择方式:


例如,选择第一个WIFI加手机后会出现二维码和代码,随便选一种方式进行连接:


基本上软件设计就这几部。具体怎么设计可以参见百度文库的教程(共4个小时),看完之后你会有基本的了解。

App Inventor 入门基础教程  地址:http://wenku.baidu.com/course/view/1ea9ae45b307e87101f6968d


下面就具体简介本软件的制作:
1、首先是界面设计,将所有需要的控件全部拉到框内,然后在右侧设置对应的属性。

2、首先初始化软件(就是刚进入软件回做什么)

其实就是声明了一个全局变量,变量名字叫做 OK;
下面是弹出一个框(就是那种出现后几秒钟后消失的那种提示),上面的内容是“欢迎使用本软件!@zgc261”。

3、选择蓝牙设备:

第一句是,选择前,将配对的蓝牙地址组成数组列表,以供选择;
第二句是,选择后将选择的地址赋给全局NAME;

4、下面开始连接选择的蓝牙地址:

功能描述:
如果已经连接的话,则点击之后断开连接;
若没有连接,则点击之后进行连接,并显示相关提示;
连接成功后,会显示OK;

5、测试:

两个按键的功能是:亮和灭,其实就是通过蓝牙向单片机发送了A和B(字符),然后在单片机断进行解析。

这样软件基本就做好了,可以打包下载了。



谢谢大家。

-----------------------------------------------------------------------------------------------------------------------------
大家可以参考

http://www.appinventor.com.cn/forum.php?mod=viewthread&tid=3242

2015-09-29 20:08:31 u014115577 阅读数 5253
  • arduino实战

    学习如何在arduino中使用各种传感器,包括人体红外传感器,超声波传感器,舵机控制,温湿度传感器,激光接收传感器等,空气质量传感器,wifi模块等....课程内容会不断的更新增加,只要发现比较有趣的传感器就会做对应的实战视频

    420 人正在学习 去看看 陈贤能

一直以来,Android蓝牙控制arduino机器人自走小车这个项目困惑我好久了,以前把机器人小车的蓝牙地址写死了。现在随着android学习的深入,终于把这个项目全部完善完成了。从andrroid端APP的开发到机器人小车的设计全部统统搞定,现在我具体介绍一下我的项目:

1.机器人端

用到了L298N驱动模块,板子用到的是arduino最小系统板子,蓝牙芯片用到的是HC-05;

工作原理:手机app端蓝牙和机器人端蓝牙建立链接,通过app端蓝牙发送一个字元,机器人蓝牙接收到这个字元以后作出相应的动作。

下面是arduino最小系统板子烧录的程序

<pre name="code" class="cpp">#include <SoftwareSerial.h>
SoftwareSerial BT(10,11);


int MotorLeft1=6;
int MotorLeft2=7;
int MotorRight1=8;
int MotorRight2=9;
char val;
  
void setup()
{  
  BT.begin(9600);
  pinMode(MotorLeft1,  OUTPUT);  // 引脚 6 (PWM) 
  pinMode(MotorLeft2,  OUTPUT);  // 引脚  7 (PWM)
  pinMode(MotorRight1, OUTPUT);  // 引脚 8 (PWM)
  pinMode(MotorRight2, OUTPUT);  // 引脚 9 (PWM)

}

void go()// 前进
{
        digitalWrite(MotorRight1,LOW);
        digitalWrite(MotorRight2,HIGH);
        digitalWrite(MotorLeft1,LOW);
        digitalWrite(MotorLeft2,HIGH);
       
}

void left() //左转
{
      digitalWrite(MotorRight1,HIGH);
      digitalWrite(MotorRight2,LOW);
      digitalWrite(MotorLeft1,LOW);
      digitalWrite(MotorLeft2,HIGH);
  
}
void right() //右转
{
      digitalWrite(MotorRight1,LOW);
      digitalWrite(MotorRight2,HIGH);
      digitalWrite(MotorLeft1,HIGH);
      digitalWrite(MotorLeft2,LOW);
     
} 
void STOP() //停止
{
     digitalWrite(MotorRight1,LOW);
     digitalWrite(MotorRight2,LOW);
     digitalWrite(MotorLeft1,LOW);
     digitalWrite(MotorLeft2,LOW);
    
}
void back() //后退
{
        digitalWrite(MotorRight1,HIGH);
        digitalWrite(MotorRight2,LOW);
        digitalWrite(MotorLeft1,HIGH);
        digitalWrite(MotorLeft2,LOW);;
      
}
        
void loop()
{
  if(BT.available() )
  {
  val = BT.read();
    if (val =='W')
    {
      go();
      delay(500);
    }
    else if (val == 'A')
    {
      left();
      delay(500);
    }
    else if (val == 'D')
    {
      right();
      delay(500);
    }
    else if (val == 'S')
    {
      back();
      delay(500);
    }
    else if (val == 'Q')
    STOP();
  }
} 



现在是手机APP端的开发:

首先让手机打开蓝牙功能,然后收索附近的蓝牙设备,把收索到的蓝牙设备的名称和地址显示在ListView上,给ListView设置一个点击时间的监听器,当点击ListView上对应的蓝牙设备,通过SharedPreferences把点击的蓝牙设备address存起来,同时也通过Intent 跳入机器人小车的控制页面,进入机器人小车的控制页面通过取得SharedPreferences里面存的机器人蓝牙的地址,通过BluetoothSocket建立蓝牙链接就可以进行蓝牙之间的通信了;

Android代码如下:

package com.lanzhu1993.bluthtooth;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnItemClickListener
{
	private static final String TAG = "Main";
	private ListView lvDevices;
	private BluetoothAdapter bluetoothAdapter;
	private List<String> bluetoothDevices = new ArrayList<String>();
	private ArrayAdapter<String> arrayAdapter;
	private BluetoothDevice device;
	private SharedPreferences sp;


	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		setContentView(R.layout.activity_main);
		bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

		lvDevices = (ListView) findViewById(R.id.list_search);
		
		sp = getSharedPreferences("config", MODE_PRIVATE);

		Set<BluetoothDevice> pairedDevices = bluetoothAdapter
				.getBondedDevices();

		if (pairedDevices.size() > 0)
		{
			for (BluetoothDevice device : pairedDevices)
			{
				bluetoothDevices.add(device.getName() + ":"
						+ device.getAddress() + "\n");
			}
		}
		arrayAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, android.R.id.text1,
				bluetoothDevices);

		lvDevices.setAdapter(arrayAdapter);
		lvDevices.setOnItemClickListener(this);
		IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
		this.registerReceiver(receiver, filter);

		filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
		this.registerReceiver(receiver, filter);


	}

	public void onClick_Search(View view)
	{
		setProgressBarIndeterminateVisibility(true);
		setTitle("正在扫描...");

		if (bluetoothAdapter.isDiscovering())
		{
			bluetoothAdapter.cancelDiscovery();
		}
		bluetoothAdapter.startDiscovery();
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id)
	{
		String s = arrayAdapter.getItem(position);
		String address = s.substring(s.indexOf(":") + 1).trim();
		
		Log.v(TAG, "-------------->"+address);
		Toast.makeText(MainActivity.this, address, 1).show();
		Editor editor = sp.edit();
		editor.putString("address", address);
		editor.commit();
		Intent intent = new Intent(MainActivity.this,Lanyakongzhi.class);
		startActivity(intent);
		finish();


	}

	private final BroadcastReceiver receiver = new BroadcastReceiver()
	{
		@Override
		public void onReceive(Context context, Intent intent)
		{
			String action = intent.getAction();

			if (BluetoothDevice.ACTION_FOUND.equals(action))
			{

				BluetoothDevice device = intent
						.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

				if (device.getBondState() != BluetoothDevice.BOND_BONDED)
				{
					bluetoothDevices.add(device.getName() + ":"
							+ device.getAddress() + "\n");
					arrayAdapter.notifyDataSetChanged();
				}

			}
			else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action))
			{
				setProgressBarIndeterminateVisibility(false);
				setTitle("连接蓝牙设备");

			}
		}
	};


}

控制页面的代码

package com.lanzhu1993.bluthtooth;

import android.app.Activity;
import android.os.Bundle;
import java.io.IOException;
import java.io.OutputStream;
import java.util.UUID;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class Lanyakongzhi extends Activity {
    private static final String TAG = "BLUEZ_CAR";
    private static final boolean D = true;
    private BluetoothAdapter mBluetoothAdapter = null;
    private BluetoothSocket btSocket = null;
    private OutputStream outStream = null;
    private SharedPreferences sp;
    private String address;
    
    Button mButtonF;
    Button mButtonB;
    Button mButtonL;
    Button mButtonR;
    Button mButtonS;
    private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
//    Intent intent = new Intent();
////    String result = intent.getStringExtra("textViewLabel");
////    String address = result; 
   // private static String address = "result" ; // <==要连接的蓝牙设备MAC地址
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);                
    setContentView(R.layout.kongzhi);
    
    sp = getSharedPreferences("config", MODE_PRIVATE);
    address =  sp.getString("address", null);
    if(TextUtils.isEmpty(address)){
    	Toast.makeText(Lanyakongzhi.this, "蓝牙地址为空", 1).show();
    }
//    //获取蓝牙数据
//    Intent intent = new Intent();
//    address = intent.getStringExtra("BTname");
//    Toast.makeText(Lanyakongzhi.this, address, Toast.LENGTH_SHORT).show();
    //前进
    mButtonF=(Button)findViewById(R.id.btnF);
    mButtonF.setOnTouchListener(new Button.OnTouchListener(){
    @Override
    public boolean onTouch(View v, MotionEvent event) {
     // TODO Auto-generated method stub
     String message;
     byte[] msgBuffer;
     int action = event.getAction();
     switch(action)
     {
     case MotionEvent.ACTION_DOWN:
     try {
           outStream = btSocket.getOutputStream();
          } catch (IOException e) {
              Log.e(TAG, "ON RESUME: Output stream creation failed.", e);
          }
          message = "W";
         msgBuffer = message.getBytes();
          try {
           outStream.write(msgBuffer);
          } catch (IOException e) {
                  Log.e(TAG, "ON RESUME: Exception during write.", e);
          }
     break;
     
     case MotionEvent.ACTION_UP:
      try {
        outStream = btSocket.getOutputStream();

       } catch (IOException e) {
           Log.e(TAG, "ON RESUME: Output stream creation failed.", e);
       }
       message = "0";

       msgBuffer = message.getBytes();

       try {
        outStream.write(msgBuffer);

       } catch (IOException e) {
               Log.e(TAG, "ON RESUME: Exception during write.", e);
       }
      break;
     }
     return false;
    }
    
    });
	          //后退
	mButtonB=(Button)findViewById(R.id.btnB);
	mButtonB.setOnTouchListener(new Button.OnTouchListener(){

   
    @Override
    public boolean onTouch(View v, MotionEvent event) {
     // TODO Auto-generated method stub
     String message;
     byte[] msgBuffer;
     int action = event.getAction();
     switch(action)
     {
     case MotionEvent.ACTION_DOWN:
     try {
           outStream = btSocket.getOutputStream();

          } catch (IOException e) {
              Log.e(TAG, "ON RESUME: Output stream creation failed.", e);
          }


          message = "S";

         msgBuffer = message.getBytes();

          try {
           outStream.write(msgBuffer);

          } catch (IOException e) {
                  Log.e(TAG, "ON RESUME: Exception during write.", e);
          }
     break;
     
     case MotionEvent.ACTION_UP:
      try {
            outStream = btSocket.getOutputStream();

           } catch (IOException e) {
               Log.e(TAG, "ON RESUME: Output stream creation failed.", e);
           }


           message = "0";

           msgBuffer = message.getBytes();

           try {
            outStream.write(msgBuffer);

           } catch (IOException e) {
                   Log.e(TAG, "ON RESUME: Exception during write.", e);
           }
      break;
     }
     
     return false;
    }
             
         
    });
  //左转
    mButtonL=(Button)findViewById(R.id.btnL);
    mButtonL.setOnTouchListener(new Button.OnTouchListener(){
            
    @Override
    public boolean onTouch(View v, MotionEvent event) {
     // TODO Auto-generated method stub
     String message;
     byte[] msgBuffer;
     int action = event.getAction();
     switch(action)
     {
     case MotionEvent.ACTION_DOWN:
     try {
           outStream = btSocket.getOutputStream();

          } catch (IOException e) {
              Log.e(TAG, "ON RESUME: Output stream creation failed.", e);
          }


          message = "A";

         msgBuffer = message.getBytes();

          try {
           outStream.write(msgBuffer);

          } catch (IOException e) {
                  Log.e(TAG, "ON RESUME: Exception during write.", e);
          }
     break;
     
     case MotionEvent.ACTION_UP:
      try {
            outStream = btSocket.getOutputStream();

           } catch (IOException e) {
               Log.e(TAG, "ON RESUME: Output stream creation failed.", e);
           }


           message = "0";

           msgBuffer = message.getBytes();

           try {
            outStream.write(msgBuffer);

           } catch (IOException e) {
                   Log.e(TAG, "ON RESUME: Exception during write.", e);
           }
      break;
     }
     
     return false;
     
              }
            });
          //右转
            mButtonR=(Button)findViewById(R.id.btnR);
            mButtonR.setOnTouchListener(new Button.OnTouchListener(){
            
    @Override
    public boolean onTouch(View v, MotionEvent event) {
     // TODO Auto-generated method stub
     String message;
     byte[] msgBuffer;
     int action = event.getAction();
     switch(action)
     {
     case MotionEvent.ACTION_DOWN:
     try {
           outStream = btSocket.getOutputStream();

          } catch (IOException e) {
              Log.e(TAG, "ON RESUME: Output stream creation failed.", e);
          }


          message = "D";

         msgBuffer = message.getBytes();

          try {
           outStream.write(msgBuffer);

          } catch (IOException e) {
                  Log.e(TAG, "ON RESUME: Exception during write.", e);
          }
     break;
     
     case MotionEvent.ACTION_UP:
      try {
            outStream = btSocket.getOutputStream();

           } catch (IOException e) {
               Log.e(TAG, "ON RESUME: Output stream creation failed.", e);
           }


           message = "0";

           msgBuffer = message.getBytes();

           try {
            outStream.write(msgBuffer);

           } catch (IOException e) {
                   Log.e(TAG, "ON RESUME: Exception during write.", e);
           }
      break;
     }   
    return false;  
    }                
    });
  //停止
    mButtonS=(Button)findViewById(R.id.btnS);
    mButtonS.setOnTouchListener(new Button.OnTouchListener(){
            
    @Override
    public boolean onTouch(View v, MotionEvent event) {
     // TODO Auto-generated method stub
     if(event.getAction()==MotionEvent.ACTION_DOWN)
      try {
            outStream = btSocket.getOutputStream();
           } catch (IOException e) {
               Log.e(TAG, "ON RESUME: Output stream creation failed.", e);
           }
           String message = "Q";
           byte[] msgBuffer = message.getBytes();
           try {
            outStream.write(msgBuffer);

           } catch (IOException e) {
                   Log.e(TAG, "ON RESUME: Exception during write.", e);
           }
     return false;
    }     
    });
    if (D)
     Log.e(TAG, "+++ ON CREATE +++");
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    if (mBluetoothAdapter == null) { 
     Toast.makeText(this, "Bluetooth is not available.", Toast.LENGTH_LONG).show();
            finish();
            return;
    }
    if (!mBluetoothAdapter.isEnabled()) {
            Toast.makeText(this,  "Please enable your Bluetooth and re-run this program.", Toast.LENGTH_LONG).show();
            finish();
            return;
    }
    if (D)
    Log.e(TAG, "+++ DONE IN ON CREATE, GOT LOCAL BT ADAPTER +++");
    }
    @Override
    public void onStart() {
		super.onStart();
		if (D) Log.e(TAG, "++ ON START ++");
    }
    @Override
    public void onResume() {
        super.onResume();
        if (D) {
         Log.e(TAG, "+ ON RESUME +");
         Log.e(TAG, "+ ABOUT TO ATTEMPT CLIENT CONNECT +");

        }
        BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
        try {
           btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
        } catch (IOException e) {
                Log.e(TAG, "ON RESUME: Socket creation failed.", e);
        }
        mBluetoothAdapter.cancelDiscovery();
        try {
               btSocket.connect();
                Log.e(TAG, "ON RESUME: BT connection established, data transfer link open.");
        } catch (IOException e) {
                try {
                 btSocket.close();

                } catch (IOException e2) {

                        Log .e(TAG,"ON RESUME: Unable to close socket during connection failure", e2);
                }
        }
            // Create a data stream so we can talk to server.
            if (D)
             Log.e(TAG, "+ ABOUT TO SAY SOMETHING TO SERVER +"); 
           try {
             outStream = btSocket.getOutputStream();
            } catch (IOException e) {
                Log.e(TAG, "ON RESUME: Output stream creation failed.", e);
            }
            String message = "1";
            byte[] msgBuffer = message.getBytes();
            try {
             outStream.write(msgBuffer);
            } catch (IOException e) {
                    Log.e(TAG, "ON RESUME: Exception during write.", e);
            }       
    }
    @Override
    public void onPause() {
        super.onPause();
        if (D)
         Log.e(TAG, "- ON PAUSE -");
        if (outStream != null) {
                try {
                        outStream.flush();
                } catch (IOException e) {
                        Log.e(TAG, "ON PAUSE: Couldn't flush output stream.", e);
                }
        }
        try {
                btSocket.close();
        } catch (IOException e2) {
                Log.e(TAG, "ON PAUSE: Unable to close socket.", e2);
        }
    }
    @Override
    public void onStop() {
        super.onStop();
        if (D)Log.e(TAG, "-- ON STOP --");
    }
    @Override
    public void onDestroy() {
       super.onDestroy();
        if (D) Log.e(TAG, "--- ON DESTROY ---");

    }

}

别忘记添加蓝牙权限

<uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

最后附上效果图和机器人图片


2019-11-10 10:25:16 RiseC 阅读数 49
  • arduino实战

    学习如何在arduino中使用各种传感器,包括人体红外传感器,超声波传感器,舵机控制,温湿度传感器,激光接收传感器等,空气质量传感器,wifi模块等....课程内容会不断的更新增加,只要发现比较有趣的传感器就会做对应的实战视频

    420 人正在学习 去看看 陈贤能

HC-05的接线及使用

单个使用时只需接rx,tx,vcc,gnd四个引脚并将模块的rx与单片机的tx连接(硬串口或软串口),tx与单片机的rx连接(硬串口或软串口)

进入蓝牙的设置模式

在蓝牙模块通电之前按住蓝牙模块上的按键直到通电(慢闪)说明就进入了设置模式接下来就可以通过 以下代码进入AT

进入AT命令模式(设置蓝牙参数)

注意事项

  1. 一定要查看该蓝牙的传输波特率是多少。
  2. 串口的波特率一定要和程序的波特率一致。
  3. 把串口界面调成如下图所示
    在这里插入图片描述

进入AT模式的代码(注意在通电之前让蓝牙模块进入设置模式)

#include <SoftwareSerial.h>

// Pin10为RX,接HC05的TXD
// Pin11为TX,接HC05的RXD
SoftwareSerial BT(10, 11);
char val;

void setup() {
Serial.begin(9600);
Serial.println(“BT is ready!”);
// HC-05用AT命令查一下一般为9600或38400
BT.begin(38400);
}

void loop() {
if (Serial.available()) {
val = Serial.read();
BT.print(val);
}

if (BT.available()) {
val = BT.read();
Serial.print(val);
}

进入AT模式响应

打开arduino的串口监视器把波特率设置为与2,步骤中的波特率一致,输入以下命令,
回复OK说明模块正常工作在AT模式

常用AT命令如下

AT+UART?//查看蓝牙模块的波特率
AT+NAME=名称//设置/查询设备名称
设置/查询—模块角色:
AT+ ROLE?
0——从机(Slave)
1——主机(Master)
2——回环角色(Slave-Loop)
默认值:0

AT+ROLE=0//设置为从机(一般在用手机控制小车的蓝牙设置为从机)

AT+PSWD=配对码//设置
AT+ PSWD?//查询—配对码

AT+UART= //设置波特率
AT+ UART? //查询 串口(波特率)

完整小车蓝牙控制代码

请看资源(可私聊提供技术服务)

2019-04-03 08:55:07 weixin_42369687 阅读数 573
  • arduino实战

    学习如何在arduino中使用各种传感器,包括人体红外传感器,超声波传感器,舵机控制,温湿度传感器,激光接收传感器等,空气质量传感器,wifi模块等....课程内容会不断的更新增加,只要发现比较有趣的传感器就会做对应的实战视频

    420 人正在学习 去看看 陈贤能

 

本人刚刚入门arduino 制作了一个非常简陋的蓝牙小车

主要有hc-05蓝牙串口模块带动小车运行

 

 

 

材料有 hc-05蓝牙串口

           小车底盘轮胎以及电机

           l298n模块

           arduino uno r3

           杜邦线

           四节五号电池

材料的连接

 

 

hc-05蓝牙串口arduino uno r3的连接

 

 

    l298n模块 arduino uno r3的连接

四节五号电池  与l298n模块连接

arduino uno r3与电源连接

代码

 

int leftMotor1 = 4;
int leftMotor2 = 5;
int rightMotor1 = 6;
int rightMotor2 = 7;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
 
  pinMode(leftMotor1, OUTPUT);
  pinMode(leftMotor2, OUTPUT);
  pinMode(rightMotor1, OUTPUT);
  pinMode(rightMotor2, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  //usart read
  if(Serial.available()>0)
  {  char cmd;
    cmd = Serial.read();//读取蓝牙模块发送到串口的数据
  
    Serial.print(cmd);
 
   
   motorRun(cmd);

  } 
}
void motorRun(char cmd)
{
  switch(cmd){
    case 49:
      Serial.println("FORWARD"); //输出状态
      digitalWrite(leftMotor1, HIGH);
      digitalWrite(leftMotor2, LOW);
      digitalWrite(rightMotor1, HIGH);
      digitalWrite(rightMotor2, LOW);
      delay(1000);
      break;
     case 50:
      Serial.println("BACKWARD"); //输出状态
      digitalWrite(leftMotor1, LOW);
      digitalWrite(leftMotor2, HIGH);
      digitalWrite(rightMotor1, LOW);
      digitalWrite(rightMotor2, HIGH);
      delay(1000);
      break;
     case 51:
      Serial.println("TURN  LEFT"); //输出状态
      digitalWrite(leftMotor1, HIGH);
      digitalWrite(leftMotor2, LOW);
      digitalWrite(rightMotor1, LOW);
      digitalWrite(rightMotor2, HIGH);
     
      break;
     case 52
     :
      Serial.println("TURN  RIGHT"); //输出状态
      digitalWrite(leftMotor1, LOW);
      digitalWrite(leftMotor2, HIGH);
      digitalWrite(rightMotor1, HIGH);
      digitalWrite(rightMotor2, LOW);
    
      break;
     default:
      Serial.println("STOP"); //输出状态
      digitalWrite(leftMotor1, LOW);
      digitalWrite(leftMotor2, LOW);
      digitalWrite(rightMotor1, LOW);
      digitalWrite(rightMotor2, LOW);
      delay(1000 );
  }
}

代码如下并没有调整运行速度

也没有加巡线超声波测距等功能后续填坑

Arduino蓝牙通信实现

阅读数 1573

arduino与蓝牙模块

阅读数 1967

没有更多推荐了,返回首页