Sports performance reaction laser, Need help

Hey guys!

I’m building a laser timing gate for my lab and ran into a stopping point. Essentially I am trying to build a prototype laser timing gate that will display a direction of either left or right facing triangle when the laser is broken. This will be used for sports performance drills and data collection. As of right now I have it working to display a single triangle pointing to the right and it works! When the beam is broken it will display a triangle on an adafruit LED Matrix.

What I’d like to do is use the matrix triangle as an object (one that points right and one that points left) and have it called in either a random order or a predetermined order so that it doesn’t always point in one direction when the laser is broken. My idea currently is to create an array of states the LED could be in (either a 1 as right or a 2 as left), and have it choose a direction each time.

OR to create 8 states like (1,2,2,1,2,1,2,1), and have the loop increment each time across the array until it reaches the count number of 8 so that it moves from left to right facing arrows and we can record the direction the subject had to move and can pair it with the motion capture data we are getting.

So my question is: Can I turn the adafruit matrix function into a variable that can be called and put into an array? If so, how could I do that? Thanks.


/*

This code is for use with a Light Dependent Resistor as a second device of a timing gate
for UC Health Motion Capture Lab under Dr. Kate Worster.

This portion of the gate will receive a signal from Gate 1 via an LDR, and signal
what direction an LED matrix will signal a runner to go.

This device should be able to output serial monitor data to a MATLAB plot,
as well as send a 5V pulse to a vicon monitor to be labeled as a general GAIT event
in the Nexus software.

created 10 Feb 2020
by Gary J Schneider

*/

/////////////////////////Libraries for LED Matrix/////////////////////////
#include “Adafruit_GFX.h”
#include “Adafruit_HT1632.h”

//////////////////////////Assign Pins/////////////////////////////////////
int ledPin1 = 5; // green led pin(2)
int ledPin2 = 6; // red led pin(4)
int photo = A0; // photoresistor pin(10)
int x = 1; // initial value for x

//////////////////////////LED Display Pins////////////////////////////////
#define HT_DATA 2
#define HT_WR 3
#define HT_CS 4
#define BACKCOLOR 0xF800

Adafruit_HT1632LEDMatrix matrix = Adafruit_HT1632LEDMatrix(HT_DATA, HT_WR, HT_CS);

//////////////////////////Set Up//////////////////////////////////////////////
/*
Begin serial monitoring of the photoresistor, ensure the LED’s are set
to output.Will provide the users a notifcation of power on through a single
flash of the LED panel that will occur when turned on.
*/
/////////////////////////////////////////////////////////////////////////////

void setup()
{
Serial.begin(9600);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(photo, INPUT);
matrix.begin(ADA_HT1632_COMMON_16NMOS);
matrix.fillScreen();
delay(1000);
matrix.clearScreen();

}

///////////////////////////////////Loop//////////////////////////////////////////

void loop()
{
//Serial functions:
//1. Assign x the value of the LDR sensor
//2. Print the value of the LDR sensor in serial monitor
//3. Print LDR value in the serial monitor
//4. Inital value of c = 0

x = analogRead(photo);
Serial.print("Reading: ");
Serial.println(x);

if (x < 600) //Laser is broken and light value under 600, do this…
{
digitalWrite(ledPin1, LOW); // turn green led off
digitalWrite(ledPin2, HIGH); // turn red led on

** matrix.fillTriangle(4, 0, 4, 15, 22, 8, BACKCOLOR); //Creates right pointing triangle**
matrix.writeScreen();
delay(1500); //Delay the clearing of the screen so runner has time to read
}
else (x > 600)// if laser link is in tact…
{
digitalWrite(ledPin1, HIGH); // turn green led on
digitalWrite(ledPin2, LOW); // turn red led off
matrix.clearScreen();
}

}

okay so I reformatted it to have functions that will change directions, and I can call a different function to display a different arrow.

So my next question is, could I make those functions a direction in an ordered list that I can call up to the maximum number of items in that list and then have it restart the loop.

So basically the first trial it would produce a left arrow, the second a right, the second a right… etc until it reaches 8 times through the loop, in which it would then reset the counter and repeat.

