Arduino改車小幫手

手機APP

HC-05請使用:https://play.google.com/store/apps/details?id=braulio.calle.bluetoothRCcontroller

 

onshape圖檔

https://cad.onshape.com/documents/56a1c498e8262c7ae6096ea7/w/0f11a3e1cb10b91961972b6a/e/a423350ec974ae829bb06a79

 

老師我提供的基礎亂寫程式:

此程式只保證你的車子可以動,剩下優化請找資訊老師 😀

#include <SoftwareSerial.h>
char BTcmd;
SoftwareSerial BT(7,8); // RX, TX
int MotorRight1=3;
int MotorRight2=4;
int MotorLeft1=5;
int MotorLeft2=6;
int MotorRight3=11;
int MotorRight4=10;
int MotorLeft3=12;
int MotorLeft4=13;

void setup()
{
Serial.begin(9600);
BT.begin(9600);
pinMode(MotorRight1, OUTPUT);
pinMode(MotorRight2, OUTPUT);
pinMode(MotorLeft1, OUTPUT);
pinMode(MotorLeft2, OUTPUT);
pinMode(MotorRight3, OUTPUT);
pinMode(MotorRight4, OUTPUT);
pinMode(MotorLeft3, OUTPUT);
pinMode(MotorLeft4, OUTPUT);
}

void go()// 前進
{
digitalWrite(MotorRight1,HIGH);
digitalWrite(MotorRight2,LOW);
digitalWrite(MotorLeft1,HIGH);
digitalWrite(MotorLeft2,LOW);
digitalWrite(MotorRight3,HIGH);
digitalWrite(MotorRight4,LOW);
digitalWrite(MotorLeft3,HIGH);
digitalWrite(MotorLeft4,LOW);

}

void left() //右轉
{
digitalWrite(MotorRight1,HIGH);
digitalWrite(MotorRight2,LOW);
digitalWrite(MotorLeft1,LOW);
digitalWrite(MotorLeft2,HIGH);
digitalWrite(MotorRight3,HIGH);
digitalWrite(MotorRight4,LOW);
digitalWrite(MotorLeft3,LOW);
digitalWrite(MotorLeft4,HIGH);

}
void right() //左轉
{
digitalWrite(MotorRight1,LOW);
digitalWrite(MotorRight2,HIGH);
digitalWrite(MotorLeft1,HIGH);
digitalWrite(MotorLeft2,LOW);
digitalWrite(MotorRight3,LOW);
digitalWrite(MotorRight4,HIGH);
digitalWrite(MotorLeft3,HIGH);
digitalWrite(MotorLeft4,LOW);

}

void back()//
{
digitalWrite(MotorRight1,LOW);
digitalWrite(MotorRight2,HIGH);
digitalWrite(MotorLeft1,LOW);
digitalWrite(MotorLeft2,HIGH);
digitalWrite(MotorRight3,LOW);
digitalWrite(MotorRight4,HIGH);
digitalWrite(MotorLeft3,LOW);
digitalWrite(MotorLeft4,HIGH);

}
void stop() //停止
{
digitalWrite(MotorRight1,LOW);
digitalWrite(MotorRight2,LOW);
digitalWrite(MotorLeft1,LOW);
digitalWrite(MotorLeft2,LOW);
digitalWrite(MotorRight3,LOW);
digitalWrite(MotorRight4,LOW);
digitalWrite(MotorLeft3,LOW);
digitalWrite(MotorLeft4,LOW);

}

void loop()
{
if (BT.available())
{
BTcmd= BT.read();
if ('F' == BTcmd) go();
else if ('L' ==BTcmd) left();
else if ('B' ==BTcmd) back();
else if ('R' == BTcmd) right();
else stop(); //else stop()
}
else{

delay(200);
}
Serial.flush();
}



 

要做馬達修改的話,可以去光華商場買材料,今天下班後我去採點拍了一下(可以參考:[專題] 光華商場材料採買

One comment

  1. #include
    char BTcmd;
    SoftwareSerial BT(7,8); // RX, TX
    int MotorRight1=3;
    int MotorRight2=4;
    int MotorLeft1=5;
    int MotorLeft2=6;

    void setup() {
    // put your setup code here, to run once:
    Serial.begin(9600);
    BT.begin(9600);
    pinMode(MotorRight1, OUTPUT);
    pinMode(MotorRight2, OUTPUT);
    pinMode(MotorLeft1, OUTPUT);
    pinMode(MotorLeft2, OUTPUT);
    }

    char m3;
    char m4;
    char m5;
    char m6;

    void loop() {
    // put your main code here, to run repeatedly:
    if(BT.available()){
    BTcmd= BT.read();
    if(‘F’ == BTcmd){
    m3=HIGH;m4=LOW;m5=HIGH;m6=LOW;
    }
    else if(‘L’ ==BTcmd){
    m3=HIGH;m4=LOW;m5=LOW;m6=HIGH;
    }
    else if(‘B’ ==BTcmd){
    m3=LOW;m4=HIGH;m5=HIGH;m6=LOW;
    }
    else if(‘R’ ==BTcmd){
    m3=LOW;m4=HIGH;m5=LOW;m6=HIGH;
    }
    else{
    m3=LOW;m4=LOW;m5=LOW;m6=LOW;
    }
    }
    else{
    delay(200);
    }

    digitalWrite(MotorRight1,m3);
    digitalWrite(MotorRight2,m4);
    digitalWrite(MotorLeft1,m5);
    digitalWrite(MotorLeft2,m6);

    Serial.flush();
    }

發表迴響

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com 標誌

您的留言將使用 WordPress.com 帳號。 登出 /  變更 )

Google photo

您的留言將使用 Google 帳號。 登出 /  變更 )

Twitter picture

您的留言將使用 Twitter 帳號。 登出 /  變更 )

Facebook照片

您的留言將使用 Facebook 帳號。 登出 /  變更 )

連結到 %s