0

I recently got into Arduino with a Rex Qualis Arduino Uno R3 and I am trying to build a project that would beat the Simon memory game (or Repeat the Beat).

It waits for user response through one of four buttons then adds that to the list, executes the list, then waits for user input on the next move.

Everything works how it's supposed to, but the weirdest things happen on execution:

  • On the first loop after full execution, Servo 1 will execute its move function without authorization.
  • On the second loop after full execution, Servo 2 will execute its move function and so on.
  • After the fourth loop, execution, and servo 4 executing its move function, it doesn't happen again. I don't know why it cycles through all the servos one by one in the first four loops then is fine after but it kinda breaks my project.

Is there a problem in my code that redirects to the move functions or something? All help is appreciated. Here is the code for reference:

//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;
    }
  } 
}
dda
  • 6,030
  • 2
  • 25
  • 34
Da Cube
  • 1
  • 3

1 Answers1

0

First i would check to see if the code that makes the Servos move 1-4 isn't the one in the setup loop.

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

Here you make a servo sistem check, which means every time you power up the arduino, the first servo will move, then the second and so on and only then the void loop starts...comment these lines and see if that helps

  • I wish this was the answer but I power it up and it does everything I tell it to in the setup. The loop is the one with the problem. It’s a while loop and a switch and case. Idk where the extra commands are coming from. Thank you for your input though. – Da Cube Apr 27 '18 at 13:07
  • Well, i had to eliminate that possibility since i don't know how well you go along with code, Arduino and stuff :) Hope i didn't offend you. The code looks good to me, it is weird indeed. Can you get a visual on the array? Use Serial.print to see how that array looks. If the first 4 values are 1 2 3 4 then the problem might be in the while loop – user5467736 Apr 27 '18 at 13:27
  • Hey no not at all! I apologize if I came off short. Visualizing the array seems like a good idea so I’ll do that and get back to you. – Da Cube Apr 27 '18 at 13:29
  • Something is extremely wrong as the serial prints out a ton of numbers and some are negative...the problem MUST be in the “while” loop – Da Cube Apr 27 '18 at 14:11
  • Please copy and paste here the content of the array or whatever values Arduino returns...also, i suspect there is something wrong with the way you handle buttons, i mean the physical wiring, can you provide more data on that? – user5467736 Apr 27 '18 at 21:43
  • I found the problem... my array decoder relies on the length of the array and at the beginning I declare ‘moves[100]’ I am currently working on a solution that uses a dynamic memory between two arrays. – Da Cube Apr 27 '18 at 21:49
  • Awesome! Glad you found the problem. You can use this article to get a grip on declaring dynamic arrays. Cheers! https://stackoverflow.com/questions/3851181/define-array-then-change-its-size – user5467736 Apr 27 '18 at 22:05
  • Sure, glad to help. If anything else comes up in the future, don't hesitate to ask – user5467736 Apr 27 '18 at 22:14