Photoresistor Speed Question and Problem with Baud Rate/Serial.begin

Hey Everyone,
hope this is the right place to post something like this just picked up an Arduino a few days ago and am kinda stuck on my first project.

I need to detect an object flying past a photoresistor which is oposite to a Led and once detected turn different LEDs on. The problem is that the object is moving fast, roughly 105 m/s for the 10mm big photoresistor to notice it it has ~90micro seconds. To have a good detection rate I figgured the refresh rate of the photoresistor should be around 50 micro seconds.

My first Question is can a photoresistor wired into an Arduino Nano Clone manage that refresh rate?

My second problem is the program seems to operating on the given Baud Speed not the micro seconds values. Which is a problem since
1: Even if the photoresistor works at the speeds I mentioned above the equivalent Baud rate would be insanely high
2: My PC can barely handle a Baud rate of 1.000.000 after that it fries the aduino application.

If I remove Serial.begin and Serial.print the program wont work as in the LEDs that are supposed to turn on after detection never turn on. My understanding was that the serial stuff is only for displaying the data from your arduino on your computer. So why is it so invasive for the programm to remove it.

Thanks for any help in advance.

//Constants
const int pResistor = A0; // Photoresistor at Arduino analog pin A0
const int ledPin=9;       // Transistor Pin
const long blinkDuration = 500000;
const long SensorInterval = 50;
//Variables
long value;          // Store value from photoresistor
long currentValue;
long previousValue;
long previous;

byte ledState = LOW;           //   LOW = off

unsigned long currentmicros = 0;    // stores the value of micros() in each iteration of loop()

unsigned long previousLedmicros = 0;

unsigned long previousSensormicros = 0; // time when Sensor last checked

void setup(){
  Serial.begin(2000000);
 pinMode(ledPin, OUTPUT);  // Set lepPin - 9 pin as an output
 pinMode(pResistor, INPUT);// Set pResistor - A0 pin as an input (optional)
}

void loop(){
   currentmicros = micros();

readSensor();
updateLedState();
LedSwitch();
}

void readSensor() {
   if (micros() - previousSensormicros >= SensorInterval) {
     value = analogRead(pResistor);
   }
   Serial.println(value);
}

void updateLedState () {
  previousValue = currentValue;
  currentValue = value;
  if (ledState == LOW){
     if (currentValue - previousValue <= -20){
       ledState = HIGH;
       previousLedmicros = currentmicros;
   }
  }
  else{
    if (currentmicros - previousLedmicros >= blinkDuration) {
      ledState = LOW;
      previousLedmicros += blinkDuration;
   }
  }
}

void LedSwitch() {
   digitalWrite(ledPin, ledState);
}

Use a photo-transistor rather than a photo-resistor.

...R

This matter is getting close to use Interrupt to catch the event.
Your loop is running a at quite high speed and the buffer for Serial likely gets filled up and then the execution will be hanging there, waiting for buffer space.

Photo resistors (LDRs) are very slow.

Photodiodes, especially PIN diodes, are fastest, but must be used properly and generally require some additional circuitry. You can buy ready made modules, sometimes called "photogates".

Railroader:
This matter is getting close to use Interrupt to catch the event.
Your loop is running a at quite high speed and the buffer for Serial likely gets filled up and then the execution will be hanging there, waiting for buffer space.

Thanks for the suggestion. How would I go about using an Interrupt ? I've never heard of it

How would I go about using an Interrupt ? I've never heard of it

You don't need interrupts for this project and for beginners, use of interrupts introduces as many new problems as it solves.

The sensor hardware is the problem, so get that working first!

Use the Arduino Reference and see what You find there. Interrupt Service Routine is usually called "ISR".
An Interrupt is when a hardware signal, Your sensor, triggers an input and execution is immedeately jumping to the ISR, setting a flag, steppeing up a counter etc. When the ISR has finished its job the executiion continues at the Place it was interrupted.

jremington:
You don't need interrupts for this project

I can't imagine that polling can be guaranteed to capture an event that only last 50 microsecs. IMHO an interrupt is essential.

...R

Poor imagination! Polling can detect a change faster than an interrupt can possibly respond.

 while(PIND&4);  //wait for LOW on Pin D2

Here is the entire assembly listing for that line, as produced by avr-gcc:

    while (PIND&4) 
  80:	4a 99       	sbic	0x09, 2	; 9
  82:	fe cf       	rjmp	.-4      	; 0x80 <main>

There are at least 2 issues with the approach and code in the original post.

  1. A light dependent resistor isn't nearly fast enough to capture a 90 microsecond event. LDRs have response times on the order of milliseconds to tens of milliseconds and they're slower to recover from the light beam being broken than they are to respond to light. A phototransistor or photodiode based detector will be much faster, typically less than 1 microsecond.

  2. The standard analogRead() isn't fast enough to reliably capture a 90 microsecond event on an 8-bit Arduino since analogRead() takes about 110 microseconds. There exist Arduino libraries with faster read times, but a sensor that provides a digital output is probably a better approach since the project is concerned only with a binary decision.

jremington:
Poor imagination! Polling can detect a change faster than an interrupt can possibly respond.

 while(PIND&4);  //wait for LOW on Pin D2

Hmmmm ...

{A} that's all very well if you are content to twiddle your thumbs until something happens - I assume the OP would like his Arduino to do other stuff.

{B} I suspect an interrupt is registered much more quickly than your code can repeat - having been registered it is probable that there is no great urgency to deal with it.

...R

I suspect an interrupt is registered much more quickly than your code can repeat

Again, try to use some imagination.

jremington:
Again, try to use some imagination.

I have. I stick to my opinion.

I suspect we are thinking of different things.

...R

We clearly are thinking of different things.

I assume the OP would like his Arduino to do other stuff.

Sort of a reverse XY problem -- you seem to be trying to answer a question that the OP has not asked.

Gentlemen! Wait for somebody to get the chrystal online.

Hi,
What is the application?
What is the object you want to detect?
Do you just want to detect it apperance OR also measure its speed?

Have you googled;
arduino chronograph

Tom... :slight_smile: