So i'm programming a robot...

Hello, im working on a robot for my local science fair and am having great difficulty programming it so here I am asking for help. First I have a question:
(This involves IF and ELSE statements) If i have a program with 2 different IF statements in it and can have 1 ELSE for them?

To the point :cold_sweat: :cold_sweat: :cold_sweat: :cold_sweat: :cold_sweat: :cold_sweat: :cold_sweat::
In my program I have 2 continuous rotation servos and two NC micro switches that act as touch senors for the robot. What i am trying to do is to get the robot go forward untill one of the senors are pressed. When that happens I want the robot to stop, backup, turn, then continue forward. I'm having trouble writing a program for two senors (left and right), can someone please help me with the programming by leaving some tips or examples in the reply's. Thanks a lot in advance and i really need the help? :sweat_smile:

Here's what I have so far if it help's anyone help me:

const int Lsencor = 2;
const int Rsencor = 3;
#include <Servo.h>
Servo myservo;
Servo myservo2;

void setup()
{
pinMode (Lsencor, INPUT);
pinMode (Rsencor, INPUT);
myservo.attach(10);
myservo2.attach(6);
}

void loop() {
//Setup for Lmotor
digitalRead (Lsencor);

if (Lsencor == HIGH);
{
myservo.write(92);
myservo2.write(92);
delay(3000);
myservo.write(180);
myservo2.write(10);
delay(4000);
myservo2.write(92);
myservo.write(30);
delay(2500);
myservo.write(92);
}

//setup for Rmotor
digitalRead (Rsencor);

if (Rsencor == HIGH) {
myservo.write(92);
myservo2.write(92);
delay(3000);
myservo2.write(10);
myservo.write(180);
delay(5000);
myservo.write(92);
myservo2.write(180);
delay(2500);
myservo2.write(92);
}

else{
myservo.write(180);
myservo2.write(10);
}
}

Try this. Plus you have never really told your problem so I'm not sure what I should fix exactly lol!

const int Lsencor = 2;
const int Rsencor = 3;
#include <Servo.h>
Servo myservo;
Servo myservo2;

void setup()
{
  pinMode (Lsencor, INPUT);
  pinMode (Rsencor, INPUT);
  myservo.attach(10);
  myservo2.attach(6);
}

void loop() {
  //Setup for Lmotor
  digitalRead (Lsencor);
  //setup for Rmotor
  digitalRead (Rsencor);

  if (Lsencor == HIGH);
  {
    myservo.write(92);
    myservo2.write(92);
    delay(3000);
    myservo.write(180);
    myservo2.write(10);
    delay(4000);
    myservo2.write(92);
    myservo.write(30);
    delay(2500);
    myservo.write(92);
  }
  else if (Rsencor == HIGH) {
    myservo.write(92);
    myservo2.write(92);
    delay(3000);
    myservo2.write(10);
    myservo.write(180);
    delay(5000);
    myservo.write(92);
    myservo2.write(180);
    delay(2500);
    myservo2.write(92);
  }
  else{
    myservo.write(180);
    myservo2.write(10);
  }
}

two NC micro switches

How are these switches connected? It will make a big difference on your code.

they are connected from the 5v pin to pins 3 and 2

  digitalRead (Lsencor);
  //setup for Rmotor
  digitalRead (Rsencor);

Let's read the state of the pins, but not save the results. Waste of time, if you ask me.

  if (Lsencor == HIGH);

If the value is HIGH do ;. If not, do nothing. I don't see that that accomplishes anything.

Do you really expect 2 to ever be HIGH?

    delay(3000);
    myservo.write(180);
    myservo2.write(10);
    delay(4000);
    myservo2.write(92);
    myservo.write(30);
    delay(2500);

Assuming the switch was not pressed, lets start some motors spinning in various directions, and not bother checking the switches again for 9 and half seconds. Well, OK, don't mind if the robot does hit the wall. We'll fix that in 10 seconds.

