01MOTODUINO-2

參考資料:http://sinocgtchen.blogspot.tw/2013/05/motoduinoarduino-l293d-ir-tracker-sensor.html

利用手機APP,透過藍芽來測試自走車會不會按照「高速 中速 低速」的指令來運動

#include <SoftwareSerial.h>

SoftwareSerial BT(0,1); // 接收腳(RX), 傳送腳(TX);接HC-06之TXD、RXD;先不要用0,1,因為USB用

const int Motor_E1 = 5; // 控制馬達1轉速 digital pin 5 of Arduino (PWM)

const int Motor_E2 = 6; // 控制馬達2轉速 digital pin 6 of Arduino (PWM)

const int Motor_M1 = 10; // 控制馬達1正反轉 digital pin 10 of Arduino

const int Motor_M2 = 11; // 控制馬達2正反轉 digital pin 11 of Arduino

char val; // 接收來自藍芽資料

int power;

void setup()

{

// Start serial communication at 57600 baud rate(傳輸率)

// Serial.begin(57600);

BT.begin(57600);

pinMode(Motor_M1, OUTPUT); //設定 Motor_M1為輸出腳位

pinMode(Motor_M2, OUTPUT); //設定 Motor_M2為輸出腳位出

power=0;

}

//////////// 主程式 ////////

void loop()

{

// if(Serial.available())

// { val = Serial.read();

if(BT.available() ) {

val=BT.read();

switch(val) {

case 'f': // 前進

forward(0, power);

break;

case 'b': // 後退

backward(0, power);

break;

case 'l': // 左轉

left(0, power);

break;

case 'r': // 右轉

right(0, power);

break;

case 's': // 停止

motorstop(0, 0);

break;

case 'x' :

lowspeed();

break;

case 'y' :

midspeed();

break;

case 'z' :

highspeed();

break;

} }

}

void motorstop(byte flag, byte motorspeed)

{

Serial.println("stop!");

digitalWrite( Motor_E1, motorspeed);

digitalWrite( Motor_E2, motorspeed);

}

void forward(byte flag, byte motorspeed)

{

Serial.println("forward!");

digitalWrite( Motor_M1, HIGH);

digitalWrite( Motor_M2, HIGH);

analogWrite( Motor_E1, motorspeed);

analogWrite( Motor_E2, motorspeed);

}

void backward(byte flag, byte motorspeed)

{

Serial.println("backward!");

digitalWrite( Motor_M1, LOW);

digitalWrite( Motor_M2, LOW);

analogWrite( Motor_E1, motorspeed);

analogWrite( Motor_E2, motorspeed);

}

void right(byte flag, byte motorspeed)

{

Serial.println("right!");

digitalWrite( Motor_M1, HIGH);

digitalWrite( Motor_M2, HIGH);

analogWrite( Motor_E1, 0);

analogWrite( Motor_E2, motorspeed);

}

void left(byte flag, byte motorspeed)

{

Serial.println("left!");

digitalWrite( Motor_M1, HIGH);

digitalWrite( Motor_M2, HIGH);

analogWrite( Motor_E1, motorspeed);

analogWrite( Motor_E2, 0);

}

void lowspeed(){

power=100;

}

void midspeed(){

power=200;

}

void highspeed(){

power=250;

}

--------------------------------------------------------------------

利用手機APP,透過藍芽來測試自走車的循跡功能(2016.05.15測試成功)

避障功能???

#include <Arduino.h>

#include <Wire.h>

//#include <Servo.h>

#include <SoftwareSerial.h>

SoftwareSerial BT(0,1); // 接收腳(RX), 傳送腳(TX);接HC-06之TXD、RXD;先不要用0,1,因為USB用

// Sensor sequence: SLeftLeft SMiddle SRightRight

const int SLeftLeft = 2; //左感測器輸入腳

const int SMiddle = 3; //中間感測器輸入腳

const int SRightRight = 4; //右感測器輸入腳

// variables will change:

int SLL; //左感測器狀態

int SM; //中感測器狀態

int SRR; //右感測器狀態

const int Motor_E1 = 5; // 控制馬達1轉速 digital pin 5 of Arduino (PWM)

const int Motor_E2 = 6; // 控制馬達2轉速 digital pin 6 of Arduino (PWM)

