#include <SoftwareSerial.h> // TX RX software library for bluetooth
#include <Wire.h>
//輸出到L298N的腳位
#define MT_L 6 //ENA
#define IN1 5
#define IN2 4
#define IN3 5
#define IN4 4
#define MT_R 3 //ENB
void Stop();
void Forward();
void Backward();
char Car_status;
int Speed_value=150;
#include <Servo.h> // servo library
Servo myservo1, myservo2, myservo3, myservo4, myservo5; // servo name
int bluetoothTx = 12; // bluetooth tx to 12 pin
int bluetoothRx = 13; // bluetooth rx to 13 pin
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
void setup()
{
pinMode(MT_L,OUTPUT);
pinMode(IN1,OUTPUT);
pinMode(IN2,OUTPUT);
pinMode(IN3,OUTPUT);
pinMode(IN4,OUTPUT);
pinMode(MT_R,OUTPUT);
myservo1.attach(11); // attach servo signal wire to pin 9
myservo2.attach(10);
myservo3.attach(9);
myservo4.attach(8);
//Setup usb serial connection to computer
Serial.begin(9600);
//Setup Bluetooth serial connection to android
bluetooth.begin(9600);
}
void loop()
{
//Read from bluetooth and write to usb serial
if(bluetooth.available()>= 2 )
{
unsigned int servopos = bluetooth.read();
unsigned int servopos1 = bluetooth.read();
unsigned int realservo = (servopos1 *256) + servopos;
Serial.println(realservo);
if (realservo >= 1000 && realservo <1180) {
int servo1 = realservo;
servo1 = map(servo1, 1000, 1180, 0, 180);
myservo1.write(servo1);
Serial.println("Servo 1 ON");
delay(10);
}
if (realservo >= 2000 && realservo <2180) {
int servo2 = realservo;
servo2 = map(servo2, 2000, 2180, 0, 180);
myservo2.write(servo2);
Serial.println("Servo 2 ON");
delay(10);
}
if (realservo >= 3000 && realservo <3180) {
int servo3 = realservo;
servo3 = map(servo3, 3000, 3180, 0, 180);
myservo3.write(servo3);
Serial.println("Servo 3 ON");
delay(10);
}
if (realservo >= 4000 && realservo <4180) {
int servo4 = realservo;
servo4 = map(servo4, 4000, 4180, 0, 180);
myservo4.write(servo4);
Serial.println("Servo 4 ON");
delay(10);
}
if (realservo >= 5000 && realservo <5180) {
int servo5 = realservo;
servo5 = map(servo5, 5000, 5180, 0, 180);
myservo5.write(servo5);
Serial.println("Servo 5 ON");
delay(10);
}
if(Serial.available() == true){
Car_status = Serial.read();
}
//當藍芽有資料傳輸時,會將讀取到的字元存到Car_status。
if (bluetooth.available()){
Car_status =bluetooth.read();
Serial.println(Car_status);
}
//依照Car_status的字元來選擇車子方向控制
switch(Car_status){
case 'F':
Forward();
Serial.println("Forward");
Car_status='N';
break;
case 'B':
Backward();
Serial.println("Backward");
Car_status='N';
break;
case 'S':
Stop();
Serial.println("Stop");
Car_status='N';
break;
case 'v':
Speed_value=0; //先將速度歸0
do{
if (bluetooth.available()){
Car_status =bluetooth.read(); //讀取藍芽傳送字元
Serial.println(Car_status);
//當藍芽讀取到字元'e'時,就會跳出迴圈。
if( Car_status=='e'){
Serial.println(Speed_value);
break;
}
else
//將傳送來的速度數值字元轉成整數。
Speed_value=10*Speed_value+(Car_status-48);
}
}while(true);
}
}
}
//下面是停止、前進、後退、左轉、右轉的函式。
void Stop()
{
analogWrite(MT_L,0);
analogWrite(MT_R,0);
}
void Forward()
{
digitalWrite(IN1,HIGH);
digitalWrite(IN2,LOW);
digitalWrite(IN3,LOW);
digitalWrite(IN4,HIGH);
analogWrite(MT_R,Speed_value);
analogWrite(MT_L,Speed_value);
}
void Backward()
{
digitalWrite(IN1,LOW);
digitalWrite(IN2,HIGH);
digitalWrite(IN3,HIGH);
digitalWrite(IN4,LOW);
analogWrite(MT_R,Speed_value);
analogWrite(MT_L,Speed_value);
}
I have a problem to my code
I can't use it
My phone can connect
but i can't control
The ardiuno wiring is normal