Signal Delay Problem

Hi! Im making a lap timer using ir sensors, it's working but it has a noticeable delay. I programmed an LED to light up on a certain distance between emmiter and sensor but the LED doesn't immediatly turn on when the distance is reached, I can see in the serial monitor, it's delayed around 1sec, not fast enough for racing quadcopter.

Is the arduino not fast enough for this?

How can we help without seeing the code?

Weedpharma

Is the arduino not fast enough for this?

Probably about 10,000 times faster than required, which leaves us with your code and/or hardware (most likely code). So we need to see that.

//Lap Timer

//int button = 4;
//int buttonStatus;
int timerLed = 6;
int timerLedStatus;
//int buzz = 5;
int ir = A1;
int irValue;
int irMappedValue;
int timer;
long previousTime = 0;
long interval = 1000;

void setup() {
  Serial.begin(57600);
  pinMode(ir, INPUT);
//  pinMode(button, INPUT);
  pinMode(timerLed, OUTPUT);
  digitalWrite(timerLed, HIGH);
}
void loop() {
//  buttonStatus = digitalRead(button);
  timerLedStatus = digitalRead(timerLed);
//if(buttonStatus == HIGH) {
  //    digitalWrite(timerLed, HIGH);
if(timerLedStatus == HIGH) {
  timerOn();
} 
}

void timerOn() {
  irValue = analogRead(ir);
  irMappedValue = map(irValue,0,942,0,255);
  irMappedValue = constrain(irMappedValue, 0,255);
 Serial.println(irMappedValue);
//   Serial.println(irValue);
 unsigned long timer = millis();
 if(timer - previousTime > interval) {
   previousTime = timer;
 Serial.println(timer/1000);
 if(irMappedValue >= 25) {  //Show Time if signal >= 25
   Serial.print("Time :  ");
   Serial.print(timer/1000);
   Serial.println("    ");
   digitalWrite(timerLed, LOW);
   Serial.println("Distance : ");
   Serial.print(irMappedValue);
   Serial.println("    ");
 }
 }
}

Thanks for the replies! Sorry I forgot to post my code. I'm expecting for the timer to show as soon as signal reaches 25, but it doesn't.

My crystal ball says that your IR sensor catches whatsoever, but not what you want it to indicate.

You can find out more by a simple test program, that only shows the on/off state of the IR LED on another LED. This way you can be sure that it's not the sketch that delays the signal. Then let the object pass the beam, and find out when the LED goes on and off.

Okay...

You need to run the auto format function to clean up your indenting. If it appears to scramble your code then the code was wrong.

You set an LED output HIGH and then read that pin? It's a known technique, but usually you do that when your microcontroller only has a few bytes of memory. The Arduino has kilobytes, so this isn't necessary and it's confusing to read an output.

Then you read the analog input and do some mapping. Good.

Then you have a blink-without-delay type of timer that runs once per second. That seems to be the part of the code that evaluates the result and turns the LED off. So that is why it is slow to turn it off.

MorganS:
Okay...

You need to run the auto format function to clean up your indenting. If it appears to scramble your code then the code was wrong.

You set an LED output HIGH and then read that pin? It's a known technique, but usually you do that when your microcontroller only has a few bytes of memory. The Arduino has kilobytes, so this isn't necessary and it's confusing to read an output.

Then you read the analog input and do some mapping. Good.

Then you have a blink-without-delay type of timer that runs once per second. That seems to be the part of the code that evaluates the result and turns the LED off. So that is why it is slow to turn it off.

The LED is just an indicator that the sensor value reached my choicen signal(25), it'll turn off and the timer will show the Time.

The IR sensor can read the emitter properly I can see the value increases the closer the emitter, the value in serial monitor shows very fast without delay.

Do you mean the millis timer could be the one delaying? I thought it doesn't delay the code like the "delay" function. Thanks! Ill try to fix it. might need your help again :slight_smile:

Thanks guys! yes the millis was making the delay. I moved the curly braces to proper position and it fixed the delay! :slight_smile: