相當於Arduino UNO + Grove shield的組合
既然如此,這樣應該可以把原先的樹莓派小車,改成廉價版本的Arduino小車.
但考慮到Arduino無法直接使用藍芽搖桿連線.腦筋就動到 AT89S525萬用遙控器身上
在此跟賣家盜圖,點圖就是賣家的賣場.有興趣的朋友可以去買.
至於零件的部分,露天拍賣上可以找到大概花費1000有找.
自走車 車體套件 附L298N馬達驅動模組
|
雙頭母對母 20CM杜邦線 * 10 Pcs
|
Arduino Nano V3.0
|
無線遙控模組 AT89S52
315MHz (點控式)
|
就四個按鈕先定義小車的行為 先做以下動作定義
A
|
B
|
C
|
D
|
動作定義
|
0
|
0
|
0
|
0
|
停止
|
0
|
0
|
0
|
1
|
原地左轉
|
0
|
0
|
1
|
0
|
原地右轉
|
0
|
0
|
1
|
1
|
無定義
|
0
|
1
|
0
|
0
|
後退
|
0
|
1
|
0
|
1
|
後退左轉
|
0
|
1
|
1
|
0
|
後退右轉
|
0
|
1
|
1
|
1
|
無定義
|
1
|
0
|
0
|
0
|
前行
|
1
|
0
|
0
|
1
|
前行左轉
|
1
|
0
|
1
|
0
|
前行右轉
|
1
|
0
|
1
|
1
|
無定義
|
1
|
1
|
0
|
0
|
無定義
|
1
|
1
|
0
|
1
|
無定義
|
1
|
1
|
1
|
0
|
無定義
|
1
|
1
|
1
|
1
|
無定義
|
定義好 就開始組裝接線 就些接線的部分
Arduino Nano
|
L298N
|
AT89S52
|
Remark
|
D2
|
IN1
|
||
D3
|
IN2
|
||
D4
|
IN3
|
||
D5
|
IN4
|
||
D6
|
|||
D7
|
|||
D8
|
D0
|
||
D9
|
D1
|
||
D10
|
D2
|
||
D11
|
D3
|
||
GND
|
GND
|
GND
|
L298N /
AT89S52 的GND都要接到Arduion的GND上,GND需等位準
|
就Coding的部分先假設會成功...以下程式還沒正式上路.但可藉由UART監控 遙控器的作動
以下為程式碼,使用Arduino的軟體將下文複製到編輯區,上傳到Arduino.上傳完畢後.
順利的話應該車就會順著遙控器的指令行動了
(Arduino的編輯器網址:https://www.arduino.cc/en/Main/Donate,網頁上有下載點Just Download,點選即可下載安裝)
// defines pins numbers const int PinIN1 = 2; // D2 link to L298N IN1 const int PinIN2 = 3; // D3 link to L298N IN2 const int PinIN3 = 4; // D4 link to L298N IN3 const int PinIN4 = 5; // D5 link to L298N IN4 const int PinD0 = 8; // D8 link to AT89S52 315M AVR D0 const int PinD1 = 9; // D9 link to AT89S52 315M AVR D1 const int PinD2 = 10; // D10 link to AT89S52 315M AVR D2 const int PinD3 = 11; // D11 link to AT89S52 315M AVR D3 const int Pulses = 200; //Define delay time 1000 = 1 sec int BTN_A = 0; int BTN_B = 0; int BTN_C = 0; int BTN_D = 0; int BTN[4] = {0, 0, 0, 0}; //Sub Function for Moto Ctrl L298N void StopMoving(){ // L298N asking Motor STOP digitalWrite(PinIN1, LOW); digitalWrite(PinIN2, LOW); digitalWrite(PinIN3, LOW); digitalWrite(PinIN4, LOW); } void TurnRight(){ // L298N asking Motor Turn Right digitalWrite(PinIN1, LOW); digitalWrite(PinIN2, HIGH); digitalWrite(PinIN3, LOW); digitalWrite(PinIN4, HIGH); } void TurnLeft(){ // L298N asking Motor Turn Right digitalWrite(PinIN1, HIGH); digitalWrite(PinIN2, LOW); digitalWrite(PinIN3, HIGH); digitalWrite(PinIN4, LOW); } void Backward(){ // L298N asking Motor Go Backward digitalWrite(PinIN1, LOW); digitalWrite(PinIN2, HIGH); digitalWrite(PinIN3, HIGH); digitalWrite(PinIN4, LOW); } void Fordward(){ // L298N asking Motor Go Fordward digitalWrite(PinIN1, HIGH); digitalWrite(PinIN2, LOW); digitalWrite(PinIN3, LOW); digitalWrite(PinIN4, HIGH); } void BackwardRight(){ digitalWrite(PinIN1, LOW); digitalWrite(PinIN2, HIGH); digitalWrite(PinIN3, LOW); digitalWrite(PinIN4, LOW); } void BackwardLeft(){ digitalWrite(PinIN1, LOW); digitalWrite(PinIN2, LOW); digitalWrite(PinIN3, HIGH); digitalWrite(PinIN4, LOW); } void ForwardRight(){ digitalWrite(PinIN1, HIGH); digitalWrite(PinIN2, LOW); digitalWrite(PinIN3, LOW); digitalWrite(PinIN4, LOW); } void ForwardLeft(){ digitalWrite(PinIN1, LOW); digitalWrite(PinIN2, LOW); digitalWrite(PinIN3, LOW); digitalWrite(PinIN4, HIGH); } void setup() { Serial.begin(9600); Serial.println("Hello! I'm Arduino Car.\n"); // Sets the 4 pins as Output to L298N pinMode(PinIN1,OUTPUT); pinMode(PinIN2,OUTPUT); pinMode(PinIN3,OUTPUT); pinMode(PinIN4,OUTPUT); // Sets the 4 pins as Input from AT89S52 315M pinMode(PinD0,INPUT); pinMode(PinD1,INPUT); pinMode(PinD2,INPUT); pinMode(PinD3,INPUT); } void loop() { BTN_A = digitalRead(PinD0); BTN_B = digitalRead(PinD1); BTN_C = digitalRead(PinD2); BTN_D = digitalRead(PinD3); BTN[0] = BTN_A; BTN[1] = BTN_B; BTN[2] = BTN_C; BTN[3] = BTN_D; // Serial print the AT89S52 315M D0~D3 Status Serial.print("BTN Status: A="); Serial.print(BTN_A); Serial.print(" ; B="); Serial.print(BTN_B); Serial.print(" ; C="); Serial.print(BTN_C); Serial.print(" ; D="); Serial.print(BTN_D); Serial.print(". \t"); Serial.print(". \n"); if (BTN_A == 0 && BTN_B == 0 && BTN_C == 0 && BTN_D == 0){ // ABCD=0000 define STOP StopMoving(); } if (BTN_A == 1 && BTN_B == 0 && BTN_C == 0 && BTN_D == 0){ // ABCD=1000 define fordward Fordward(); } if (BTN_A == 0 && BTN_B == 1 && BTN_C == 0 && BTN_D == 0){ // ABCD=0100 define backward Backward(); } if (BTN_A == 0 && BTN_B == 0 && BTN_C == 1 && BTN_D == 0){ // ABCD=0010 define Turn Right TurnRight(); } if (BTN_A == 0 && BTN_B == 0 && BTN_C == 0 && BTN_D == 1){ // ABCD=0001 define Turn Left TurnLeft(); } if (BTN_A == 1 && BTN_B == 0 && BTN_C == 1 && BTN_D == 0){ // ABCD=1010 define Turn Left for forward ForwardLeft(); } if (BTN_A == 1 && BTN_B == 0 && BTN_C == 0 && BTN_D == 1){ // ABCD=1001 define Turn Right for forward ForwardRight(); } if (BTN_A == 0 && BTN_B == 1 && BTN_C == 1 && BTN_D == 0){ // ABCD=0110 define Turn Left for backward BackwardLeft(); } if (BTN_A == 0 && BTN_B == 1 && BTN_C == 0 && BTN_D == 1){ // ABCD=0101 define Turn Right for backward BackwardRight(); } }
沒有留言:
張貼留言