The “twoeyes” program from Enniscorthy

We had a great day on Saturday at a workshop in Enniscorthy with some local budding engineers..  The robots we built used the Arduino nano and had two colour sensors on the front.  The robot tries to keep BOTH sensors over a bright tape on a dark surface.  This allows it to follow a winding track along the ground. The code for this robot is listed below. Please post back any comments or changes.

//
// RoboSlam example: This code makes use of two colour sensors
// The program will attempt to keep BOTH sensors over a bright
// tape on a dark surface.  If the robot moves off the tape
// corrective action will be take.  Care must be taken to
// position the sensors the "right" distance from the tape.
// This may take a little experimentation.

// Declare an integer variable to store the colour readings
int leftcolour;
int rightcolour;
// The setup routine runs once when the power is switched on.
void setup()
{
  // Digital output for LED
  pinMode(2, OUTPUT);

  // Digital outputs for left motor
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);

  // Digital outputs for right motor
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);

  // Open serial port to display colour readings on screen
  Serial.begin(9600);
}

// The loop routine runs over and over until the power is switched off.
void loop()
{
  // Read the voltage on pins A0 and A1 (the colour sensors, high value implies white)
  leftcolour = analogRead(A0);
  rightcolour = analogRead(A1);

  // Choose motor action based on colour reading
  if ( (leftcolour > 512) && (rightcolour > 512))
  {
    // both white, go straight
    digitalWrite(2, LOW);  // LED off
    digitalWrite(3, HIGH); // Left motor forward
    digitalWrite(4, LOW);
    digitalWrite(7, HIGH); // Right motor forward
    digitalWrite(8, LOW);
  }
  else
  {
    if (leftcolour <= 512)
    {
      // black detected on left, so turn to the right
      digitalWrite(2, HIGH); // LED on
      digitalWrite(3, HIGH); // Left motor forward
      digitalWrite(4, LOW);
      digitalWrite(7, LOW);  // Right motor reverse
      digitalWrite(8, HIGH);
    }
    if (rightcolour <= 512)
    {
      // black detected on right, so turn to the left
      digitalWrite(2, HIGH); // LED on
      digitalWrite(3, LOW); // Left motor reverse
      digitalWrite(4, HIGH);
      digitalWrite(7, HIGH);  // Right motor forward
      digitalWrite(8, LOW);
    }
  }
}

Pictures of an assembled two-eyes robot
IMG_20160615_172425160

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

4 Responses to The “twoeyes” program from Enniscorthy

  1. Djibril says:

    Hi,

    I’ve uploaded the program onto the Arduino Nano but I’ve messed up the wiring on the breadboard. Can you send a picture of the breadboard with the 2 sensors connected on it.

    Thank You.

    Like

  2. fduignan says:

    I have added some pictures of an assembled robot and hopefully this will help with your wiring problems. Please reply if you are having difficulties.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s