I don't know whats wrong with my code...

Hello, i'm working on a project with servo motor's and micro switches and am having a problem with the code, here is the code (ill explain the problem shortly):

 const int Lsencor = 4;
const int Rsencor = 7;
#include <Servo.h>
Servo myservo;
Servo myservo2;
int Lsencorvar = 0;
int Rsencorvar = 0;

void setup() 
{
  pinMode (Lsencor, INPUT);
  pinMode (Rsencor, INPUT);
  myservo.attach(5); //Left wheel
  myservo2.attach(6); //Right wheel
}

void forward()
{
  myservo.write(10);
  myservo2.write(180);
}

void turnright()
{
  myservo.write(92);
  myservo2.write(92);
  delay(250);
  myservo.write(180);
  myservo2.write(10);
  delay(500);
  myservo.write(45);
  delay(500);
}
void turnleft()
{
  myservo.write(92);
  myservo2.write(92);
  delay(250);
  myservo.write(180);
  myservo2.write(10);
  delay(500); <--------------------------PROBLEM AREA
  myservo.write(150);
  delay(500);
}

void back()
{
  myservo.write(180);
  myservo2.write(10);
}

void loop()
{
  Lsencorvar = digitalRead (Lsencor);
  Rsencorvar = digitalRead (Rsencor);
  if (Lsencor == HIGH)
  {
    turnright();
  }
  if (Rsencor == HIGH)
  {
    turnleft();
  }
  else
  {
    forward
  }
}

if you saw the arrow pointing to the delay then you just looked at my problem. My problem is when i run that part of the code the board resets every time it gets to that delay. any suggestions on how to fix this? PS: feel free to point out anything else you see that might be wrong.

Are you saying this is THE ACTUAL code you run?

  else
  {
    forward  <<-- THIS IS'T RIGHT
  }
  else
  {
    forward();
  }

I'd start with fixing this:

const int Lsencor = 4;
const int Rsencor = 7;
#include <Servo.h> <<< put this as the first line of the sketch

void loop()
{
Lsencorvar = digitalRead (Lsencor);
Rsencorvar = digitalRead (Rsencor);
if (Lsencor == HIGH) <<< This should be Lsencorvar
{
turnright();
}
if (Rsencor == HIGH) <<< and Rsencorvar
{
turnleft();
}
else
{
forward
}
}

lloyddean:
Are you saying this is THE ACTUAL code you run?

yes...why do you ask?

Because looking at it I don't believe it will even compiler error free!

Except for missing (); here

else
{
forward();
}
it compiles okay.

well guess what? I owe you guys! You helped fix my robot! XD i tested it and fixed some of the delay times and it runs PERFECTLY. Special thanks to crossroads. He helped with the major problem. :slight_smile:

Cool.