Pages: [1]   Go Down
Author Topic: So i'm programming a robot...  (Read 1812 times)
0 Members and 1 Guest are viewing this topic.
Montreal,Qc
Offline Offline
Newbie
*
Karma: 0
Posts: 23
I am completely new to programming but im learning fast.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

« Last Edit: February 20, 2012, 09:04:34 pm by ArduinoN00b » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 25
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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);
  }
}
Logged

-TECman-

Greenville, IL
Offline Offline
Edison Member
*
Karma: 11
Posts: 1309
Warning Novice on board! 0 to 1 chance of errors!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
two NC micro switches

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

Logged


Montreal,Qc
Offline Offline
Newbie
*
Karma: 0
Posts: 23
I am completely new to programming but im learning fast.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 547
Posts: 45982
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  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:
  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:
    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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 25
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

-TECman-

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24291
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Offline Offline
Newbie
*
Karma: 0
Posts: 25
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

-TECman-

West Des Moines, Iowa USA
Offline Offline
Sr. Member
****
Karma: 2
Posts: 428
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Working from your functional spec (and without knowing anything about servos), I can suggest a general framework:
Code:
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();
}
Logged

There's always a better way!

West Des Moines, Iowa USA
Offline Offline
Sr. Member
****
Karma: 2
Posts: 428
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

There's always a better way!

the land of sun+snow
Offline Offline
Faraday Member
**
Karma: 149
Posts: 2790
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


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:
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:
if (Lsensor == HIGH)  back_turn_right();
 else if (Rsensor == HIGH)  back_turn_left();
 else { /* just continue */ }
Logged

Montreal,Qc
Offline Offline
Newbie
*
Karma: 0
Posts: 23
I am completely new to programming but im learning fast.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the help everyone  smiley-razz. 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-grin
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24291
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

the land of sun+snow
Offline Offline
Faraday Member
**
Karma: 149
Posts: 2790
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


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.




Logged

Pages: [1]   Go Up
Jump to: