49Views4Replies

Author Options:

What is wrong with my memory game code |Arduino? Answered

Hello!

I am having problem with my code, it doesn't start at all..

//Joystick
const int X_pin = A4;
const int Y_pin = A5;
int X_Value = 0;//Waarde X-Axis Joystick
int Y_Value = 0;//Waarde Y-Axis Joystick

//leds
const int led_Rood = 13;
const int led_Groen = 12;
const int led_Blauw = 11;
const int led_Geel = 10;
//componenten
const int buzzer = 4;

const int level_Max = 20;
int level[level_Max];
int level_Index = 0;



void setup() {
  randomSeed(analogRead(0));
  pinMode (led_Rood, OUTPUT);
  pinMode (led_Groen, OUTPUT);
  pinMode (led_Blauw, OUTPUT);
  pinMode (led_Geel, OUTPUT);

  pinMode(buzzer, OUTPUT);

  Serial.begin(9600);
  Serial.println("Het spel is begonnen!");
}
void loop() {

  X_Value = analogRead(X_pin);
  Y_Value = analogRead(Y_pin);

  int joy_Blauw = (X_Value == 0);
  int joy_Groen = (X_Value == 1023);
  int joy_Geel = (Y_Value == 1023);
  int joy_Rood = (Y_Value == 0);
  int huidig = 0;
  int random_leds = random(10,14);
  level[level_Index] = random_leds;
 
  delay(5000);
  while (huidig < level_Index){
    int laatste_input = 0; // laatste input van de joystick
    while ((joy_Rood || joy_Geel || joy_Groen || joy_Blauw )== HIGH){
      if (joy_Rood == HIGH){
        laatste_input = led_Rood;
        digitalWrite(led_Rood, HIGH);
      }else{
        digitalWrite(led_Rood, LOW);
      }
        if (joy_Geel == HIGH){
        laatste_input = led_Geel;
        digitalWrite(led_Geel, HIGH);
      }else{
        digitalWrite(led_Geel, LOW);
      }
        if (joy_Groen == HIGH){
        laatste_input = led_Groen;
        digitalWrite(led_Groen, HIGH);
      }else{
        digitalWrite(led_Groen, LOW);
      }
        if (joy_Blauw == HIGH){
        laatste_input = led_Blauw;
        digitalWrite(led_Blauw, HIGH);
      }else{
        digitalWrite(led_Blauw, LOW);
      }
    }
    digitalWrite(laatste_input, LOW);
    if(laatste_input !=0){
      if(level[huidig] != laatste_input){
        reset();
        laatste_input = 0;
        break;
      }else{
        huidig++;
        laatste_input =0;
      }
    }
  }
}
void reset(){
  for (int i = 0; i < 14; i++){
    digitalWrite(led_Rood, HIGH);
    digitalWrite(led_Groen, HIGH);
    digitalWrite(led_Geel, HIGH);
    digitalWrite(led_Blauw, HIGH);
    delay(500);
    digitalWrite(led_Rood, LOW);
    digitalWrite(led_Groen, LOW);
    digitalWrite(led_Geel, LOW);
    digitalWrite(led_Blauw, LOW);
    delay(500);
   
  }
  level_Index = 0;
  delay(3000);
}

I am using this tutorial.
(joystick instead of buttons)
I can't seem to find what I am doing wrong can someon help me? 

Thank you in advance!

Discussions

0
None
Codingpro

11 months ago

is your serial speed set to 9600?

0
None
Codingpro

11 months ago

int joy_Blauw = (X_Value == 0);

int joy_Groen = (X_Value == 1023);
int joy_Geel = (Y_Value == 1023);
int joy_Rood = (Y_Value == 0);
int huidig = 0;

in a void loop?? It will keep recreating the variables. make the variables at the top and set it like this;

joy_Blauw = (X_Value == 0);int joy_Groen = (X_Value == 1023);

joy_Geel = (Y_Value == 1023);
joy_Rood = (Y_Value == 0);
huidig = 0;

that part should work better, but it wouldn't effect the code that much.

0
None
jfryar30272

11 months ago

First thing that I notice is that your "const int X_pin = A4;" is incorrect. Remove the "A" from the assigned value. It should look like "const int X_pin = 4;". Update this for the X_pin and the Y_pin. If this doesn't fix it, let me know and I'll take another look.

Good luck!

0
None
steveastrouk

1 year ago

Nothing, not even the words "The game has started", on your serial port ?