Multiple inputs simultaneously

I am using an Arduino Uno or a Mega for a toy car track to time 4 cars.
Is there a way to get 4 inputs for the finish at one time.
I am using simple IR emitters and detectors for input in A0,A1,A2, and A3.
These is a small probability there could be a tie.
Here is my code for 2 inputs:

// cwcoleman - 250328
//Photodiode to digital pin 2 optional
int led13=13;                   //LED to digital pin 7  
int sen0=0;   //Readings from sensor to analog pin 0  
int sen1=1;

void setup()    
 {  
  pinMode(led13,OUTPUT);  
  digitalWrite(led13,LOW);      // LED initially off  
  Serial.begin(9600);         
 }  
int limit0 = 100; int limit1 = 1000;
int val0 = 0; int val1 = 0;
int done0 = 0; int done1 = 0;//boolean
int count = 0;
long start_time,elapsed = 0,finish_array[2]; 
void loop() 
 {
  if(count == 0) start_time = millis(); 
  count++;
  val0=analogRead(sen0);//photodiode reading
  val1=analogRead(sen1);
  
  if(val0 > limit0)        // OBSTICLE DETECTED  
  {  
    elapsed = millis()-start_time;
    finish_array[0] = elapsed;
   done0 = 1; 
  }  
  if(val1 > limit1)        // OBSTICLE DETECTED  
  {  
    elapsed = millis()-start_time;
     finish_array[1] = elapsed;
   done1 = 1;  
  }

    if (done0 ==1 && done1 ==1){
      Serial.println("done 0 && done 1 are true ");
      Serial.print(finish_array[0]);Serial.print(" ");Serial.print(finish_array[1]);
      Serial.println("\n");
    }
    }

 

This is how I connect pin A0

Look a familiar pretty picture, where have I seen it before, in another of your posts maybe?

You are using a analogue to digital converter, each one takes about 100uS to perform, so you can never read them all that the same time.

You need to use a digital input for the readings. Then using direct port addressing methods read them all at the same time.

By the way never connect an LED direct to pins without using a series resistor. The very very first Arduinos used to have a built series resistor on pin 13. But that was removed about 20 years ago.

1 Like

Thank you!
How do I do this:
"You need to use a digital input for the readings. Then using direct port addressing methods read them all at the same time."

I created the graphic with a different user account. I forgot the email I used for that account or it was an email where the vendor went out of business.

The "best" approach might depend on what your definitions of "at one time" and "simultaneously" are. What do your requirements state the timing tolerance is?

You do this by making the pins you want to use inputs in the setup function.

See this link
PortManipulation

Each car starts at the same time. Is there a way to collect 4 inputs at the same time with 4 different IR LED detectors.

Again, what is the tolerance on "at the same time"? Nanoseconds? Microseconds? Seconds? Hours? Days?

1 Like

microseconds

Ill read https://docs.arduino.cc/retired/hacking/software/PortManipulation/ and try to figure out how to apply it. Thank you!

You need a device that runs an order of magnitude faster than the order you wish to measure. You can only be as precise as your least precise device.

Be realistic. Do you really need to determine which car crossed a finish line within microseconds? It's possible but seems a bit much.

Arduino can easily read a set of inputs to within microsecond precision by simply reading an entire port or two at once. Now the problem moves down one level to finding sensors that will respond that quickly. How fast are your photodiodes?

I totally agree with the finish line issue. We have a plan now for that.
The leds are Infrared (IR) 940nm I'm really not sure how (Wavelength: 940nm ) can tell me how fast I am getting getting data. I do believe PIN 2-11 are faster and A0-A5. Thanks for all the tips!

The reaction time of a sensor is not related to the IR-frequency.

And again: be realistic. What would be the biggest still acceptable time-difference in measuring the 4 inputs?

As soon as you describe pretty detailed:

  • what cars this are ?
  • what maximum speed do they achieve?
  • what is the distance the cars drive before reaching the finish line?

Are the cars just running down a 50 cm ramp and you do measure then after 60cm
All cars are optimised to the max and the usual difference between the cars is 1 or 2 millimeters?

I guess not.

For the reaction time you have to lookup the datasheet of your IR-diodes as a start
then you have to build a maximum fast switching circuit and then measure the latency of the whole circuitry.

Sounds like a lot of work for which you need a lot of electronic knowledge.

So again: please describe your project pretty detailed.

I guess a latency of 1/100 second will be sufficient.
I guess the whole thing is a 20 to 100 dollar hobby-project.
If two cars should really reach the finish-line in the same 1/100 second let them be both on place one for the rare occurance of beeing at the finish line this close

I will be fine with 4 inputs. I am going to switch to digital pins.
the track in 30 to 40 feet. the cars travel the distance in about 3 to 6 seconds.
we will tolerate 1/100 and rerace cars that appear to finish at the same time
thank you so much!

Just an academic question…
You suggest that each car will be timed from its own start of movement.

Shouldn’t the start be determined from the starters light?

each car will start with a manual drop lever. all starting at the same time. good idea having an RGB LED at a start indicator!

I think someone is missing the point of timing the race. Yes, you can read all 4 pins at the same time, but that is not the real problem. The real problem is how fast can you read 4 pins and save the time, then read the 4 pins again and save the new time, etc. The cars will be changing the values of the 4 pins at an unknown rate, so your reading can only indicate, on an Arduino UNO at 4 millisecond increments. You will have inaccurate timing if you read the 4 pins faster than that! Plus you have to sync the reading with the ms clocking rate.
The world of race timing gets rather complicated.

Where does that come from? Are you perhaps thinking about the 4 microsecond resolution of micros() on a 16MHz AVR? That's pretty good.

I'd look into interrupts. Besides connecting the detectors to individual pins on the same port, you could "OR" them together with a hardware gate and connect the result to an interrupt input. In the ISR you'd read micros() for timing and read the port to see which input(s) fired it.

Depending on how long the race lasts, you could also use Input Capture mode on Timer1. But, it's only 16 bits so you lose resolution as the duration of the race gets longer.

1 Like

Yes, you are correct. Right thinking, but wrong timing!

No. You can read each input pin sequentially, determine whether the logic level changed from the last reading, and if so, store the time associated with that change.

Reading all inputs at once is possible if the inputs are on the same port, but you still have to go through each bit sequentially and test whether there was a level change associated with that bit.