Code examples for TCRT5000 proximity detection

Basic version:

//
// Proximity sensing with TCRT5000
// written by Ted Burke - 8-11-2017
//

void setup()
{
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);

  digitalWrite(2, HIGH);

  Serial.begin(9600);
}

void loop()
{
  int v, vth;

  v = analogRead(6);   // range is 0 to 1023
  vth = analogRead(7); // threshold voltage

  Serial.print(0);
  Serial.print(" ");
  Serial.print(1023);
  Serial.print(" ");
  Serial.print(vth);
  Serial.print(" ");
  Serial.println(v);

  if (v > vth)
  {
    digitalWrite(3, HIGH);
  }
  else
  {
    digitalWrite(3, LOW);
  }

  delay(100);
}

Background subtract:

//
// Proximity sensing with TCRT5000
// written by Ted Burke - 8-11-2017
//

void setup()
{
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);

  Serial.begin(9600);
}

void loop()
{
  int v, vth, von, voff;

  voff = analogRead(6);   // range is 0 to 1023
  digitalWrite(2, HIGH);  // switch on IR LED
  delayMicroseconds(100); // 100 us delay to allow IR LED to light up
  von = analogRead(6);    // range is 0 to 1023
  digitalWrite(2, LOW);   // switch off LED

  v = von - voff;

  // Clamp v to the range 0 to 200
  if (v < 0) v = 0;
  if (v > 200) v = 200;
  
  vth = analogRead(7); // threshold voltage

  Serial.print(0);
  Serial.print(" ");
  Serial.print(200);
  Serial.print(" ");
  Serial.print(vth);
  Serial.print(" ");
  Serial.print(voff);
  Serial.print(" ");
  Serial.print(von);
  Serial.print(" ");
  Serial.println(v);

  if (v > vth)
  {
    digitalWrite(3, HIGH);
  }
  else
  {
    digitalWrite(3, LOW);
  }

  delay(100);
}

Beepz example:

//
// Proximity detection with TCRT5000
// Written by Ted Burke - 8-11-2017
//

void setup()
{
  pinMode(2, OUTPUT); // IR LED
  pinMode(3, OUTPUT); // green LED

  // Print sensor readings via serial
  Serial.begin(115200);

  // Audio output on pin D11
  pinMode(11, OUTPUT);
  TCCR2A = _BV(COM2A0) | _BV(COM2B1) | _BV(WGM21) | _BV(WGM20);
  TCCR2B = _BV(WGM22) | _BV(CS22);
  OCR2A = 180;
  OCR2B = 50;
}

long n = 0;
int v_on, v_off, previous_reflection, reflection, threshold;
long next_sample_time = 0;
float avg = 0;

void loop()
{
  // Wait for next sample time
  while(millis() < next_sample_time);
  next_sample_time = next_sample_time + 40;

  // Read on and off voltages from IR sensor
  threshold = analogRead(7) / 10;
  v_off = analogRead(6);
  digitalWrite(2, HIGH);
  delay(20);
  v_on = analogRead(6);
  digitalWrite(2, LOW);

  // Calculate reflection value and clamp to range 0-200
  previous_reflection = reflection;
  reflection = v_on - v_off;
  if (reflection < 0) reflection = 0;
  if (reflection > 200) reflection = 200;

  avg = 0.9*avg + 0.1*reflection;

  // Light the green LED if reflection is stronger than threshold
  if (avg > threshold)
  {
    digitalWrite(3, HIGH);
    OCR2A = 255 - avg;
  }
  else
  {
    digitalWrite(3, LOW);
    OCR2A = 0;
  }

  // Print readings
  Serial.print(0);
  Serial.print(" ");
  Serial.print(200);
  Serial.print(" ");
  Serial.print(threshold);
  Serial.print(" ");
  Serial.print(reflection);
  Serial.print(" ");
  Serial.println(avg);
}
Advertisements
Posted in Uncategorized | Leave a comment

Day 6 Tutorial

Three videos, the first two are quite short. All are about group work, group learning, and group dynamics.

Posted in Uncategorized | Leave a comment

Race to the Wall, October 2017 – Rules and Information

This semester’s Race to the Wall event will commence at 3pm (or shortly thereafter) on Wednesday 18th October 2017. The event will end on the same day at 6pm.

Teams who do not complete the Race to the Wall task on Wednesday 18th October 2017 (week 6) will have a second opportunity to complete the task on Friday 20th October 2017 (week 6) between 3:30pm and 4:30pm, and a third opportunity on Wednesday 8th March 2017 (week 7) between 3:00pm and 6:00pm. Any teams that complete the challenge for the first time in week 7 will be ranked below all teams that complete the challenge in week 6.

The final ranking will be determined as follows:

  1. The highest ranked teams will be those that complete the Race to the Wall task in week 6 and are fully compliant with the weight and size restrictions (see below). These teams will be ranked in ascending order of recorded time, each team’s recorded time being their best time achieved in week 6.
  2. The next ranked set of teams will be those that complete the task in week 6 but are not fully compliant with the size and weight restrictions. These teams will be ranked in ascending order of recorded time, each team’s recorded time being their best time achieved in week 6. Please note, however, that permission to attempt the task with a non-compliant robot will be granted at the discretion of the RoboSumo tutors only. Robots which grossly exceed the limits will be disqualified.
  3. The next ranked set of teams will be those that complete the task in week 7 and are fully compliant with the weight and size restrictions. These teams will be ranked in order of speed.
  4. The next ranked set of teams will be those that complete the task in week 7 and are not fully compliant with the weight and size restrictions. These teams will be ranked in order of speed.
  5. The final ranked set of teams will be those that do not complete the task in either week, but are still be deemed by the tutors to merit ranking on the basis of technical attainment.