const int Motor_M1 = 10; // 控制馬達1正反轉 digital pin 10 of Arduino

const int Motor_M2 = 11; // 控制馬達2正反轉 digital pin 11 of Arduino

char val; // 接收來自藍芽資料

int power;

byte SensorStatus=0;

//#define SENSOR_L 4;

//#define SENSOR_M 2;

//#define SENSOR_R 1;

double sw;

double sw2;

double flag;

double flag2;

float getDistance(int trig,int echo){

pinMode(trig,OUTPUT);

digitalWrite(trig,LOW);

delayMicroseconds(2);

digitalWrite(trig,HIGH);

delayMicroseconds(10);

pinMode(echo, INPUT);

return pulseIn(echo,HIGH,30000)/58.0;

}

void setup()

{

// Start serial communication at 57600 baud rate(傳輸率)

// Serial.begin(57600);

BT.begin(57600);

// 輸出入接腳初始設定

pinMode(SLeftLeft, INPUT);

pinMode(SMiddle, INPUT);

pinMode(SRightRight, INPUT);

pinMode(Motor_M1, OUTPUT); //設定 Motor_M1為輸出腳位

pinMode(Motor_M2, OUTPUT); //設定 Motor_M2為輸出腳位出

power=200;

flag = 1;

sw2 = 1;

sw = 1;

flag2 = 1;

}

//////////// 主程式 ////////

void loop()

{

if((((sw)==(0))) & (((flag)==(0)))){

SensorStatus = (digitalRead(2)) + (((digitalRead(3)) * (2)) + ((digitalRead(4)) * (4)));

drivemotor();

}

if((((sw2)==(0))) & (((flag2)==(0)))){

if((getDistance(13,12)) < (10)){

backward(0, power);;

delay(1000*0.5);

if(((1)==(random(0,1)))){

left(0, power);

}else{

right(0, power);

}

delay(1000*0.5);

}

forward(0, power);

}

// if(Serial.available())

// { val = Serial.read();

if(BT.available() ) {

val=BT.read();

switch(val) {

case 'f': // 前進

forward(0, power);

break;

case 'b': // 後退

backward(0, power);

break;

case 'l': // 左轉

left(0, power);

break;

case 'r': // 右轉

right(0, power);

break;

case 's': // 停止

motorstop(0, 0);

flag = 1;

flag2 = 1;

break;

case 'x' :

lowspeed();

break;

case 'y' :

midspeed();

break;

case 'z' :

highspeed();

break;

case 't' :

if(((sw)==(0))){

sw = 1;

}

if(((sw)==(1))){

sw = 0;

flag = 0;

}

break;

case 'n' :

if(((sw2)==(0))){

sw2 = 1;

}

if(((sw2)==(1))){

sw2 = 0;

flag2 = 0;

}

break;

} }

}

void motorstop(byte flag, byte motorspeed)

{

Serial.println("stop!");

digitalWrite( Motor_E1, motorspeed);

digitalWrite( Motor_E2, motorspeed);

}

void forward(byte flag, byte motorspeed)

{

Serial.println("forward!");

digitalWrite( Motor_M1, HIGH);

digitalWrite( Motor_M2, HIGH);

analogWrite( Motor_E1, motorspeed);

analogWrite( Motor_E2, motorspeed);

}

void backward(byte flag, byte motorspeed)

{

Serial.println("backward!");

digitalWrite( Motor_M1, LOW);

digitalWrite( Motor_M2, LOW);

analogWrite( Motor_E1, motorspeed);

analogWrite( Motor_E2, motorspeed);

}

void right(byte flag, byte motorspeed)

{

Serial.println("right!");

digitalWrite( Motor_M1, HIGH);

digitalWrite( Motor_M2, HIGH);

analogWrite( Motor_E1, 0);

analogWrite( Motor_E2, motorspeed);

}

void left(byte flag, byte motorspeed)

{

Serial.println("left!");

digitalWrite( Motor_M1, HIGH);

digitalWrite( Motor_M2, HIGH);

analogWrite( Motor_E1, motorspeed);

analogWrite( Motor_E2, 0);

}

void lowspeed(){

power=150;

}

void midspeed(){

power=200;

}

