2017-09-25 583 views
2

我需要幫助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() { 
} 

所以我希望你明白我的意思,我希望我做意義上的空函數。哦,如果你想要,也可以幫助我解決駕駛功能,例如幫助我右轉或左轉或前進或後退功能。那麼你能幫我嗎?

+0

你使用下拉? –

+0

「那麼你能幫我嗎?」。發佈問題就足夠了。你不必求助 – Piglet

回答

0

它不停的原因很簡單:你從未告訴他們停下來。在關閉按鈕的情況下,它將完成start_driving()功能,並且最後的命令對於電機爲高。剩下的時間它將停留在loop()功能的其他部分,並且永遠不會被告知爲低電平。 因此,最後在start_driving()函數中添加一些digitalWrite(motor1/2, LOW);,或者在loop()的其他部分中添加一些digitalWrite(motor1/2, LOW);

我做了一些改變你的代碼,並添加所需的零件(LED有一個像你的汽車同樣的問題):

#define LEDPIN   13  // Use defines, because the values won't change anyway 
            // For the same reason they are Uppercase like LOW, HIGH, INPUT ... 
            // It is an indicator for non changeing values 
#define RIGHTMOTORPIN 11  // Changed to PWM capable Pins, so you can 
#define LEFTMOTORPIN 10  // have a variable speed later on 
#define BUTTONPIN  A0 

#define RIGHT   0   // For more readability in the makeTurn function 
#define LEFT   1 

void setup() { 
    Serial.begin(9600); 

    pinMode(LEDPIN, OUTPUT); 
    pinMode(RIGHTMOTORPIN, OUTPUT); 
    pinMode(LEFTMOTORPIN, OUTPUT); 

    pinMode(BUTTONPIN, INPUT); 
} 

void loop() { 
    if(digitalRead(BUTTONPIN) == LOW){ 
    digitalWrite(LEDPIN, HIGH); 
    Serial.println("Turned on!");    // Put it in front, so it isn't shown after the drive 
    Serial.println("It is driving!"); 
    startDriving(); 
    }else{ 
    Serial.println("Not turned on!"); 
    digitalWrite(LEDPIN, LOW);    // I guess the led should be turned off too. 
    parallelMotorControl(LOW);    // THIS is the reason, why it continued driving! 
               // You never stopped the Motors 
    } 
} 

void startDriving() { 
    parallelMotorControl(HIGH); 
    delay(500); 
    makeTurn(RIGHT); 
    parallelMotorControl(HIGH); 
    delay(500); 
    makeTurn(LEFT); 
    parallelMotorControl(HIGH); 
    delay(1000); 
} 

// Reuse more code, that is similar. 
void makeTurn(bool turnDirection){ 
    if(turnDirection){       // Right turn 
    digitalWrite(RIGHTMOTORPIN, LOW); 
    digitalWrite(LEFTMOTORPIN, HIGH); 
    }else{          // Left turn 
    digitalWrite(RIGHTMOTORPIN, HIGH); 
    digitalWrite(LEFTMOTORPIN, LOW); 
    } 
    delay(500); 
    parallelMotorControl(LOW); 
    delay(78); 
} 

void parallelMotorControl(bool motorState){ 
    digitalWrite(RIGHTMOTORPIN, motorState); 
    digitalWrite(LEFTMOTORPIN, motorState); 
}