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 :
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?
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;
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.
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!
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.
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!
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.