void highspeed(){

power=250;

}

void drivemotor()

{

if(SensorStatus==0){

motorstop(0, 0);

}

if(SensorStatus==1){

left(0, power);

}

if(SensorStatus==2){

forward(0, power);

}

if(SensorStatus==3){

left(0, power);

}

if(SensorStatus==4){

right(0, power);

}

if(SensorStatus==5){

right(0, power);

}

if(SensorStatus==6){

right(0, power);

}

if(SensorStatus==7){

forward(0, power);

}

}

=======================================

單純循跡的官方範例

// Sensor sequence: SLeftLeft SMiddle SRightRight

const int SLeftLeft = 9; //左感測器輸入腳

const int SMiddle = 10; //中間感測器輸入腳

const int SRightRight = 11; //右感測器輸入腳

// variables will change:

int SLL; //左感測器狀態

int SM; //中感測器狀態

int SRR; //右感測器狀態

const int Motor_M1 = 7;

const int Motor_M2 = 8;

const int Motor_E1 = 5;

const int Motor_E2 = 6;

byte byteSensorStatus=0;

#define SENSOR_L 4;

#define SENSOR_M 2;

#define SENSOR_R 1;

void setup() {

//set up serial communications

Serial.begin(9600);

// 輸出入接腳初始設定

pinMode(SLeftLeft, INPUT);

pinMode(SMiddle, INPUT);

pinMode(SRightRight, INPUT);

pinMode(Motor_M1, OUTPUT);

pinMode(Motor_M2, OUTPUT);

}

void loop(){

byteSensorStatus = 0;

// 讀取感測器狀態值

SLL = digitalRead(SLeftLeft);

if(SLL == 1)

byteSensorStatus = (byteSensorStatus | (0x01 << 2));

SM = digitalRead(SMiddle);

if(SM == 1)

byteSensorStatus = (byteSensorStatus | (0x01 << 1));

SRR = digitalRead(SRightRight);

if(SRR == 1)

byteSensorStatus = (byteSensorStatus | 0x01);

// Serial.println(byteSensorStatus, HEX);

switch(byteSensorStatus)

{ // 感測器黑色:0 白色:1

case 0: // SL:0 SM:0 SR:0

motorstop(0, 255);

break;

case 1: // SL:0 SM:0 SR:1 //no used

left(0, 255);

break;

case 2: // SL:0 SM:1 SR:0 //no used

motorstop(0, 255);

break;

case 3: // SL:0 SM:1 SR:1

left(0, 255);

break;

case 4: // SL:1 SM:0 SR:0 // no used

right(0, 255);

break;

case 5: // SL:1 SM:0 SR:1

forward(0, 255);

break;

case 6: // SL:1 SM:1 SR:0

right(0, 255);

break;

case 7: // SL:1 SM:1 SR:1

forward(0, 255);

}

}

void looptest(){

forward(0,255);

delay(1000);

back(0,255);

delay(1000);

right(0,255);

delay(1000);

left(0,255);

delay(1000);

motorstop(0,0);

delay(3000);

}

void motorstop(byte flag, byte numOfValues)

{

digitalWrite( Motor_E1, 0);

digitalWrite( Motor_E2, 0);

// Serial.println("stop : ");

}

void forward(byte flag, byte numOfValues)

{

digitalWrite( Motor_M1, LOW);

digitalWrite( Motor_M2, LOW);

analogWrite( Motor_E1, numOfValues);

analogWrite( Motor_E2, numOfValues);

}

void back(byte flag, byte numOfValues)

{

digitalWrite( Motor_M1, HIGH);

digitalWrite( Motor_M2, HIGH);

analogWrite( Motor_E1, numOfValues);

analogWrite( Motor_E2, numOfValues);

}

void right(byte flag, byte numOfValues)

{

digitalWrite( Motor_M1, LOW);

digitalWrite( Motor_M2, LOW);

analogWrite( Motor_E1, numOfValues);

analogWrite( Motor_E2, 0);

}

void left(byte flag, byte numOfValues)

{

digitalWrite( Motor_M1, LOW);

digitalWrite( Motor_M2, LOW);

analogWrite( Motor_E1, 0);

analogWrite( Motor_E2, numOfValues);

}