Example code from todays lecture: switch and rangefinder

// Tip the Can - written by Ted Burke - 16-10-2019

// pin numbers
int red = 2;
int green = 3;
int blue = 4;
int trig = 6;
int echo = 7;

void setup()
{
  pinMode(red, OUTPUT);   // red
  pinMode(green, OUTPUT); // green
  pinMode(blue, OUTPUT);  // blue

  pinMode(trig, OUTPUT);  // HC-SR04 trigger pin

  Serial.begin(9600);     // open serial connection to laptop
}

int button_was_pressed = 0; // global flag variable

void loop()
{
  // Read the distance from the rangefinder
  int duration;                          // stores echo pulse duration in microseconds
  float distance;                        // stores the distance in metres
  digitalWrite(trig, HIGH);              // begin trigger pulse
  delayMicroseconds(20);                 //
  digitalWrite(trig, LOW);               // end trigger pulse
  duration = pulseIn(echo, HIGH, 10000); // measure echo pulse duration
  distance = 340e-6 * (duration/2);      // convert to distance in metres
  Serial.print(distance);                // print out the distance
  Serial.println(" metres");             //
  delay(100);                            // 100 ms delay

  // Check the switch
  if (digitalRead(9) == 1)
  {
    button_was_pressed = 1;
  }

  // Decide what to do with the LEDs
  if (button_was_pressed == 0 && (distance > 0.8 || distance == 0))
  {
    // This code runs if the button has not been pressed and
    // the distance is either greater than 0.8m or zero, which
    // indicates a timeout
    // "&&" means logical AND
    // "||" means logical OR
    digitalWrite(red, HIGH);
    digitalWrite(green, LOW);
    digitalWrite(blue, LOW);
  }
  else if (button_was_pressed == 0 && distance <= 0.8)
  {
    // This code runs if the button has not been pressed and
    // the distance is less than 0.8m (but not zero)
    digitalWrite(red, LOW);
    digitalWrite(green, HIGH);
    digitalWrite(blue, LOW);
  }
  else
  {
    // This code runs if neither of the previous two blocks did
    digitalWrite(red, LOW);
    digitalWrite(green, LOW);
    digitalWrite(blue, HIGH);
  }
}
Advertisements
Posted in Uncategorized | Leave a comment

Assembly instructions for TCRT5000 printed circuit board

Martin Sorohan, who designed the printed circuit board adapter for the TCRT5000 infrared reflective sensor, has kindly supplied the following assembly instructions for soldering it:

Colour Sensor Assembly on PCB Board — Robo Sumo

Posted in Uncategorized | Leave a comment

HC-SR04 rangefinder example from today’s lecture

//
// HC-SR04 rangefinder example
// Written by Ted Burke 9-10-2019
//

void setup()
{
  pinMode(7, OUTPUT); // trigger
  pinMode(8, INPUT);  // echo

  pinMode(2, OUTPUT); // LED 1 (red)
  pinMode(3, OUTPUT); // LED 2 (green)
  pinMode(4, OUTPUT); // LED 3 (blue)
}

void loop()
{
  // Send trigger pulse
  digitalWrite(7, HIGH);
  delayMicroseconds(20);
  digitalWrite(7, LOW);

  delayMicroseconds(3300); // 3.3 ms delay

  int echoState;

  echoState = digitalRead(8);
  
  if (echoState == 0)
  {
    // object within 50cm
    digitalWrite(2, HIGH);
    digitalWrite(3, LOW);
  }
  else
  {
    // object is farther than 50cm
    digitalWrite(2, LOW);
    digitalWrite(3, HIGH);
  }

  delay(50); // 50 ms delay to let any ultrasoubnd reflections die away
}
Posted in Uncategorized | Leave a comment

Arduino example from lecture, 2nd Oct 2019

This is the example circuit from today’s lecture. It includes:

  1. Control of a geared d.c. motor via SN754410NE driver IC (Arduino pins D5 and D6).
  2. Red, green and blue indicator LEDs (Arduino pins D2, D3 and D4).
  3. A microswitch input (with pull-down resistor) on Arduino pin D8.
  4. A TCRT5000 reflective IR sensor connected to Arduino analog input A3.

