Arduino按钮开关功能

时间:2017-09-25 04:33:10

标签: button 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文件再次启动(我仍然保留第一个仍然是),但我试图打开和关闭按钮功能来打开led并激活一个void函数叫

void start_driving() {
}

所以我希望你明白我的意思,我希望我有意义。哦,如果你想要的话,你可以帮我解决驾驶功能,比如帮我转右或左转或前进或后退功能。那么请你帮帮我吗?

1 个答案:

答案 0 :(得分:0)

它不停止的原因很简单:你从未告诉他们停止。在关闭按钮的情况下,它将完成start_driving()功能,并且电机的最后一个命令为HIGH。其余的时间它将保留在loop()函数的else部分,并且永远不会被告知为低。 因此,最后在digitalWrite(motor1/2, LOW);函数中添加一些start_driving(),或在loop()其他部分添加#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); }

我对您的代码进行了一些更改并添加了所需的部件(LED具有与您的电机相同的问题):

{{1}}