Hi! I am very new to C++ but thought it would be cool to use a SparkFun kit and a Photogate that my school had laying around. I ended up attempting to calculate the period of a pendulum I created using the Photogate.
I connected a Digital Protoboard to the Redboard then the Photogate. I used this code below I found from the Vernier Photogate Tutorial Page to use in the Arduino IDE. Though this code works it only outputs the time of time when the photogate was blocked.
So in essence I need to track the time it takes the pendulum to first block the photogate, then block again moving to the left, and finally just blocking the gate one last time as it comes back to center.
So I believe I need a section of code that captures the first time stamp, a section of code that waits until a second blocked event happens, then captures the second time stamp and finally collects the difference? I can't seem to wrap my head around this and I can't even begin to understand how to code it. I was hoping maybe this forum could provide some insight on this project.
Code:
/* VernierTutorialPhotogate (v2018)
* This sketch will send a status message to the Serial
* Monitor on whether the Photogate is blocked or unblocked.
* It lists the time that the photogate is blocked in microseconds since the program started running or
* since the last time the counter overflowed.
* It will also turn on the LED (pin D13) when the
* photogate is blocked.
*
* Plug the Photogate into the Digital 1 port on the
* Vernier Arduino Interface Shield or into a Digital Protoboard
* Adapter wired to Arduino pins 2, 3, 4, and 5.
*/
int photogatePin = 2; //create global variable for pin assignment to sensor
intLEDpin = 13; //create global variable for pin assignment to LED
int photogateStatus; //create global variable for photogate status: LOW=blocked, HIGH=unblocked
int oldStatus = HIGH;
unsignedlong timeus = 0; //Time in us
void setup()
{
Serial.begin(9600); // set up Serial library at 9600 bps
pinMode(LEDpin, OUTPUT);
Serial.println("Vernier Format 2");
Serial.println("Photogate blocked times taken using Ardunio");
Serial.print("Time");
Serial.print("us");
}; // end of setup
void loop()
{
photogateStatus = digitalRead(photogatePin); //low when blocked
if (photogateStatus == LOW) {
digitalWrite(LEDpin, HIGH); // turn on LED
if (oldStatus == HIGH) {
timeus = micros();
Serial.println(timeus);
}
} else
digitalWrite(LEDpin, LOW); // turn off LED
oldStatus = photogateStatus;
}; // end of loop