我需要幫助Arduino中的按鈕打開和關閉。我想在我的技術課上爲我的課程製作自己的Arduino汽車。Arduino按鈕開啓和關閉功能
這是一個連接到2個電機,Arduino板和Arduino傳感器屏蔽v5.0的晶體管電路。我輸入的代碼用於開/關功能,但它不會關閉,只能保持永久。
代碼的第一個:
//variables
int led = 13; //led on indicator
int motor1 = 12; //right motor
int motor2 = 11; //left motor
int button = A0; //on and off button
int val = 0; //value for on press button
void setup() {
//setting up code to definition of pins
Serial.begin(9600);
pinMode(led, OUTPUT);
pinMode(motor1, OUTPUT);
pinMode(motor2, OUTPUT);
pinMode(button, INPUT);
}
void loop() {
//looping the code to execute
//button code for on and off
val = digitalRead(button);
if (val == LOW) {
digitalWrite(led, HIGH);
start_driving();
Serial.println("Turned on!");
Serial.println("It is driving!");
} else {
Serial.println("Not turned on!");
}
}
//functions
void start_driving() {
//The driving function
digitalWrite(motor1, HIGH);
digitalWrite(motor2, HIGH);
delay(500);
turn_left();
digitalWrite(motor1, HIGH);
digitalWrite(motor2, HIGH);
delay(500);
turn_right();
digitalWrite(motor1, HIGH);
digitalWrite(motor2, HIGH);
delay(1000);
}
//turn right
void turn_right() {
//turning right at 90 degrees
digitalWrite(motor1, LOW);
digitalWrite(motor2, HIGH);
delay(500);
digitalWrite(motor1, LOW);
digitalWrite(motor2, LOW);
delay(78);
}
//turn left
void turn_left() {
//turning left a 90 degrees
digitalWrite(motor1, HIGH);
digitalWrite(motor2, LOW);
delay(500);
digitalWrite(motor1, LOW);
digitalWrite(motor2, LOW);
delay(78);
}
現在我想提出一個新的.ino文件重新開始(我仍然保持第一位仍然),但我試圖讓一個按鈕和關閉功能開啓的領導,也被稱爲激活
void start_driving() {
}
所以我希望你明白我的意思,我希望我做意義上的空函數。哦,如果你想要,也可以幫助我解決駕駛功能,例如幫助我右轉或左轉或前進或後退功能。那麼你能幫我嗎?
你使用下拉? –
「那麼你能幫我嗎?」。發佈問題就足夠了。你不必求助 – Piglet