Thread: Using a Photogate and a Redboard to calculate the Period of a Pendulum

  1. #1
    Registered User
    Join Date
    May 2019
    Posts
    1

    Using a Photogate and a Redboard to calculate the Period of a Pendulum

    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
    Last edited by Salem; 05-19-2019 at 10:24 PM. Reason: Removed crayola

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating a pendulum with gravity
    By RyRy in forum C Programming
    Replies: 1
    Last Post: 01-11-2014, 01:33 PM
  2. Print after period program
    By ejiroy in forum C Programming
    Replies: 3
    Last Post: 03-01-2011, 01:39 AM
  3. Adding trial period to software
    By BobS0327 in forum C Programming
    Replies: 17
    Last Post: 01-03-2006, 02:13 PM
  4. keybd_event type a (period)
    By Giraph in forum Windows Programming
    Replies: 6
    Last Post: 10-18-2003, 08:33 PM
  5. Making anything...period
    By Stan100 in forum Game Programming
    Replies: 19
    Last Post: 10-01-2002, 09:00 AM

Tags for this Thread