Code Critique/Guidance for Reading Activation Times of Phototransistor (TCRT5000)

Hello there. I'm testing a project prototype that involves 3 circuits, each of which has a 9V side activated by an LED-phototransistor (TCRT5000), which then opens a MOSFET gate for a 24V side. I have 3 copies of this circuit at different positions, and I want to just read the position of a ball rolling down inclined tracks (expected on the scale of tens/hundreds of milliseconds). My plan is to hook 3 digital arduino pins to the 9V side between the phototransistor and the MOSFET of each of the 3 circuits, and to use the following code to get the readings. As this is my first arduino project, any critiques/guidance on my code would be appreciated. Thanks.

int opt_sensor_A = 2;
int opt_sensor_B = 4;
int opt_sensor_C = 7;

bool sensor_state_A;
bool sensor_state_B;
bool sensor_state_C;

int time_at_sensor_A;
int time_at_sensor_B;
int time_at_sensor_C;

void setup() {
  pinMode(optSensorA, INPUT);
  pinMode(optSensorB, INPUT);
  pinMode(optSensorC, INPUT);

  Serial.begin(9600);
}

void loop() {
  sensor_state_A = digitalRead(optSensorA);
  sensor_state_B = digitalRead(optSensorB);
  sensor_state_C = digitalRead(optSensorC);

  if (sensor_state_A == HIGH) {
    time_at_sensor_A = millis();
    Serial.println('Time: 0 ms');
  }

  if (sensor_state_B == HIGH) {
    time_at_sensor_B = millis() - time_at_sensor_A;
    Serial.print('Time: ');
    Serial.println(time_at_sensor_B);
  }

  if (sensor_state_C == HIGH) {
    time_at_sensor_C = millis() - time_at_sensor_A;
    Serial.print('Time: ');
    Serial.println(time_at_sensor_C);
  }
}

When timing something, like a switch closure, most people use a state machine that detects and records when the switch becomes closed, not whether it is closed.

In Arduino you will find an example at Files>02.Digital>StateChangeDetection

1 Like

Nice. Thanks.

EDIT: Wait, does that example really apply to my case, though? I don't care about recording the time at which each phototransistor changes back to its LOW state. I just want a reading whenever each one gets activated to its HIGH state.

Substitute "gets activated" for "becomes closed" in reply #2.

By the way, the result of digitalRead() for a closed switch can be either HIGH or LOW, depending on how the switch is wired.

Ah, if I understand correctly, is the point of refactoring my code to a state-change-detection sort of logic to prevent the code from printing unnecessarily multiple times for one activation, since presumably at its functioning frequency, the arduino would rerun the loop multiple times within the duration of a single activation ("HIGH") state of my circuit?

I take it you have not tried the code. The problem will be obvious.

Keep in mind that the only thing slowing down loop() (which is called tens, if not hundreds of thousands of times each second) is printing. At the low serial Baud rate of 9600 bits per second, it takes about 1 millisecond to print each character on the serial monitor.

1 Like

Yes, you’re correct. I’m still waiting for a few components in the mail, so I wanted to get a head-start on writing the program.

I didn’t realize the significance of the baud rate though. I’m going to have tinker with that setting in my test runs. Thanks for clarifying on that.

I suggest to use 115200 Baud, which is about 10 characters per millisecond. That can still get in the way of accurate timing, so some people wait until all the timing is done before printing the result.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.