Problem with joystick simulating code

Hi.

I’m having a problem with this code.

Basically, I’m trying to build a digital game using 2 pots to simulate a joysticks x and y axis.

What happens is, when the joystick is centered, and you press the start button a random number is generated and it turns on a light in either a North, South, East or West position. The idea of the game is to get the joystick to the position within a time limit to win.

My problem is that it keeps going to the lose loops.

Could someone please show me what i’m doing wrong?

int xPin = A1;
int yPin = A0;

int xPosition = 0;
int yPosition = 0;

int StartButton = 10;
int SwitchState = 0;

int ledUP = 2;
int ledDOWN = 3;
int ledLEFT = 4;
int ledRIGHT = 5;
int ledWIN = 6;
int ledLOSE = 7;
int ledCENTRE = 8;

int WinTime = 3000;

long randNumber;

void setup()
{
  Serial.begin(9600);
  
  pinMode(xPin, INPUT);
  pinMode(yPin, INPUT);
  pinMode (StartButton, INPUT);
  
  pinMode(ledUP,OUTPUT);
  pinMode(ledDOWN,OUTPUT);
  pinMode(ledLEFT,OUTPUT);
  pinMode(ledRIGHT,OUTPUT);
  
  pinMode(ledWIN,OUTPUT);
  pinMode(ledLOSE,OUTPUT);
  pinMode(ledCENTRE,OUTPUT);
  
  digitalWrite (ledUP,LOW);
  digitalWrite (ledDOWN,LOW);
  digitalWrite (ledLEFT,LOW);
  digitalWrite (ledRIGHT,LOW);
  digitalWrite (ledCENTRE,LOW);
  digitalWrite (ledWIN,LOW);
  digitalWrite (ledLOSE,LOW);
 
     
  //randomSeed(analogRead(5));  // needs to be in the main loop to generate number each game

}




void loop() 
{ 
  xPosition = analogRead(xPin);
  yPosition = analogRead(yPin);
  SwitchState = digitalRead (StartButton);
  
  Serial.print("X: ");
  Serial.print(xPosition);
  Serial.print(" | Y: ");
  Serial.println(yPosition);
  Serial.println (randNumber);
  delay (50);

//randomSeed(analogRead(5));
//randNumber = random(2, 6);

 //joystick centreing code including tolerences
if (xPosition >480 && xPosition <540 && yPosition >480 && yPosition <540)
  {  
       {
        digitalWrite (ledUP,LOW);
        digitalWrite (ledDOWN,LOW);
        digitalWrite (ledLEFT,LOW);
        digitalWrite (ledRIGHT,LOW);
        digitalWrite (ledCENTRE,HIGH);
        digitalWrite (ledWIN,LOW);
        digitalWrite (ledLOSE,LOW);
       }
 
 // start button loop starts here
 if (SwitchState == HIGH)
     {
      {
        analogRead(xPin);
        analogRead(yPin);
       randomSeed(analogRead(5));
        randNumber = random(2, 6);
        delay (500);
       }
 
 //UP light ON.
      if(randNumber == 2) 
      {
            {
            analogRead(yPin);
            digitalWrite (ledUP,HIGH);
            delay (WinTime);
            analogRead(yPin);
            }     
      
      if (yPosition < 100)
             {
               Serial.print("UP WIN LOOP");
               analogWrite (ledCENTRE,LOW);
               analogWrite (ledWIN,HIGH);
               analogWrite (ledLOSE,LOW);
               delay (3000);
               analogWrite (ledWIN,LOW); 
             }
          
           else {
              Serial.print("UP LOSE LOOP");
              digitalWrite (ledCENTRE,LOW); 
              digitalWrite (ledLOSE,HIGH);
              digitalWrite (ledWIN,LOW);
              delay (2000);
              }  
       }
      
      
//DOWN light ON.
       else if (randNumber == 3) 
      {
            {
            analogRead(yPin);
            digitalWrite (ledDOWN,HIGH);
            delay (WinTime);
            //analogRead(yPin);
            }
                   if (yPosition > 923)
                   {
                     Serial.print("DOWN WIN LOOP");
                     digitalWrite (ledCENTRE,LOW);
                     digitalWrite (ledWIN,HIGH);
                     digitalWrite (ledLOSE,LOW);
                     delay (3000);
                     digitalWrite (ledWIN,LOW);
                   }
                   else {
                        Serial.print("DOWN LOSE LOOP");
                        digitalWrite (ledCENTRE,LOW); 
                        digitalWrite (ledLOSE,HIGH);
                        digitalWrite (ledWIN,LOW);
                        delay (2000);
                        }
      }
      
     
      
      
//LEFT light ON.
      
      else if (randNumber == 4) 
      {
        {
        analogRead(xPin);
        digitalWrite (ledLEFT,HIGH);
        delay (WinTime);
        //analogRead(xPin);     
        }
        
        if (xPosition < 100)
             { 
               Serial.print("LEFT WIN LOOP");
               digitalWrite (ledCENTRE,LOW);
               digitalWrite (ledWIN,HIGH);
               digitalWrite (ledLOSE,LOW);
               delay (3000);
               digitalWrite (ledWIN,LOW);
             }
             else {
                  Serial.print("LEFT LOSE LOOP");
                  digitalWrite (ledCENTRE,LOW); 
                  digitalWrite (ledLOSE,HIGH);
                  digitalWrite (ledWIN,LOW);
                  delay (2000);
                  }
       }
      
      
      
      
      
//RIGHT light ON.
      else if (randNumber == 5) 
          {
           { 
            analogRead(xPin);
            digitalWrite (ledRIGHT,HIGH);
            delay (WinTime);
            analogRead(xPin);
          }
          
              if (xPosition > 923)
                 {  
                   Serial.print("RIGHT WIN LOOP");
                   digitalWrite (ledCENTRE,LOW);
                   digitalWrite (ledWIN,HIGH);
                   digitalWrite (ledLOSE,LOW);
                   delay (3000);
                   digitalWrite (ledWIN,LOW);
                 }
                 
                 else {
                      Serial.print("RIGHT LOSE LOOP");
                      digitalWrite (ledCENTRE,LOW); 
                      digitalWrite (ledLOSE,HIGH);
                      digitalWrite (ledWIN,LOW);
                      delay (2000);
                      }
     
     }
// start button loop ends here
       
}
}
}

SKETCH_MAY_1st.ino (5.24 KB)

Please do us a favour and post the program here to avoid the need to download it. When you post it please be sure to put it in code tags to make it easier to read and copy.

You have way too many curly braces. I am certain that you have blocks of code inside blocks of code correctly.

Get rid of any
{
{
crap.

I'm sure that once you do, you'll see exactly what the problem is.

If not, print the code out. Draw boxes around the blocks of code. Are all the boxes where they belong?

How is this problem different from your other Thread?

Unless it is for a different project please ask the moderator to merge the two Threads so everyone can see all the information.

...R

I checked the brackets and they seem to be ok..I think.

I don't see why it skips the win loops even when I twist to pot to the right position?

Is there something I'm forgetting to do with the pots?

Is there something I'm forgetting to do with the pots?

Smoke them?

I checked the brackets and they seem to be ok..I think.

They are not.