Go Down

Topic: So i'm programming a robot... (Read 2 times) previous topic - next topic

ArduinoN00b

Feb 21, 2012, 02:57 am Last Edit: Feb 21, 2012, 03:04 am by ArduinoN00b Reason: 1
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 :smiley-roll-sweat: :smiley-roll-sweat: :smiley-roll-sweat: :smiley-roll-sweat: :smiley-roll-sweat: :smiley-roll-sweat: :smiley-roll-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? :smiley-sweat:

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

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);
  }
}


TECman

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

Code: [Select]
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);
 }
}
-TECman-

cyclegadget

Quote
two NC micro switches


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

Good links: Eagle tutorial= http://www.youtube.com/playlist?list=PLDE1858BD83D19C70
General Arduion tutorials = http://tronixstuff.wordpress.com
http://www.gammon.com.au/forum/bbshowpost.php?bbtopic_id=123

ArduinoN00b

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

PaulS

Code: [Select]
  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.

Code: [Select]
  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?

Code: [Select]
    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.

TECman

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-

AWOL

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.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

TECman

@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-
-TECman-

Morris Dovey

Working from your functional spec (and without knowing anything about servos), I can suggest a general framework:
Code: [Select]
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();
}
There's always a better way!

Morris Dovey

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...
There's always a better way!

oric_dan


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

Quote

(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.

Code: [Select]

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,

Code: [Select]

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


ArduinoN00b

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!   :D

AWOL

Code: [Select]
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.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

oric_dan


Quote

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:

Quote

left = digitalRead(LSW);


From OP:
Quote

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.





Go Up