These are some photographs of my breadboard circuit:

The following example programs illustrate how to use each feature of the circuit.

Blink an LED

//
// Arduino example: blink an LED
// Written by Ted Burke, 2-10-2019
//

void setup()
{
  pinMode(2, OUTPUT); // red LED
}

void loop()
{
  digitalWrite(2, HIGH); // LED on
  delay(500);            // 500ms delay
  digitalWrite(2, LOW);  // LED off
  delay(500);            // 500ms delay
}

Bi-directional control of a d.c. motor

//
// Arduino example: bi-directional control of a d.c. motor
// Written by Ted Burke, 2-10-2019
//

void setup()
{
  // digital outputs
  pinMode(2, OUTPUT); // red LED
  pinMode(3, OUTPUT); // green LED
  pinMode(4, OUTPUT); // blue LED
  pinMode(5, OUTPUT); // motor forward
  pinMode(6, OUTPUT); // motor reverse
}

void loop()
{
  // motor forward for 1 second with green LED on
  digitalWrite(2, LOW);  // red LED off
  digitalWrite(3, HIGH); // green LED on
  digitalWrite(4, LOW);  // blue LED off
  digitalWrite(5, HIGH); // motor forward
  digitalWrite(6, LOW);
  delay(1000);

  // motor reverse for 1 second with blue LED on
  digitalWrite(2, LOW);  // red LED off
  digitalWrite(3, LOW);  // green LED off
  digitalWrite(4, HIGH); // blue LED on
  digitalWrite(5, LOW);
  digitalWrite(6, HIGH); // motor reverse
  delay(1000);

  // motor stop for 2 seconds with red LED on
  digitalWrite(2, HIGH); // red LED on
  digitalWrite(3, LOW);  // green LED off
  digitalWrite(4, LOW);  // blue LED off
  digitalWrite(5, LOW);  // motor stop
  digitalWrite(6, LOW);
  delay(2000);
}

Using a microswitch as an input device

This example controls the motor direction using the switch input.

//
// Arduino example: microswitch input controls motor
// Written by Ted Burke, 2-10-2019
//

void setup()
{
  // digital outputs
  pinMode(2, OUTPUT); // red LED
  pinMode(3, OUTPUT); // green LED
  pinMode(4, OUTPUT); // blue LED
  pinMode(5, OUTPUT); // motor forward
  pinMode(6, OUTPUT); // motor reverse
}

void loop()
{
  // Declare an integer variable
  int x;

  // Read the voltage on pin D8 (1 for 5V, 0 for 0V)
  // and store the value in the variable x
  x = digitalRead(8);

  // Choose the motor direction depending on the microswitch
  if (x == 1)
  {
    // motor forward when switch is pressed
    digitalWrite(5, HIGH);
    digitalWrite(6, LOW);
  }
  else
  {
    // motor reverse when switch is not pressed
    digitalWrite(5, LOW);
    digitalWrite(6, HIGH);
  }
}

This example toggles an LED on or off each time the switch is pressed:

//
// Arduino example: Toggle an LED on/off each time switch is pressed
// Written by Ted Burke, 2-10-2019
//

// Create a global variable to remember LED state
// Possible values are: 1 for on, 0 for off
int onoff = 0;

void setup()
{
  // digital outputs
  pinMode(2, OUTPUT); // red LED
  pinMode(3, OUTPUT); // green LED
  pinMode(4, OUTPUT); // blue LED
  pinMode(5, OUTPUT); // motor forward
  pinMode(6, OUTPUT); // motor reverse
}

void loop()
{
  if (digitalRead(8) == 1)
  {
    // Toggle the value of the onoff variable
    if (onoff == 1)
    {
      onoff = 0;
    }
    else
    {
      onoff = 1;
    }

    // allow time for switch to be released
    delay(200);
  }

  // Set the LED state
  if (onoff ==1)
  {
    digitalWrite(4, HIGH); // blue lED on
  }
  else
  {
    digitalWrite(4, LOW);  // blue LED off
  }
}

A shorter equivalent version of the previous example:

//
// Arduino example: Toggle an LED on/off each time switch is pressed
// Written by Ted Burke, 2-10-2019
//