/*

This code is for use with a Light Dependent Resistor as a second device of a timing gate
for UC Health Motion Capture Lab under Dr. Kate Worster.

This portion of the gate will receive a signal from Gate 1 via an LDR, and signal
what direction an LED matrix will signal a runner to go.

This device should be able to output serial monitor data to a MATLAB plot,
as well as send a 5V pulse to a vicon monitor to be labeled as a general GAIT event
in the Nexus software.

created 10 Feb 2020
by Gary J Schneider

*/

/////////////////////////Libraries for LED Matrix////////////////////////////////////////////////
#include “Adafruit_GFX.h”
#include “Adafruit_HT1632.h”

//////////////////////////Assign Pins////////////////////////////////////////////////////////////
int ledPin1 = 5; // green led pin(2)
int ledPin2 = 6; // red led pin(4)
int photo = A0; // photoresistor pin(10)
int x = 1; // initial value for x
int dir = {1,2,2,1,2,1,2,1}; //array of values for direction where 1 = left and 2 = right
int i;
int left = 1;
int right = 2;

//////////////////////////LED Display Pins/////////////////////////////////////////////////////////
#define HT_DATA 2
#define HT_WR 3
#define HT_CS 4
#define BACKCOLOR 0xF800

Adafruit_HT1632LEDMatrix matrix = Adafruit_HT1632LEDMatrix(HT_DATA, HT_WR, HT_CS);

///////////////////////////////////////Function Calls//////////////////////////////////////////////

void breakright() //Parameters set for a triangle to point right if beam broken
{
digitalWrite(ledPin1, LOW); // turn green led off
digitalWrite(ledPin2, HIGH); // turn red led on

matrix.fillTriangle(4, 0, 4, 15, 22, 8, BACKCOLOR); //Creates right pointing triangle
matrix.writeScreen();
delay(1500); //Delay the clearing of the screen so runner has time to read
}

void breakleft() //Parameters set for a triangle to point left if beam broken
{
digitalWrite(ledPin1, LOW); // turn green led off
digitalWrite(ledPin2, HIGH); // turn red led on

matrix.fillTriangle(20, 0, 20, 15, 1, 8, BACKCOLOR); //Creates right pointing triangle
matrix.writeScreen();
delay(1500); //Delay the clearing of the screen so runner has time to read
}

void lasertact() //If beam is in tact, turn on green LED and clear screen
{
digitalWrite(ledPin1, HIGH); // turn green led on
digitalWrite(ledPin2, LOW); // turn red led off
matrix.clearScreen();
}

int doActionsArray =
{
breakleft,
breakright,
lasertact,
};

//////////////////////////////Set Up////////////////////////////////////////////////////////////////
/*
Begin serial monitoring of the photoresistor, ensure the LED’s are set
to output.Will provide the users a notifcation of power on through a single
flash of the LED panel that will occur when turned on.
*/
///////////////////////////////////////////////////////////////////////////////////////////////////

void setup()
{
Serial.begin(9600);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(photo, INPUT);
matrix.begin(ADA_HT1632_COMMON_16NMOS);
matrix.fillScreen();
delay(1000);
matrix.clearScreen();
}

////////////////////////////////////////Loop////////////////////////////////////////////////////////

void loop()
{

x = analogRead(photo); // Serial monitor to read photoresistor data and display
Serial.print("Reading: ");
Serial.println(x);

if (x < 600) // if link between laser and photoresistor… (check serial monitor and change this # to your liking)
{
breakleft();
}

else (x > 600); // if laser link is in tact…
{
lasertact();
}

}

Please edit your post to add code tags, as described in How to use this forum.

Hi, Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum". OR http://forum.arduino.cc/index.php/topic,148850.0.html. Then look down to item #7 about how to post your code. It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom... :)

If you just want a random left/right you can read an unused (not connected) analog pin, and use the last bit to choose direction.
An unconnected analog pin will return a (pretty) random value when read.

Something like this:

:
  if (x < 600) // if link between laser and photoresistor... (check serial monitor and change this # to your liking)
  {
    if (analogRead(A1) & 1) { //Use bitwise AND to isolate the lowest bit. Will return true if lowest bit is 1
      breakleft();
    }
    else {
      breakright();
    }
  }