架橋車 Bridge car renew(arduino)
#include <SoftwareSerial.h>
#include <Wire.h>
#include <Servo.h>
long a;
long e;
Servo servo_2;
Servo servo_3;
Servo servo_4;
const int leftMotorIn1 = 6;
const int leftMotorIn2 = 7;
SoftwareSerial CAR_BT(8,9); //定義 tX 及rX 腳位
void setup()
{
servo_2.attach(2);
servo_3.attach(3);
servo_4.attach(4);
pinMode(leftMotorIn1, OUTPUT);
pinMode(leftMotorIn2, OUTPUT);
a = 90;
e = 90;
Serial.begin(9600); //Arduino起始鮑率:9600
CAR_BT.begin(9600); //藍牙鮑率:9600 注意!每個藍牙晶片的鮑率都不太一樣,請務必確認
}
void p() {
digitalWrite(leftMotorIn2, LOW);
digitalWrite(leftMotorIn1, HIGH);
}
void D() {
servo_4.write(40);
}
void q() {
digitalWrite(leftMotorIn1, LOW);
digitalWrite(leftMotorIn2, HIGH);
}
void U() {
servo_4.write(140);
}
void u() {
servo_4.write(140);
}
void uq() {
servo_4.write(140);
digitalWrite(leftMotorIn1, LOW);
digitalWrite(leftMotorIn2, HIGH);
}
void d() {
servo_4.write(40);
}
void dp() {
servo_4.write(40);
digitalWrite(leftMotorIn2, LOW);
digitalWrite(leftMotorIn1, HIGH);
}
void b() {
servo_2.write(70);
servo_3.write(110);
}
void s() {
digitalWrite(leftMotorIn2, LOW);
digitalWrite(leftMotorIn1, LOW);
servo_2.write(90);
servo_3.write(90);
servo_4.write(90);
}
void f() {
servo_2.write(110);
servo_3.write(70);
}
void l() {
servo_2.write(110);
servo_3.write(110);
}
void w() {
servo_2.write(110);
servo_3.write(90);
}
void r() {
servo_2.write(70);
servo_3.write(70);
}
void o() {
servo_2.write(90);
servo_3.write(70);
}
void m() {
servo_2.write(90);
servo_3.write(110);
}
void z() {
servo_2.write(70);
servo_3.write(90);
}
void loop()
{
int inSize;
char input;
if( (inSize = (CAR_BT.available())) > 0) { //讀取藍牙訊息
Serial.print("size = ");
Serial.println(inSize);
Serial.print("Input = ");
Serial.println(input=(char)CAR_BT.read());
if( input == 'j' ) {
uq();
delay(1000);
s();
q();
delay(3000);
s();
uq();
delay(1000);
s();
q();
delay(3000);
s();
uq();
delay(2000);
s();
q();
delay(6000);
s();
}
if( input == 'k' ) {
p();
delay(8000);
s();
dp();
delay(300);
s();
p();
delay(3700);
s();
dp();
delay(700);
s();
p();
delay(2300);
s();
dp();
delay(700);
s();
p();
delay(1300);
s();
dp();
delay(700);
s();
p();
delay(1300);
s();
}
if( input == 'f' ) {
f();
}
if( input == 'l' ) {
l();
}
if( input == 'w' ) {
w();
}
if( input == 'r' ) {
r();
}
if( input == 'o' ) {
o();
}
if( input == 'u' ) {
u();
}
if( input == 'U' ) {
U();
}
if( input == 'b' ) {
b();
}
if( input == 'd' ) {
d();
}
if( input == 'D' ) {
D();
}
if( input == 's' ) {
s();
}
if( input == 'p' ) {
p();
}
if( input == 'q' ) {
q();
}
if( input == 'z' ) {
z();
}
if( input == 'm' ) {
m();
}
}
}