int onoff = 0;

void setup()
{
  pinMode(4, OUTPUT); // blue LED
}

void loop()
{
  if (digitalRead(8) == 1)
  {
    onoff = 1 - onoff; // toggle variable
    delay(200);        // allow time for switch to be released
  }

  digitalWrite(4, onoff); // Set led state
}

Measuring voltage from a sensor using an analog input

//
// Arduino example: Obtain sensor readings via analog input
// Written by Ted Burke, 2-10-2019
//

void setup()
{
  // digital outputs
  pinMode(2, OUTPUT); // red LED
  pinMode(3, OUTPUT); // green LED
  pinMode(4, OUTPUT); // blue LED
  pinMode(5, OUTPUT); // motor forward
  pinMode(6, OUTPUT); // motor reverse
}

void loop()
{
  // Declare an integer variable to store sensor readings
  int voltage;

  // read voltage from pin A3
  // Values from 0-1023 represent the voltage range 0V-5V
  voltage = analogRead(3);

  // Choose motor direction based on input voltage
  // Go forward when voltage > 2.5V
  // Reverse when voltage <= 2.5V
  if (voltage > 512)
  {
    // motor forward
    digitalWrite(5, HIGH);
    digitalWrite(6, LOW);
  }
  else
  {
    // motor reverse
    digitalWrite(5, LOW);
    digitalWrite(6, HIGH);
  }
}

Another analog input example. This prints the readings via the serial
link so that they can be displayed in the serial monitor or graphed
using the serial plotter:

//
// Arduino example: Obtain sensor readings via analog input
// Written by Ted Burke, 2-10-2019
//

void setup()
{
  // digital outputs
  pinMode(2, OUTPUT); // red LED
  pinMode(3, OUTPUT); // green LED
  pinMode(4, OUTPUT); // blue LED
  pinMode(5, OUTPUT); // motor forward
  pinMode(6, OUTPUT); // motor reverse

  Serial.begin(9600); // open serial connection at 9600 bits/second
}

void loop()
{
  // Declare an integer variable to store sensor readings
  int voltage;

  // read voltage from pin A3
  // Values from 0-1023 represent the voltage range 0V-5V
  voltage = analogRead(3);

  // Print the voltage reading over the serial connection
  Serial.println(voltage);

  // Cycle through the three LEDs
  // Note that the delays depend on the measured voltage
  digitalWrite(2, HIGH);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  delay(voltage);

  digitalWrite(2, LOW);
  digitalWrite(3, HIGH);
  digitalWrite(4, LOW);
  delay(voltage);

  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, HIGH);
  delay(voltage);
}
Posted in Uncategorized | Leave a comment

Snapshots from lecture 25th Sept 2019

Posted in Uncategorized | Leave a comment

LED Flash Challenge – Semester 1, 2019-2020

We’re beginning RoboSumo with a short competitive puzzle called the LED Flash Challenge. No formal assessment weight is attached to this challenge, but we’ll keep a close eye on who does well. In this challenge, doing well means two things: getting it working quickly and, more importantly, trying to understand what you’re doing.

In today’s lab (and for some of you part of the next lab) you’ll be working with your team to complete two tasks:

  1. Build a simple breadboard circuit for the Arduino Nano and program it to blink an LED on and off.
  2. Add a second LED to the circuit and reprogram the Arduino to transmit a specific binary sequence as a series of flashes from the two LEDs.

The first task is very prescriptive, which means that we’ll basically tell you exactly what to do, but to complete the second task you’ll need to think for yourselves.

You need a team number to complete this challenge. Your tutor will assign your team a unique number within the range shown below:

  • Teams 10-19: DT066A Group C1 with Brian Cogan in room KEG-012
  • Teams 20-29: DT066A Group C2 with Jane Courtney in room KEG-014
  • Teams 30-49: DT066A Groups D1 & D2 with Ted Burke and Catherine Fitzgerald in room KEG-036
  • Teams 50-59: DT066A Group E1 with Mick McKeever in room KE4-023 (room TBC!)

Part 1: Blinking LED