The way to deal with switches and motors is to get rid of all delays. You do not want the robot to move for some period of time, then check to see if it hit something. That is not how a person moves or drives, so there is no reason to program the robot that way.

You want to start the robot moving. Then, continuously monitor the switches. When one changes state, stop, and decide on a new direction. Start going. Repeat.

NO DELAYS! Period.

First what is your science fair project lol because I have never heard of a robot who hits walls the backs away being one but besides that. What you really need is 4 buttons 2 for the front and 2 for the back. Then just make it go until one of them gets hit. But there is still a problem your robot doesn't even avoid walls. It appears to me that it still has to actually HIT the wall before it backs up. Why don't you use something like a distance sensor so it never even touches the wall and backs up when it is close!

TECman, your stream-of-consciousness style of posting is very hard to read.
Bumper robots are actually very common.

Could you just maybe slow down a little and use the spelling checker, maybe some punctuation and sprinkling of line-breaks??

This is a forum, not a chatroom.

@AWOL

I wasn't trying to be rude when asking about what kind of science fair project he was going. I just wanted to know because I have been wanting to do my own science fair project on robotics but could never figure out how to display it.

-TECman-

Working from your functional spec (and without knowing anything about servos), I can suggest a general framework:

int left,right;

void forward()
{
}
void stop()
{
}
void backup()
{
}
void turn()
{
}
void setup(void)
{  pinMode(LSW,INPUT);
   pinMode(RSW,INPUT);

}
void loop(void)
{  forward();
   do
   {  left = digitalRead(LSW);
      right = digitalRead(RSW);
   }  while (!(left || right)) ;
   stop();
   backup();
   turn();
}

Something else to think about: You may want to balance the number of left and right turns to avoid twisting the power cord into knots in the course of a long demonstration...

All of the current suggestions are pretty good. In regards,

(This involves IF and ELSE statements) If i have a program with 2 different IF statements in it and can have 1 ELSE for them?

the answer was buried in one of the replies, ie, use 1 or more ... else if ... and last else as default.

if { }
else if { }
else if { }
else { }

If you have a lot of different options, look at using the switch() ... case construct.

Also, I'm working on a small robot with continuous rotation servos too. As others mentioned,
your delays sound very long, eg, 4.5 sec to turn will probably cause the robot to spin over a
very large angle. What is common is to get it to turn a few degrees, say 20-30 degrees [ie, for
probably 200-500 msec or so], and then go forwards again. Then, if the robot immediately hits
the same obstacle, it will then back and turn a little more, and so make a gradual avoidance
turn. If it turns too far in one go, then the robot will be making emormous changes and act
too crazy.

Also, as Morris indicated, your code has gotten complex enough that you should start modularizing
it, to make it more readable. Eg,

 if (Lsensor == HIGH)  back_turn_right();
 else if (Rsensor == HIGH)  back_turn_left();
 else { /* just continue */ }

Thanks for the help everyone :P. I noticed a few comments criticizing my code's, well I apologize for my noobish-ness but I am self taught and have had no professional help. Im' really thankful for the advice, but if someone could quote some of the things others posted (that have coding in it) and maybe help me understand by talking about which parts do what. I realize your all important people and have things to do but It would mean a lot! thanks! :smiley:

if (Lsensor == HIGH)  back_turn_right();
 else if (Rsensor == HIGH)  back_turn_left();

Please, don't confuse the pin number with the value read from that pin.

Code:
if (Lsensor == HIGH) back_turn_right();
else if (Rsensor == HIGH) back_turn_left();
Please, don't confuse the pin number with the value read from that pin.

Oops, sorry I missed that, Morris had it right:

left = digitalRead(LSW);

From OP:

I noticed a few comments criticizing my code's,

Think of it not so much as people criticizing, but trying to be helpful, according to their
own way of doing things :-). Everyone does things a little differently. You now have 12
different ideas to choose from, LOL.