步驟1:設(shè)置
軟件
在此步驟中,我將提供零件清單和下載Arduino軟件的鏈接。我為此項(xiàng)目使用了Arduino Create。您可以使用Arduino程序執(zhí)行相同的操作,該程序可從此處下載。
部件列表:
Arduino Uno(其他板可能
標(biāo)準(zhǔn)直流電動機(jī)
L293DNE H橋
2個按鈕
跳線
面包板
所有這些物品都可以很便宜地在線購買。
步驟2:串行通信-接線
首先,讓我們進(jìn)行串行通信。如圖所示連接Arduino。
第3步:串行通信-代碼
現(xiàn)在,您已將Arduino連接好,將代碼復(fù)制并粘貼到Arduino IDE。將讀取此代碼的作用,以獲取您將在串行監(jiān)視器中手動鍵入的信號。輸入1或2時,電動機(jī)將在短時間內(nèi)順時針或逆時針旋轉(zhuǎn)。做一點(diǎn)實(shí)驗(yàn)!鍵入多個1或2,然后看看會發(fā)生什么!
int in1pin = 6;
int in2pin = 7; // connections to H-Bridge, clockwise / counter
char receivedChar; // store info
boolean newData = false; // create a true/false statement
void setup() {
pinMode(in1pin, OUTPUT);
pinMode(in2pin, OUTPUT); // set pins to OUTPUTS
Serial.begin(9600); // start up serial communication
}
void loop() {
recvData(); // read and store data
moveMotor(); // move motor according to data and then reset
}
void recvData() {
if (Serial.available() 》 0) { // if the serial monitor has a reading
receivedChar = Serial.read(); // set char to be what is read
newData = true; // make statement true
}
}
void moveMotor() {
int motordirection = (receivedChar - ‘0’); // turn recieved data into usable form and give it a name
while(newData == true) {
Serial.println(motordirection); // print motor direction
if (motordirection == 1) { // if it reads 1.。.
digitalWrite(in1pin, HIGH); // turn motor one way
digitalWrite(in2pin, LOW);
delay(250);
}
else if (motordirection == 2) { // if it reads 2.。.
digitalWrite(in1pin, LOW); // turn motor other way
digitalWrite(in2pin, HIGH);
delay(250);
}
else { // if nothing is read
digitalWrite(in1pin, LOW); // motor is off
digitalWrite(in2pin, LOW);
}
newData = false; // reset value to false
}
}
步驟4:按鈕命令-接線
要進(jìn)行按鈕通訊,請如圖所示添加按鈕。
第5步:按鈕命令-代碼
現(xiàn)在,制作新草圖并復(fù)制并粘貼此代碼,然后播放周圍。
int in1pin = 6;
int in2pin = 7; // h bridge pins
int leftButton = 8;
int rightButton = 9; // buttons
void setup() {
pinMode(in1pin, OUTPUT);
pinMode(in2pin, OUTPUT); // outputs
pinMode(leftButton, INPUT_PULLUP);
pinMode(rightButton, INPUT_PULLUP); // inputs w internal pullup resistors
}
void loop() {
int leftPinState = digitalRead(leftButton);
int rightPinState = digitalRead(rightButton); // set value names for read data
if (leftPinState == LOW) { // if left button is pressed 。..
digitalWrite(in1pin, HIGH); // make motor go one way
digitalWrite(in2pin, LOW);
}
else if (rightPinState == LOW) { // if right button is pressed 。..
digitalWrite(in1pin, LOW);
digitalWrite(in2pin, HIGH); // make motor go other way
}
else { // if neither button is pressed 。..
digitalWrite(in1pin, LOW); // nothing happens
digitalWrite(in2pin, LOW);
}
}
-
直流電機(jī)
+關(guān)注
關(guān)注
36文章
1710瀏覽量
70156 -
Arduino
+關(guān)注
關(guān)注
188文章
6468瀏覽量
186950
發(fā)布評論請先 登錄
相關(guān)推薦
評論