This task is relatively straightforward and shouldn’t take you too long to get working. Open the link below in a new tab and follow the instructions as far as the end of Part 1. Once your LED is blinking, come back here. (Note: The LED you receive may be a different colour and/or shape to that shown in the instructions.)

Instructions for Blinking LED example

Once your LED is blinking, there are four things you need to understand before moving on:

  1. How one of the Arduino pins (D2) was turned into a digital output.
  2. How the LED is turned on.
  3. How the LED is turned off.
  4. How to delay the program for a specified number of milliseconds, so that the rate of the LED blinking can be controlled.

Once you understand these four things, you have finished this part of the task (the easy part) and it’s time to move on to the LED Flash Challenge.

Part 2: LED Flash Challenge

In this part, you’re going to modify your circuit to create a simple optical transmitter, which transmits a digital message (a sequence of 1s and 0s) as a series of LED flashes.

The message that you’ll transmit will be 2 bytes long (a byte is 8 bits, or 8 ones and zeros) and it will contain your team number (byte 1) followed by a second number calculated by subtracting your team number from 255 (byte 2).

For example, if your team number is 79…

  • byte1 = 79
  • byte2 = 255 – 79 = 176
  • byte1 + byte2 = 255

Here, let me explain how binary numbers work…

Try doing some independent research on binary numbers. There’s lots more great stuff on YouTube, Wikipedia, etc.

Specifically, you need to do the following:

  1. Modify the code to create a second digital output pin.
  2. Extend the circuit by adding a second LED (with current limiting resistor) to that digital output pin.
  3. Convert your team number into an 8-bit binary number. This is byte 1 of your message.
  4. Calculate the required value of byte 2 (so that byte1+byte2 = 255) and write it as an 8-bit binary number.
  5. Each byte will be transmitted as a sequence of ones and zeros, preceded by a start bit (1) and followed by a stop bit (0). That means your complete transmission will be 20 bits long. You should calculate this sequence ad write it down on paper first.
  6. To transmit a 1, turn LED1 off and LED2 on for 500ms.
  7. To transmit a 0, turn LED2 off and LED1 on for 500ms.
  8. To ensure the sequence is read correctly, transmit a long sequence of zeros (for about 5 seconds) before you transmit your message.
  9. As is typically the case in digital transmissions, each byte must be transmitted least significant bit first.

Let’s consider that example team number 79 again. As explained above, byte 1 is 79 and byte 2 is 146.

  • Before transmitting the sequence, send a “0” for about 5 seconds.
  • The first bit of the sequence is the start bit for byte 1 which is “1”.
  • Written as a binary number, 79 (seventy-nine) is 0b01001111. The “0b” prefix indicates that a number is being written in binary form – it’s not part of the number value. The byte is transmitted least significant bit first, i.e. in the following order: “1,1,1,1,0,0,1,0”.
  • The next bit is the stop bit for byte 1, which is “0”.
  • The next bit is the start bit for byte 2, which is “1”.
  • Written as a binary number, 216 is 0b10110000, so the next 8 bits are “0,0,0,0,1,1,0,1”.
  • The final bit is the stop bit for byte 2, which is “0”.

To summarise, the complete 20-bit sequence for team 79 would be as follows:

led_flash_challenge_example

The validator for checking your transmission is a web application which I have posted at the following location:

I will set a validation station in KEG-036 where you can record your result once your circuit is working. Other tutors may set up validation stations in the other rooms, but that will depend on available cameras and light levels.

You are welcome to try the validator on your own laptop / PC. In principle, it should work on any modern PC with a webcam and up-to-date browser. However, since video capture is relatively new in HTML, I recommend using the current release of Google Chrome which is what I tested it in. Some people have successfully used it in the web browser on their phone.

Your tutor will be able to clarify anything you don’t understand about this.

Posted in Uncategorized | Leave a comment

RoboSumo Winners sem 2, 2018-2019

1st Place: Team Soul – Kei Shing, Bei Chen, Zunqi

2nd Place: Team Barbie – Vanesa, Laura, Caoimhe

3rd Place: Team CSR2019 – Roland, Carmen, Sean (not in photo)

4th Place: Team Ed-E – Thomas, Rafal, Stephen (not in photo)

Posted in Uncategorized | Leave a comment