Arduino项目伺服故障(记忆游戏)

时间:2018-04-26 16:44:00

标签: memory arduino arduino-uno servo

我最近使用Rex Qualis Arduino Uno R3进入Arduino,我正在尝试建立一个能够击败Simon记忆游戏(或重复节拍)的项目。

它通过四个按钮之一等待用户响应,然后将其添加到列表中,执行列表,然后在下一步移动时等待用户输入。

一切都按照它应有的方式运作,但最奇怪的事情在执行时发生:

  • 在完全执行后的第一个循环中,Servo 1将在未经授权的情况下执行其移动功能。
  • 在完全执行后的第二个循环中,Servo 2将执行其移动功能,依此类推。
  • 在第四次循环,执行和伺服4执行其移动功能之后,它不会再次发生。我不知道为什么它在前四个循环中逐个循环通过所有伺服系统然后很好,但它有点打破了我的项目。

我的代码中是否存在重定向到移动函数或其他内容的问题?所有帮助表示赞赏。以下是供参考的代码:

//Simon killer
//Da Cube

#include <Servo.h>

//Declare buttons
int button1 = 4;
int button2 = 5;
int button3 = 6;
int button4 = 7;

//Declare servos
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;

int moves[100]; //Memory up to 100
int x = 0;
int y = 1;

void setup() {
  pinMode(button1, INPUT_PULLUP); //Button setup
  pinMode(button2, INPUT_PULLUP);
  pinMode(button3, INPUT_PULLUP);
  pinMode(button4, INPUT_PULLUP);

  servo1.attach(8); //Servo setup
  servo2.attach(9);
  servo3.attach(10);
  servo4.attach(11);

  moveServo1();//System check
  moveServo2();
  moveServo3();
  moveServo4();
}

//move functions
void moveServo1() {
  servo1.write(5);
  delay(500);
  servo1.write(45);
  delay(500);
}

void moveServo2() {
  servo2.write(5);
  delay(500);
  servo2.write(45);
  delay(500);
}

void moveServo3() {
  servo3.write(175);
  delay(500);
  servo3.write(135);
  delay(500);
}

void moveServo4() {
  servo4.write(5);
  delay(500);
  servo4.write(45);
  delay(500);
}

void loop() {
  //Read Input by button
  while (x < y) {
    if (digitalRead(button1) == LOW) {
      moves[x] = 1;
      x++;
    } else if (digitalRead(button2) == LOW) {
      moves[x] = 2;
      x++;
    } else if (digitalRead(button3) == LOW) {
      moves[x] = 3;
      x++;
    } else if (digitalRead(button4) == LOW) {
      moves[x] = 4;
      x++;
    }
  }
  y++;

  //Decode Memory Array
  for (int i = 0; i < (sizeof(moves)); i++) {
    switch (moves[i]) {
      case 1:
       moveServo1();
       break;
      case 2:
       moveServo2();
       break;
      case 3:
       moveServo3();
       break;
      case 4:
       moveServo4();
       break;
    }
  } 
}

1 个答案:

答案 0 :(得分:0)

首先,我会检查使Servos移动1-4的代码不是设置循环中的代码。

val parallelism = 10
val executor = Executors.newFixedThreadPool(parallelism)
val ec: ExecutionContext = ExecutionContext.fromExecutor(executor)
val tasks: Seq[String] = ???
val results: Seq[Future[Int]] = tasks.map(query => {
  Future{
    //spark stuff here
    0
  }(ec)
})
val allDone: Future[Seq[Int]] = Future.sequence(results)
//wait for results
Await.result(allDone, scala.concurrent.duration.Duration.Inf)
executor.shutdown //otherwise jvm will probably not exit 

在这里你进行伺服系统检查,这意味着每次启动arduino时,第一个伺服将移动,然后是第二个,依此类推,然后只有空循环开始...注释这些线,看看是否帮助