-4

I need to check if a LED is blinking every 2 seconds...is it possible? I am using Arduino Mega 2560. Thank you.

1 Answers1

1

There are multiple options, depending on the LED itself.

If you have access to the wiring of the LED (I assume 5V !) you can connect a interrupt Pin of the Arduino with it and a common GND. Now you can count the "Turn Ons" and devide it by the Time, to get an average value, which should be equal to two.

Example Code would be (NOT tested!):

    #define MEASUREPIN          2       // Watch https://www.arduino.cc/en/Reference/AttachInterrupt for infos

    long measureStartTime{0};           // ms since start of first blink
    long runTime{0};                    // [ms]
    long avgTime;                       // [ms]
    volatile long cycles{0};

    void setup() {
      pinMode(MEASUREPIN, INPUT);
      Serial.begin(9600);

      attachInterrupt(digitalPinToInterrupt(MEASUREPIN), countCycles, RISING);
    }

    void loop() {
      if(measureStartTime == 0 && cycles == 0){  
        Serial.println("Blink not started"); 
      }else{
        if(measureStartTime == 0){
          measureStartTime == millis();
        }else{
          runTime = millis()-measureStartTime;
          avgTime = runTime/cycles;
          Serial.print("Average blink interval: ");
          Serial.print(avgTime);
          Serial.println("ms");
        }    
      }
    }

    void countCycles(){
      cycles++;
    }

If you haven't access to the wiring you can use a lightsensor, to generate a similar signal.

I hope that fits your needs, because i am not allowed to comment, so i couldn't get further infos.

H. Puc
  • 77
  • 1
  • 8