Arduino:同时使用多个输入

时间:2015-11-21 15:04:41

标签: arduino

我正在进行简单的汽车转向,所以我已经制作了2个前/后按钮和2个左/右转向按钮。我有2个LED,只是看看它是如何工作的,但它不适合我喜欢的方式。我一次只能使用一个按钮,所以在前进的过程中,我无法向左/向右移动等。如何做到这一点?这是我在IDE中的代码:

   int LED = 12;
   int LED2 = 2;
   int ButtonUP = 9;
   int ButtonDOWN = 7;
   int ButtonLEFT = 4;
   int ButtonRIGHT = 8;

   int sygnalUP = 0;
   int sygnalDOWN = 0;
   int sygnalLEFT = 0;
   int sygnalRIGHT = 0;

 void setup() {
pinMode (LED, OUTPUT);
pinMode (ButtonUP, INPUT_PULLUP);
pinMode (ButtonDOWN, INPUT_PULLUP);
pinMode (ButtonLEFT, INPUT_PULLUP);
pinMode (ButtonRIGHT, INPUT_PULLUP);
Serial.begin(9600);
Serial.println("     |GORA|DOL|LEW|PRA");
}

 void loop() {
 int buttonUPst = digitalRead(ButtonUP);
 int buttonDOWNst = digitalRead(ButtonDOWN);
 int buttonLEFTst = digitalRead(ButtonLEFT);
 int buttonRIGHTst = digitalRead(ButtonRIGHT);


 if (buttonUPst == LOW) {
 digitalWrite(12, HIGH); 
 sygnalUP = 1;
 Serial.print("Sygnal: ");
 Serial.print(sygnalUP);
 Serial.print(" | ");
 Serial.print(sygnalDOWN);
 Serial.print(" | ");
 Serial.print(sygnalLEFT);
 Serial.print(" | ");
 Serial.println(sygnalRIGHT);
 delay(300);
 digitalWrite(12, LOW);
 sygnalUP = 0;
 }

 if (buttonDOWNst == LOW) {
 digitalWrite(12, HIGH); 
 sygnalDOWN = 1;
 Serial.print("Sygnal: ");
 Serial.print(sygnalUP);
 Serial.print(" | ");
 Serial.print(sygnalDOWN);
 Serial.print(" | ");
 Serial.print(sygnalLEFT);
 Serial.print(" | ");
 Serial.println(sygnalRIGHT);
 delay(300);
 digitalWrite(12, LOW);
 sygnalDOWN = 0;
 }

 if (buttonLEFTst == LOW) {
 digitalWrite(LED2, HIGH); 
 sygnalLEFT = 1;
 Serial.print("Sygnal: ");
 Serial.print(sygnalUP);
 Serial.print(" | ");
 Serial.print(sygnalDOWN);
 Serial.print(" | ");
 Serial.print(sygnalLEFT);
 Serial.print(" | ");
 Serial.println(sygnalRIGHT);
 delay(300);
 digitalWrite(LED2, LOW);
 sygnalLEFT = 0;
 }

 if (buttonRIGHTst == LOW) {
 digitalWrite(LED2, HIGH); 
 sygnalRIGHT = 1;
 Serial.print("Sygnal: ");
 Serial.print(sygnalUP);
 Serial.print(" | ");
 Serial.print(sygnalDOWN);
 Serial.print(" | ");
 Serial.print(sygnalLEFT);
 Serial.print(" | ");
 Serial.println(sygnalRIGHT);
 delay(300);
 digitalWrite(LED2, LOW);
 sygnalRIGHT = 0;
 }
}

正如您所看到的,我已经制作了代码来查看输入如何串行工作,它看起来像这样:

     |GORA|DOL|LEW|PRA
Sygnal: 1 | 0 | 0 | 0
Sygnal: 1 | 0 | 0 | 0
Sygnal: 1 | 0 | 0 | 0
Sygnal: 1 | 0 | 0 | 0
Sygnal: 0 | 1 | 0 | 0
Sygnal: 0 | 1 | 0 | 0
Sygnal: 0 | 1 | 0 | 0
Sygnal: 0 | 1 | 0 | 0
Sygnal: 0 | 0 | 1 | 0

在这里你还可以看到我不能让两个“1”并排。我知道我在这里犯了一个基本错误,但我的知识很差。谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

你快到了!

您在表格中打印Sygnal值,但各个if块一直保证只有其中一个是真的。但是,您在每个循环开始时读取的button值可以同时为LOW。所以你可能会做类似以下的事情:

  • 只需根据Sygnal值(以及相应的loop() s}每button次更新一次digitalWrite()值。
  • 断言多个delay()时,只使用一个digitalWrite()
  • 如果您仍想打印Sygnal值,只需在loop()末尾打印一次。

编辑:这是以上几行的修改后的loop()。希望这会有所帮助。

void printStates() {
  Serial.print("Sygnal: ");
  Serial.print(sygnalUP);
  Serial.print(" | ");
  Serial.print(sygnalDOWN);
  Serial.print(" | ");
  Serial.print(sygnalLEFT);
  Serial.print(" | ");
  Serial.println(sygnalRIGHT); 
  Serial.flush(); // block execution for as long as needed to print row
}

void loop() {
  int buttonUPst = digitalRead(ButtonUP);
  int buttonDOWNst = digitalRead(ButtonDOWN);
  int buttonLEFTst = digitalRead(ButtonLEFT);
  int buttonRIGHTst = digitalRead(ButtonRIGHT);

  if (buttonUPst == LOW) {
    digitalWrite(LED, HIGH); 
    sygnalUP = 1;
  } else {
    digitalWrite(LED, LOW); 
    sygnalUP = 0;
  }

  // ...Repeat above if-else for other three button directions here...

  printStates();
}