Link to home
Start Free TrialLog in
Avatar of jasoncuevas
jasoncuevas

asked on

Running a motor using Interactive C (full question)

The project is to design a robot out of legos which, by using infrared and breakbeam sensors, will follow a path of black tape on a white floor, pick up a block at the end of the tape, turn around, follow the tape back, and stop at the end. The break beam sensor is broken when the block enters two arms, at which point it activates muscle wire which drops a bar, trapping the block. The break beam sensor deals only with the block, not with the tape. The infrared sensors are place near the middle of the bot, facing straight down. They are placed a little closer together than the width of the tape, so that if it hits a straightaway, both sensors are over the tape. My issue is trying to figure out how to get the sensors to recognize that it's at the end of the tape. At the moment, it backs up if it gets off the tape (which it does 15-20 times while actually travelling the track), so when it gets to the end, it just spazzes by going alternatingly forward/backward on and off the tape.
Any help would be appreciated, here's the code.

void main()
{
    int left_motor = 1, right_motor = 3, left_sensor = 5,
      right_sensor = 6, break_sensor = 2, back_count = 0,
      break_count = 0, motor_sensor = 0;
   
    while(1)
      {
        if(analog(break_sensor) > 200 && break_count == 0)  //if break beam sensor is broken, it will do this
          {
            motor(left_motor,100);
            motor(right_motor,100);   //moves bot forward
            msleep(300L);
            off(left_motor);
            off(right_motor);
            motor(motor_sensor,100);  //activates muscle wire, traps box
            sleep(5.0);  
            off(motor_sensor); //turns motor_0 motors off
            motor(left_motor, 100);
            msleep(500L);
            motor(right_motor,-100);    //this makes robot pull a 180
            msleep(500L);
            break_count++;   //exits the loop permanently
        }
        else
          {  
            if(analog(left_sensor)<120 && analog(right_sensor)>120) //left sensor off tape
              {                                                     //right sensor on tap
                motor(left_motor,100);
                motor(right_motor,25);  //turns left motor on high, right motor 25%, turns left
              }
            if(analog(left_sensor)>120 && analog(right_sensor)>120) //both on tape
              {
                motor(left_motor,100);
                motor(right_motor,100);  //both motors full speed
              }
            if(analog(left_sensor)>120 && analog(right_sensor)<120) //left on, right off
              {    
                motor(left_motor,25);
                motor(right_motor,100);   //makes a right turn to get back on tape
              }
            if(analog(left_sensor)<120 && analog(right_sensor)<120) //both sensors off tape
              {
                motor(left_motor,-50);
                motor(right_motor,-50);   //back up both motors              
              }  
          }
    }    
}
Avatar of Axter
Axter
Flag of United States of America image

Hi jasoncuevas,
Exactly what is your question?

Cheers!
Avatar of jasoncuevas
jasoncuevas

ASKER

How do I make it know when it is at the end of the tape without it interfering with the while loop.
ASKER CERTIFIED SOLUTION
Avatar of PaulCaswell
PaulCaswell
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Jason,

How many sensors do you have looking for the black tape?  I suggest three, positioned close enough that at least two of the sensors can seen the tape simultaneously, and possibly close enough that all three can see the tape.

Here's layout 1:

S  S  S   (The three sensors)
  TTT     (The tape)
  TTT
  TTT
  TTT

Nominally, only the middle sensor is reading the tape.  However, as the craft veers off-center, both the center sensor and one of the side sensors both detect the tape.  The robot has strayed off center toward the side sensor that is detecting tape and must be moved toward that sensor.

If the center sensor loses tape but a side sensor still detects it, you stop the robot and reverse until the center sensor again detects tape.  You make a steering correction and resume forward.

When all three sensors lose the tape, you've reached the end of the tape.


The variation where all three sensors can detect the tape simultaneously works similarly.

Kent