Line following robot, photo-reflectors

I'm building a line following robot with Arduino UNO R3 for a uni project and can't seem to get it working at all... it should work on the basis of using 2 photoreflectors to detect a black line.

I've used a chassis with 2x DC motors hooked up through 2 MOSFETs to an external power source and a digital pin per motor which works perfectly with Firefly (program for externally controlling arduino).

I then have 2x photo-reflectors each with 4 pins (Cathode, anode, emitter and collector) which I've got hooked up to live & ground but for the hell of me can't work out in which order they should connect back to arduino :frowning: I'm a tad confused how the emitter and collector pins should connect to allow me to connect to the analogue pins, and also how the 2 photo-reflectors should connect in relation to each other, any suggestions?

Finally I am trying to get away from using Firefly to control it seeing as that requires a computer connection so I need to write the sketch to allow the robot to run. I've been using a few examples I'll post links to but I am entirely lost...I don't have a great deal of time to be spending on building the robot seeing as it's only to allow me to carry out other processes of experiments and I reeeeeally need some help!

If I can't work the arduino programming/sketches then I guess I can always get some really long wires to connect back to the arduino board but it would look better if I could get it wirelessly running :slight_smile:

My main concern is probably the hooking it up to the board.

Any help at all would be HUGELY appreciated!

Links I've been looking at:
http://www.botbuilder.co.uk/codeandprojects/bbline/
http://garagelab.com/profiles/blogs/tutorial-make-your-line-following-robot

Code I currently have:

#define STB 10 //Standby

//Motor left
#define PWMA  3 //Speed control
#define AIN1  7 //Direction
#define AIN2  8 //Direction
#define MOTOR_L 1 //Left motor as 1

//Motor right
#define PWMB  9 //Speed control
#define BIN1  6 //Direction
#define BIN2  5 //Direction
#define MOTOR_R 2 //Right motor right as 2

#define SENSOR_D A0 // Pins of reflectance sensor
#define SENSOR_L A1 // Pins of reflectance sensor

void setup()
{
  pinMode(STB, OUTPUT);

pinMode(PWMA, OUTPUT);
  pinMode(AIN1, OUTPUT);
  pinMode(AIN2, OUTPUT);

pinMode(PWMB, OUTPUT);
  pinMode(BIN1, OUTPUT);
  pinMode(BIN2, OUTPUT);
  Serial.begin(9600);
}

void loop()
{

int val_sense = read_sensors();// Return status of sensors

switch(val_sense)
  {
    case 0:
      move(MOTOR_L, 255, 1); //Motor left, full speed, forward
      move(MOTOR_R, 255, 1); //Motor right, full speed, forward
      break;

case 1:
      move(MOTOR_R, 0, 1); //Motor right, stop.
      move(MOTOR_L, 255, 1); //Motor left, full speed, forward
      break;

case 2:
      move(MOTOR_L, 0, 1); //Motor left, stop.
      move(MOTOR_R, 255, 1); //Motor right, full speed, forward
      break;

case 3:
      move(MOTOR_L, 100, 2); //Motor left, less speed, backward
      move(MOTOR_R, 100, 2); //Motor right, less speed, backward
      break;
   }
}

void move(int motor, int speed, int direction)
{
  //Move specific motor at speed and direction
  //motor: 2 for B and 1 for A
  //speed: 0 is off, and 255 is full speed
  //direction: 0 backward, 1 forward

digitalWrite(STB, HIGH); //disable standby

boolean inPin1 = LOW;
  boolean inPin2 = HIGH;

if(direction == 1)
  {
    inPin1 = HIGH;
    inPin2 = LOW;
  }

if(motor == 1)
  {
    digitalWrite(AIN1, inPin1);
    digitalWrite(AIN2, inPin2);
    analogWrite(PWMA, speed);
  }
  else
  {
    digitalWrite(BIN1, inPin1);
    digitalWrite(BIN2, inPin2);
    analogWrite(PWMB, speed);
  }
}

 

int read_sensors()
{
  //Read Sensor of Right
  int sensors = 0;
  pinMode( SENSOR_D, OUTPUT );
  digitalWrite( SENSOR_D, HIGH );
  delayMicroseconds(10);
  pinMode( SENSOR_D, INPUT );
  long time = micros();

  while (digitalRead(SENSOR_D) == HIGH && micros() - time < 3000);
  int diff = micros() - time;

if (diff > 1600)
    sensors += 1;

//Read Sensor of Left
  pinMode( SENSOR_L, OUTPUT );
  digitalWrite( SENSOR_L, HIGH );
  delayMicroseconds(10);
  pinMode( SENSOR_L, INPUT );
  long time1 = micros();

  while (digitalRead(SENSOR_L) == HIGH && micros() - time1 < 3000);
  int diff1 = micros() - time1;

  if (diff1 > 1600)
    sensors += 2;

  return sensors;

}

See how to use this forum, in particular the part about "Make a meaningful subject line".

You have managed to combine two of the things I suggested you not do:

  • Noob here, help needed
  • Help me, quickly!

I don't want to seem negative, but how about posting your code? If there isn't any, I can move this post to a more appropriate part of the forum.

You need at least three sensors, so you know which way you are moving relative to where the line is. Two doesn't cut it.

for a uni project

I feel sorry for your instructors.