Blink LED for short duration whenever a new sensor event is registered

Hello Everyone, I am working with Arduino Mega to detect brief touching (i.e. licking of spout by rats) with a capacitance sensor. And the aim is two-folds: first, to detect the capacitance change and send TTL outputs to a PC which will registered the touching events; second, to blink a LED (i.e. 20Hz, 1s) whenever there is a touching event being detected. I have the first aim met quite well with the following code.
The problem is, the code for generating blinking events (the part that is commented out) are based on delay function, which halts/interrupts the TTL outputs to PC until finishing the blinking.
I have searched the forum for alternatives. It seems the Interrupt function or Arduino timer could help. Is this correct? Thanks a lot Arduino friends.

int  sensorPin  =  A0;   // capacitance sensor
int  ledPin  =  13;   // LED pin
int  BNC_OUT  = 7; //TTL out to PC
int  sensorValue =  0;  // value from capacitance sensor
int  threshold = 25; // threshold for detecting rising/falling of capacitance

void setup()
{
pinMode(ledPin,OUTPUT);
pinMode(BNC_OUT,OUTPUT);

}

void loop(){

sensorValue =  analogRead(sensorPin);

if(sensorValue > threshold) {
  digitalWrite(BNC_OUT,  HIGH);
//  Blink(ledPin, 25, 20);
  }else{
  digitalWrite(BNC_OUT, LOW);
  }
  
delay(1);

}
//
//void Blink(byte PIN, byte DELAY_MS, byte loops) {
//  for (byte i = 0; i < loops; i++)  {
//    digitalWrite(PIN, HIGH);
//    delay(DELAY_MS);
//    digitalWrite(PIN, LOW);
//    delay(DELAY_MS);
//  }
//}

I have searched the forum for alternatives. It seems the Interrupt function or Arduino timer could help. Is this correct?

No, have a look at the example BlinkWithoutDelay in the IDE. It's quite easy to integrate that into your sketch.

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