Trying to make 2 sensors work but this happens

Hi! Wonder if anyone could help me out here with this code because I keep getting several problems. Whenever I try to upload my code, I get an error message saying 'exit status 1 expected unqualified-id before 'if''

Here's my code:

int val = 0;

void setup()
{
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);
Serial.begin(9600);
}
void loop()
{
digitalWrite(9,HIGH);
digitalWrite(8,LOW);
digitalWrite(13,HIGH);
digitalWrite(12,LOW);
}
if(analogRead(A0) > 500) // This is the Left Sensor
{
digitalWrite(8,LOW); // Stops the Right motor from moving
digitalWrite(9,LOW);
}
else
{
digitalWrite(9,HIGH); // Makes the Right motor move foward
digitalWrite(8,LOW);
end
}
if(analogRead(A3) > 500) // This is the Right Sensor
{
digitalWrite(13,LOW); // Stops the Left Motor from moving
digitalWrite(12,LOW);
}
else
{
digitalWrite(13,HIGH); // This makes the Left motor move foward.
digitalWrite(12,LOW);
}
Serial.println(val); // Prints the Sensor Value
}

If you can solve it. Thanks!

You have the if statements outside of the loop function; you cannot have code outside of a function.

It looks like you have an extra } after the block of digitalWrite()'s

Mind showing me an example what you mean by that?

int val = 0;

void setup()
{
  pinMode(8,OUTPUT);
  pinMode(9,OUTPUT);
  pinMode(10,OUTPUT);
  pinMode(11,OUTPUT);
  pinMode(12,OUTPUT);
  pinMode(13,OUTPUT);
  Serial.begin(9600);
}
void loop()
{
  digitalWrite(9,HIGH);
  digitalWrite(8,LOW);
  digitalWrite(13,HIGH);
  digitalWrite(12,LOW);
} //DELETE THIS - this closes the loop() function, so all the code after it is not in a function, which is not valid
if(analogRead(A0) > 500) // This is the Left Sensor
{
  digitalWrite(8,LOW); // Stops the Right motor from moving
  digitalWrite(9,LOW);
}
else
{
  digitalWrite(9,HIGH); // Makes the Right motor move foward
  digitalWrite(8,LOW);
  end
}
if(analogRead(A3) > 500) // This is the Right Sensor
{
  digitalWrite(13,LOW); // Stops the Left Motor from moving
  digitalWrite(12,LOW);
}
else
{
  digitalWrite(13,HIGH); // This makes the Left motor move foward.
  digitalWrite(12,LOW);
}
Serial.println(val); // Prints the Sensor Value
  }

Thanks for that it worked but the second part of the analogRead is still having troubles. Im doing the same way you told me to do it but now the '}' is having problems at the end, I delete the last last one. I delete it and it says it has problems. I put it back. It says "expected '}' at end of input" and if I delete it this is what it says the same thing. :frowning:

Always post the revised code, using code tags.

Ok so a friend helped me out and the code works now! :smiley:

But there is one problem. The wheels wont move and if I try printing the serial so I can see, it's stuck at the number '243' and it wont go up or down.

But the main problem is the sensors work but the wheels don't move one pixel.

See reply #5.

What's a code tag?

Did you notice the How to use this forum post?

Oh oops.

So my code works and all it's just that the wheels wont move at all. I don't know what to do about it.

As before, follow the directions in the "How to use this forum" post and post your code, wiring diagram and links to the parts.

Thusly informed forum members might be able to help.