Seek the led game

hello everyone , I'm trying to figure out to use the joystick, seek the led's that light right now and if I do make +1 point and show it in the digital print
when I move the joystick to the led color that on +1 point and when I'm moving to the wrong way I will lose a point and the minimum score is 0

  • a way to restart the game buy moving the joystick In the upper right corner and then in the lower left corner

here is the code right now:


// Joystick variables
int sX = A5;     //joystick x axis, analog input
int sY = A6;     //joystick y axis, analog input
int sSX;         //state of x, reading from sX
int sSY;         //state of y, reading from sY
int sS;          //converted state (may not be most efficient)

// Game variables
int rNum;        //random int choosing the random light to turn on
int wins=0;      //counting consecutive wins
int highScore=0; //saving the highest score of consecutive wins

// Difficulty constants, time to react
const int easy=1000;
const int medium=500;
const int hard=325;
const int stupid=250;


int ledPins[]={6,9,10,11}; //initializing led's
int pinCount=4;                //number of led pins

void setup() {

Serial.begin(9600);
pinMode(sX, INPUT);
pinMode(sY, INPUT);

for (int thisPin = 0; thisPin < pinCount; thisPin++) { //assigning all the pins as outputs in a for loop
    pinMode(ledPins[thisPin], OUTPUT);
  }
}

void loop() {

  rNum=random(4); //generating random choice
  delay(1000);
  digitalWrite(ledPins[rNum], HIGH); //lighting the randomly chosen bulb
  delay(stupid); //difficulty
  
  //stick stuff
  sSX = analogRead(sX); //reading x axis input
  delay(100);
  sSY = analogRead(sY); //reading y axis input

  //converting y and x inputs to 4 possibilities. 0 and 1023 are the maximum values on each axis of the joystick, as measured.
  sS=0;
  switch (sSX) {
  case 0:
  sS=1;      // Left
  break;
  case 1023:
  sS=2;      // Right
  break;
}
switch (sSY) {
  case 0:
  sS=3;      // Up
  break;
  case 1023:
  sS=4;      // Down
  break;
}

  digitalWrite(ledPins[rNum], LOW); //turning off the randomly selected bulb, after the joystick choice was made
  if (sS-1==rNum) //checking if the user input the correct direction of the lit bulb
  {
    wins++;
    for (int k=0; k<=3; k++) {     //blinking green light indicating correct choice
    digitalWrite(ledPins[4], HIGH);
    delay(50);
    digitalWrite(ledPins[4], LOW);
    delay(50);
    }
  }
  else
  {
    if (wins>highScore) { //if the consecutive wins are more than the previous highscore, the new highscore is set.
        highScore=wins;
        wins=0;
    }
    for (int i=0; i<=3; i++) {       //blinking red light indicating incorrect choice
      digitalWrite(ledPins[4], HIGH);
      delay(50);
      digitalWrite(ledPins[4], LOW);
      delay(50);
    }
    for (int w=0; w<highScore; w++) { //displaying via counting and blinking the high score on a yellow bulb.
        digitalWrite(ledPins[4], HIGH);
        delay(200);
        digitalWrite(ledPins[4], LOW);
        delay(200);
    }  
  }  
  }

I hope I write the code as u asked in the rules of the forum.
thanks!

Not quite, but I have corrected it. The easiest way to post code is to use "Copy for forum" in the IDE right click menu. It adds the code tags for you ready to paste here

1 Like

the ide in the forum or on the Arduino program?

The IDE is where you write your sketch for your arduino. It has some useful tools such as “copy for forum” and auto format

What is the problem, exactly?

You have a lot of delays in your program. Remember the microcontroller can do nothing during a delay and so they are generally to be avoided. Look at blink without delay tutorial. Loops should be as fast as possible to be responsive.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.