Before stating the rules formally, here’s a quick introduction:

(Editable SVG versions of gallery images: 0, 1, 2, 3, 4, 5, 6)

Rules

The Competition

The competition requires each robot to compete in a time-trial race. Robots perform the time trial one at a time and are ranked on a leaderboard. The objective is to complete the race as quickly as possible.

The results of the race will be used to determine your robot’s starting position on the RoboSumo Leaderboard for the final RoboSumo competition. The format of this semester’s RoboSumo tournament is still to be finalised, but in previous DIT RoboSumo tournaments starting higher on the leaderboard was a significant advantage.

In the event of a tie between two or more robots in the Race to the Wall, the position on the leaderboard will be determined by an assessment of the quality of the robot construction.

The Race

The robot begins in a starting position before the start/finish line (see Figure 1). A team member pressed a start button or otherwise switches on the robot’s power, then withdraws. Team members may not physically propel the robot. Once the robot is activated, team members may not intervene or interfere with it in any way for the full duration of the task.

The robot must do the following:

  • Move forward autonomously from the starting position,
  • Break the start/finish laser beam once and only once (the first of two times during the race),
  • Continue moving forward until it touches a block,
  • Move back towards the start/finish line,
  • Stop on the start/finish line within the 20 second time limit, breaking the start/finish laser beam for the second and final time. The beam must remain continuously broken for a minimum of 2 seconds.

When the robot returns to and stops on the start/finish line, if the beam becomes unbroken within 2 seconds the robot is disqualified and the time is not recorded on the leaderboard. Disqualification for this reason does not prevent a robot from attempting the task again.

If the robot successfully completes the task, the time recorded will be the time between the first and second breaking of the beam. Each team can attempt the task an unlimited number of times during the event. However, if others are waiting to attempt the task, a team must return to the back of the queue following each attempt. At the discretion of the tutors, teams who have not yet recorded a time may be given priority over teams who have already recorded a time but wish to improve upon it.

The Robot

The robot must satisfy the criteria for the mini-sumo class in the Robot Challenge robot sumo rules, with the following additional requirements:

  • At every moment during the race, there must exist a cuboid 10cm in horizontal length, 10cm in horizontal width and of unlimited height which encloses every part of the robot. In other words, the dimensions of a robot cannot expand outside its 10cm x 10cm footprint at any time during a race.
  • The sides of the robot must be covered with an opaque material so that the robot reliably breaks the start/finish laser beam once and only once as it passes through it.

The key features are:

  • The robot must be fully autonomous.
  • The footprint of the robot (its shape / area when viewed from above) must fit within a 10cm by 10cm square.
  • The robot’s mass must not exceed 0.5kg.

race_to_wall

Figure 1: The Race to the Wall track. (Click here to download editable SVG version of image)

The Track

The race will take place on a flat horizontal light-coloured surface (probably one or more tables) in room KEG-036. One end of the track is marked with a dark line of tape (the start/finish line). A block stands at the other end of the track. The track is between 1m and 2m in length (from the start/finish line to the block). The start/finish line is parallel to the vertical face of the block.

The Start/Finish Laser Beam

The laser beam is used to start and stop the timer for the race, and is used to accurately measure each robot’s race time. The laser will be in a fixed position at a height of 5cm and will be aimed horizontally across the track, above (and parallel to) the start/finish line.

The Block

The block is a solid object at least 20cm high and 50cm wide. It will be positioned such that its face is perpendicular to the table surface and parallel to the shorter edge of the table, as shown in Figure 1. The position of the block will be otherwise unspecified but will be the same for each competing robot.

Posted in Uncategorized | 1 Comment

Day 5 Tutorial

Just one video today—Ted’s lecture on very basic coding techniques.

Posted in Uncategorized | 1 Comment

Day 4 Tutorial

Two videos from today’s lecture:

1) on the switch circuit and code

2) on the colour sensor circuit and code

And some photos to help:

Posted in Uncategorized | 1 Comment

Day 3 Tutorial

I’ve made a little video to help you plan for the Race to the Wall

Some notes from Ted’s lab:

From the lecture, here’s info on powering your colour sensor

And, finally, some hints on using the breadboard

[end]

Posted in Uncategorized | Leave a comment

Day 2 Tutorial Video

I’ve posted three videos from the second day of class, click the words to view them. There is one on soldering

another on assembling a circuit to power one motor,

and finally a talk Ted gave in our lab on the circuitry and driver chip needed to power two motors for your robot.

I have also posted a video Ted and I made before the semester started about getting the wheels to turn various directions.

They are raw, but we hope they will help in case you need a refresher or missed something in the first go. The following pictures may help, too.

Posted in Uncategorized | Tagged , , , | Leave a comment