Exit status 1. 'else' without a previous 'if'

hi, i am trying to get this code where the color of an led changes depending on the x and y values of a joystick (HW-504). they both work separately but i cannot get the code to work and keep getting
"exit status 1
'else' without a previous 'if'" for all my 'else' commands.

i have the VRX of the joystick connected to A0 and the VRY connected to A1.
the RGB led is connected to 5(red) , 6(green), 7 (blue)
here is my code, if you can find a problem it would be greatly appreciated:

#define VRX_PIN  A0 // Arduino pin connected to VRX pin
#define VRY_PIN  A1 // Arduino pin connected to VRY pin

int xValue = 0; // To store value of the X axis
int yValue = 0; // To store value of the Y axis
int r=5;
int g=6;
int b=7;
void setup() {
  Serial.begin(9600) ;
  pinMode(r,OUTPUT);
  pinMode(g,OUTPUT);
  pinMode(b,OUTPUT);
}

void loop() {

  // read analog X and Y analog values
  xValue = analogRead(VRX_PIN);
  yValue = analogRead(VRY_PIN);

  if (digitalRead(xValue)> 1015)
     (digitalRead(yValue)> 517);
     (digitalRead(yValue)< 520);{
    Serial.println("blue");
    digitalWrite(b,HIGH);
    delay(100);
    }
  else
  {digitalWrite(b,LOW);}

  if (digitalRead(xValue)< 5)
     (digitalRead(yValue)> 517);
     (digitalRead(yValue)== 520);
     (digitalRead(yValue)== 519);
     (digitalRead(yValue)== 518);{
    Serial.println("green");
    digitalWrite(g,HIGH);
    delay(100);
    }
  else
  {digitalWrite(g,LOW);}

  if (digitalRead(yValue)> 1010)
     (digitalRead(xValue)== 513);{
    Serial.println("red");
    digitalWrite(r,HIGH);
    delay(100);
    }
  else
  {digitalWrite(r,LOW);}

  if (digitalRead(yValue)< 5)
     (digitalRead(xValue)== 513);{
    Serial.println("porpol");
    digitalWrite(b,HIGH);
    digitalWrite(r,HIGH);
    delay(100);
    }
  else 
       {digitalWrite(b,LOW);
        digitalWrite(r,LOW);}

  // print data to Serial Monitor on Arduino IDE
  Serial.println("x = ");
  Serial.print(xValue);
  Serial.println(", y = ");
  Serial.print(yValue);
  Serial.println(" ");
}
  1. lose the semi-colons in the if statements
  2. put all of the logical conditions within a single parenthesis block
  3. use AND's to combine the conditions.

Like this:

if ((digitalRead(xValue)> 1015) &&
(digitalRead(yValue)> 517) &&
(digitalRead(yValue)< 520)) {

If you indent the code using CTRL-T it helps to see the structural errors.

This is an isolated if statement, followed by the start of a code block, followed later by an isolated "else".

  if (digitalRead(yValue) > 1010)
    (digitalRead(xValue) == 513); {

Incidentally, this makes no sense, as the result of digitalRead() is either 0 or 1 and the test will always be false.

(digitalRead(xValue) == 513)

Start with reading about c++ syntax and basics, believe you me, this will save you and everyone else great deal of time

Would you also suggest learning c++ as a programming language to help with the coding?

The language that one uses to program Arduino boards is C++ so, yes.

The Arduino language reference has information on the Arduino specific functions and some C++ structures.

I often refer to the Cplusplus.com site for C++ language reference and tutorials.

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