If Else Problems

I'm having a problem with the if then else. I need to make it so that when two inputs at the same time read then start a motor. I am getting an error exit status 1 'else' without a previous 'if'. Any help or suggestions are greatly appreciated. My code follows.

int buttonpin = 0; //Sets pin 0 to be called buttonpin.
int leftmotor = 10; //Sets pin 10 to control Left Motors.
int rightmotor = 11; //Sets ping 11 to control Right Motor
int leftline = 5; //Left line follower input.
int middleline = 4; //Middle line follower input.
int rightline = 3; //Right line follower input.
int bumper = 7; //Bumber swithch input
//add pins for for ping pong ball device.

void setup() {
pinMode(buttonpin, INPUT); //sets pin 0 as input.
pinMode(leftmotor, OUTPUT); //sets pin 10 as output.
pinMode(rightmotor, OUTPUT); //Sets pin 11 as output.
pinMode(leftline, INPUT); //Sets pin 5 as input.
pinMode(middleline, INPUT); //Sets pin 4 as input.
pinMode(rightline, INPUT); //Sets pin 3 as input.
pinMode(bumper, INPUT); //Sets pin 7 as input.
// configure pins yet to be added listed above.
}

void loop() {
// Main runtime instruction set.
// Read sensor input.
if (digitalRead(leftline == HIGH));{
(digitalRead(rightline == HIGH));
{
digitalWrite,(rightmotor,HIGH);
digitalWrite,(leftmotor,HIGH);

else
{
// Do nothing.
}
void loop() {
// Main runtime instruction set.
// Read sensor input.
if (digitalRead(middleline == HIGH));{
(digitalRead(rightline == HIGH));
{
digitalWrite,(rightmotor,LOW);
digitalWrite,(leftmotor,HIGH);

else
{
// Do nothing.
}
void loop() {
// Main runtime instruction set.
// Read sensor input.
if (digitalRead(leftline == HIGH));{
(digitalRead(middleline == HIGH));
{
digitalWrite,(rightmotor,HIGH);
digitalWrite,(leftmotor,LOW);

else
{
// Do nothing.
}
void loop() {
// Main runtime instruction set.
// Read sensor input.
if (digitalRead(leftline == HIGH));{
(digitalRead(rightline == HIGH));
{
digitalWrite,(rightmotor,HIGH);
digitalWrite,(leftmotor,HIGH);

else
{
// Do nothing.
}
}
}

Tidy the program up removing multiple loops() etc. and repost it using code tags.

I really need help on this and I have been researching and can't find anything similar out there. I'm not sure if this is even possible but any and all help is welcome and appreciated.

Start by following the advice in post #1
How many loop() functions are allowed in one Arduino program, for instance ?

Then spend 2 minutes with Google, and look at what the correct if/else/endif syntax is....

Regards,
Ray L.

Ray L. I am new to the Arduino side of coding. I have extensive technical knowledge but some C++ is way over my head. I was researching but I either didn't understand what it meant or it didn't work. Again any and all help is much appreciated.

Your loop(), of which you can have only one, will look something like this (untested) . . .

void loop() {
  // Main runtime instruction set.
  // Read sensor input.
  if (digitalRead(middleline) == HIGH && digitalRead(rightline) == HIGH ) {
     digitalWrite(rightmotor,LOW);
     digitalWrite(leftmotor,HIGH);
  } 
  else if (digitalRead(leftline) == HIGH && digitalRead(middleline) == HIGH ) {
     digitalWrite(rightmotor,LOW);
     digitalWrite(leftmotor,HIGH);
  } 

  // ... 
  // ... 
  // ... 
  // ... 

  else if (digitalRead(leftline) == HIGH && digitalRead(middleline) == HIGH ) {
     digitalWrite(rightmotor,LOW);
     digitalWrite(leftmotor,LOW);
  } 
  else {
     // Do nothing.
  }
}

6v6gt THANK YOU! The code is valid and works great. :smiley:

Ok, I got a little excited and just though it was working but I went to test it then it only repeats the first line of code. The right motor spins and nothing else happens even when the line sensors are in fact writing correctly, (been checked with multi meter). Again any and all help is appreciated.

If anyone could help with this I have searched but I can't seem to find something that helps. The code looks fine to me but it can't get past that first line.

Too many semicolons. Missing ). Extra ). Syntax.
if (digitalRead(leftline == HIGH));{
^^ ^^

if (digitalRead(leftline) == HIGH){

Extra comma too.
digitalWrite,(rightmotor,HIGH);
^^

You have to complete a table, maybe similar to below, then build your logic statements accordingly:

left-line   middle-line  right-line    |   left-motor   right-motor
LOW         LOW          LOW                  ?                ?
LOW         LOW          HIGH                 ?                ?
LOW         HIGH         LOW                  ?                ?
LOW         HIGH         HIGH                 ?                ?
HIGH        LOW          LOW                  ?                ?
HIGH        LOW          HIGH                 ?                ?
HIGH        HIGH         LOW                  ?                ?
HIGH        HIGH         HIGH                 ?                ?

or find some other way of communicating your requirements.

I am having problems with if then else. I recieved this fixed code below from a forum user but it only repeats the first line of code. This is for a line following robot that senses black line on white paper. Any and all help/input is greatly appreciated. The code is below.

int buttonpin = 0; //Sets pin 0 to be called buttonpin.
int leftmotor = 10; //Sets pin 10 to control Left Motors.
int rightmotor = 11; //Sets ping 11 to control Right Motor
int leftline = 5; //Left line follower input.
int middleline = 4; //Middle line follower input.
int rightline = 3; //Right line follower input.
int bumper = 7; //Bumber swithch input
//add pins for for ping pong ball device.

void setup() {
pinMode(buttonpin, INPUT); //sets pin 0 as input.
pinMode(leftmotor, OUTPUT); //sets pin 10 as output.
pinMode(rightmotor, OUTPUT); //Sets pin 11 as output.
pinMode(leftline, INPUT); //Sets pin 5 as input.
pinMode(middleline, INPUT); //Sets pin 4 as input.
pinMode(rightline, INPUT); //Sets pin 3 as input.
pinMode(bumper, INPUT); //Sets pin 7 as input.
// configure pins yet to be added listed above.
}

void loop() {
// Main runtime instruction set.
// Read sensor input.
if (digitalRead(leftline == HIGH));{
(digitalRead(rightline == HIGH));
{
digitalWrite,(rightmotor,HIGH);
digitalWrite,(leftmotor,HIGH);

else
{
// Do nothing.
}
void loop() {
// Main runtime instruction set.
// Read sensor input.
if (digitalRead(middleline == HIGH));{
(digitalRead(rightline == HIGH));
{
digitalWrite,(rightmotor,LOW);
digitalWrite,(leftmotor,HIGH);

else
{
// Do nothing.
}
void loop() {
// Main runtime instruction set.
// Read sensor input.
if (digitalRead(leftline == HIGH));{
(digitalRead(middleline == HIGH));
{
digitalWrite,(rightmotor,HIGH);
digitalWrite,(leftmotor,LOW);

else
{
// Do nothing.
}
void loop() {
// Main runtime instruction set.
// Read sensor input.
if (digitalRead(leftline == HIGH));{
(digitalRead(rightline == HIGH));
{
digitalWrite,(rightmotor,HIGH);
digitalWrite,(leftmotor,HIGH);

else
{
// Do nothing.
}
}
}

Sorry I put the wrong code above. The correct code is here.
void loop() {
// Main runtime instruction set.
// Read sensor input.
if (digitalRead(middleline) == HIGH && digitalRead(rightline) == HIGH ) {
digitalWrite(rightmotor,LOW);
digitalWrite(leftmotor,HIGH);
}
else if (digitalRead(leftline) == HIGH && digitalRead(middleline) == HIGH ) {
digitalWrite(rightmotor,LOW);
digitalWrite(leftmotor,HIGH);
}

// ...
// ...
// ...
// ...

else if (digitalRead(leftline) == HIGH && digitalRead(middleline) == HIGH ) {
digitalWrite(rightmotor,LOW);
digitalWrite(leftmotor,LOW);
}
else {
// Do nothing.
}
}

left-line middle-line right-line | left-motor right-motor
LOW LOW LOW LOW LOW
LOW LOW HIGH LOW LOW
LOW HIGH LOW LOW LOW
LOW HIGH HIGH LOW HIGH
HIGH LOW LOW LOW LOW
HIGH LOW HIGH HIGH HIGH
HIGH HIGH LOW HIGH LOW
HIGH HIGH HIGH HIGH HIGH

This is how it is supposed to be configured except the first if repeats even if there is no signal and if there is no input on any sensor I want it to do nothing. Thank you all so much you are being very helpful.

The correct code is here.

Well, some of it, anyway.

After 10 posts, it's time you learned to post code CORRECTLY. Read the stickies at the top of the forum - the ones you were supposed to read BEFORE posting anything.

Before you post any more code, put EVERY { on a line BY ITSELF.

Use Tools + Auto Format to properly indent the code.

Then, post ALL of the code.

but it only repeats the first line of code.

Some proof of that would be useful.

if (digitalRead(leftline == HIGH));

Oh dear.

I figured it out and thank you to everyone that helped me.