Create sketch for newbie

I want to create an arduino sketch that will take input from the positive and negative terminals of an LED and keep a cumulative count of how many times the LED lights up and output this to a 128x64 OLED and it will show how many times the LED lit up in the last hour, and the last 24 hours, and the last month.
Any ideas/suggestion greatly appreciated for this neophyte.

UPDATE: I have completed the Muon detector, it works (flashes the LED). I made a video that may help to see the help I am looking for - the arduino sketch(s) for OLED cumulative counters.
Video: Muon Detector- it works! Still need help... - YouTube
Pictures: Muon Stuff - studio808

Welcome to the forum

What will be turning the LED on and off ?

Just to be clear, this is an external LED not originally connected to or powered from the arduino. Is that correct?

See how the following setup (Fig-1) goes!
You excite LED1 with known analogWrite() function for 5 seconds during which LED1 will be On/Off. You measure the voltage at A0-pin to detect the Off-states of LED1, which can be displayed on SM/OLED.

LED1
Figure-1:

The OP has been asked to take connection from across the terminals of the LED. When the LED is ON, the voltage across it is about 2.5V (my LED shows: 1.7V) which is below VIH (3.0V) specification of ATmega328P MCU of the UNO Board; so, On/Off conditions of the LED can not be accounted by digitalRead() function.

So, my idea is to On/Off the LED by analogWrite(9, 128) at 490 times/sec and then count the On states for 5-sec through the evaluation of analogRead(A0) function. Let me do the experiment.

Sampling a 490Hz digital signal read using analogRead (100us a pop)?

Are you feeling OK?

This is the outcome of my experiment with 7.8% error which can be brought down to 2% (I hope so) by tuning the delay! Theoretical values is: 490x5 = 2450.

2258
2261
2252
2256
2260

We actually don't know what he has been asked to do, only what he has decided to do

The delay has been cranked down to 900 us to get an error of 0.85%. The theoretical count is: 490 x 5 = 2450

Experimental Counts:.

2472
2473
2470
2470
2472

Fixed

1 Like

Yes, that is correct.

OK, thanks!

OK, I'm learning as I go from watching YT vids. Actually I'm making a Muon detector that doesn't involve an arduino. I have the components for that and a diagram for connections. It will light up a LED every time a muon is detected. No problem getting that far but I don't want to have to sit around and watch for the LED to blink every now and then so that is why I want to add an arduino nano after this and output to an OLED and have it keep track of the muons detected (daily and weekly and monthly cumulative totals displayed on the OLED.
I have purchased a 74HC08 And Gate that I think is needed to work with the Nano. Presume the 74HC08 will be between the LED and the Nano. My sketch knowledge is very limited so that is where I was looking for guidance inasmuch as the And Gate will, I believe, track the LED lighting up and able to send that to the nano.
Hope I explained that OK. In the next few days I should have the muon detector completed with the LED flashing when a muon is detected and I can update with a video link.
Thanks for all the kind replies!

Correction- the 74HC08 will be used to detect the simultanuous input from 2 geiger counters to light up the LED. I have updated and corrected my original post.

Thanks! I'm doing more research and it may be as simple as a State Change Detection (Edge Detection) for the arduino.

Then I would just need a sketch that will display the various cumulative totals on an OLED.
Stay tuned...

1. Even in 3.3V ESP32S, the VIH is ~= 2.5V (Fig-1, minimum).


Figure-1:

2. Voltage drop across LED varies from 1.5V to 2.5V with 10 mA current passing through it. No doubt that the On/Off states of a LED are digital events; unfortunately, we cannot handle them using digitalReadd() instruction. My ADC approach has given acceptable results in Arduino UNO Platform.

I have used the ADC as a comparator; however, as you have suggested, I will do the experiment using built-in dedicated comparator of UNO.

In that case, you have to set the discrimination level (the threshold) externally using hardware at one input-pin (usually AIN0) of the comparator, which depends on the voltage drop of the LED. Better to adjust it programmatically using the internal ADC.

unsigned long presentMillis;
int myCount;

void setup()
{
  Serial.begin(9600);
  analogWrite(9, 128);  //490 Hz square wave
}

void loop()
{
  presentMillis = millis();
  while (millis() - presentMillis < 5000)
  {
    unsigned int y = analogRead(A0);
    if (y > 347)  //1023/5*1.7 ~= 347; adjustable discrimination level = f(LEDVOLT)
    {
      myCount++;
    }
    delayMicroseconds(900);  // to adjust for the ON-time of 490 Hz PWM
  }
  Serial.println(myCount);
  myCount = 0;
}

Output: for 5-sec (expected: 490 x 5 = 2450) with 0.9% error.

2471
2472
2473
2470

I have updated my original post with video and pictures.

Yep, 5V.