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; } } } 

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

8 Comments

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.
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
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.
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
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?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.