1. 工作内容
装配Joystick,外接HC-05蓝牙模块,使用按键实现小车上下左右跑动
2. 内容实现
Joystick 手柄模块驱动,参见我的另一篇文章:Joystick Shield拓展板驱动
版本【一】:通过按键实现小车跑动(Joy端代码)
#define PIN_ANALOG_X 0
#define PIN_ANALOG_Y 1
#define BUTTON_UP 2
#define BUTTON_RIGHT 3
#define BUTTON_DOWN 4
#define BUTTON_LEFT 5
#define BUTTON_E 6
#define BUTTON_F 7
#define WR_UP 0x01
#define WR_RIGHT 0x02
#define WR_DOWN 0x03
#define WR_LEFT 0x04
#define DELAY 40
void setup() {
// baud=9600 8data noCheck 2stop
Serial.begin(9600,SERIAL_8N2);
pinMode(BUTTON_UP, INPUT);
pinMode(BUTTON_RIGHT, INPUT);
pinMode(BUTTON_DOWN, INPUT);
pinMode(BUTTON_LEFT, INPUT);
pinMode(BUTTON_E, INPUT);
pinMode(BUTTON_F, INPUT);
}
void loop() {
unsigned int x,y;
if(digitalRead(BUTTON_UP) == LOW) {
Serial.write(WR_UP);
delay(DELAY);
}
if(digitalRead(BUTTON_RIGHT) == LOW) {
Serial.write(WR_RIGHT);
delay(DELAY);
}
if(digitalRead(BUTTON_DOWN) == LOW) {
Serial.write(WR_DOWN);
delay(DELAY);
}
if(digitalRead(BUTTON_LEFT) == LOW) {
Serial.write(WR_LEFT);
delay(DELAY);
}
}
版本【一】:通过按键实现小车跑动(Board端代码)
由于Arduino上每个引脚有公母头两个插口,所以L298N模块ENA和ENB分别需要2个引脚可以合并成1个,接到同一个GPIO口上,以节省IO口。
#include "motor.h"
#define UP 0x01
#define RIGHT 0x02
#define DOWN 0x03
#define LEFT 0x04
void setup() {
//蓝牙端口
Serial.begin(9600,SERIAL_8N2);
//电机端口
pinMode(ENA_PIN,OUTPUT); pinMode(ENB_PIN,OUTPUT);
pinMode(INA1_PIN,OUTPUT); pinMode(INA2_PIN,OUTPUT);
pinMode(INB1_PIN,OUTPUT); pinMode(INB2_PIN,OUTPUT);
}
void loop() {
unsigned char i;
switch(Serial.read())
{
case UP: UP_Run(); break;
case DOWN: DOWN_Run(); break;
case LEFT: LEFT_Run(); break;
case RIGHT: RIGHT_Run(); break;
default: STOP_Run(); break;
}
}
motor.h 头文件代码
#ifndef __MOTOR_H
#define __MOTOR_H
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#define ENA_PIN 9
#define ENB_PIN 4
#define INA1_PIN 8
#define INA2_PIN 7
#define INB1_PIN 6
#define INB2_PIN 5
#define SET_ENA() digitalWrite(ENA_PIN,HIGH)
#define CLR_ENA() digitalWrite(ENA_PIN,LOW)
#define SET_ENB() digitalWrite(ENB_PIN,HIGH)
#define CLR_ENB() digitalWrite(ENB_PIN,LOW)
#define SET_INA1() digitalWrite(INA1_PIN,HIGH)
#define CLR_INA1() digitalWrite(INA1_PIN,LOW)
#define SET_INA2() digitalWrite(INA2_PIN,HIGH)
#define CLR_INA2() digitalWrite(INA2_PIN,LOW)
#define SET_INB1() digitalWrite(INB1_PIN,HIGH)
#define CLR_INB1() digitalWrite(INB1_PIN,LOW)
#define SET_INB2() digitalWrite(INB2_PIN,HIGH)
#define CLR_INB2() digitalWrite(INB2_PIN,LOW)
#define SPEED_SET 7
#define SPEED_CLR 10-SPEED_SET
void UP_Run(void)
{
unsigned char i;
//A轮 正转
CLR_INA1();
SET_INA2();
//B轮 正转
CLR_INB1();
SET_INB2();
for(i=0;i<5;i++)
{
if(i%2==0)
{
CLR_ENB();
CLR_ENA();
delay(SPEED_CLR);
}else
{
SET_ENB();
SET_ENA();
delay(SPEED_SET);
}
}
CLR_ENB();
CLR_ENA();
}
void DOWN_Run(void)
{
unsigned char i;
//A轮 反转
SET_INA1();
CLR_INA2();
//B轮 反转
SET_INB1();
CLR_INB2();
for(i=0;i<5;i++)
{
if(i%2==0)
{
CLR_ENB();
CLR_ENA();
delay(SPEED_CLR);
}else
{
SET_ENB();
SET_ENA();
delay(SPEED_SET);
}
}
CLR_ENB();
CLR_ENA();
}
void LEFT_Run(void)
{
unsigned char i;
//B轮 正转
CLR_INB1();
SET_INB2();
SET_ENA(); //必须使能 否则电机不转动
for(i=0;i<5;i++)
{
if(i%2==0)
{
CLR_ENB();
delay(SPEED_CLR);
}else
{
SET_ENB();
delay(SPEED_SET);
}
}
CLR_ENB();
CLR_ENA();
}
void RIGHT_Run(void)
{
unsigned char i;
//A轮 正转
CLR_INA1();
SET_INA2();
SET_ENB(); //必须使能 否则电机不转动
for(i=0;i<5;i++)
{
if(i%2==0)
{
CLR_ENA();
delay(SPEED_CLR);
}else
{
SET_ENA();
delay(SPEED_SET);
}
}
CLR_ENA();
CLR_ENB();
}
void STOP_Run(void)
{
CLR_ENA();
CLR_INA1();
CLR_INA2();
CLR_ENB();
CLR_INB1();
CLR_INB2();
}
#endif