Trying to make an LDAT out of my Nano

This is my board:Arduino Nano 33 BLE Sense Rev2 — Arduino Online Shop

So this is everything I have tried so far in the code.
I am looking for direction and help this is my first Arduino.
The issue I am having is if I just hold a flashlight to the sensor and pass my threshold its elapsed time is 17ms which wouldn't be accurate enough.

I am also interested in maxing out the link rate (binterval) between my pc and the Arduino. I checked with: GitHub - LordOfMice/hidusbf: USB Mice Overclocking Software (for Windows)
and the Arduino polling between 1-16 with 1 being 1000hz and 16 being 62.5hz.

#include <PluggableUSBHID.h>
#include <USBMouse.h>
#include <Arduino_APDS9960.h>
#include <mbed.h>
#define SAMPLES_PER_SECOND 20000
#define PPI_CHANNEL (7)

#define ADC_BUFFER_SIZE 3
mbed::Ticker counterTicker;
USBMouse Mouse;

// Define ISR-based counter and related variables
volatile uint32_t isrCount = 0;
uint32_t clickTime = 0;

// ISR function to increment the counter
void isrCounter() {
isrCount++;
}

void setup() {
Serial.begin(58000);
while (!Serial);

if (!APDS.begin()) {
Serial.println("Error initializing APDS9960 sensor.");
}

// Initialize ISR-based counter
counterTicker.attach_us( isrCounter, 100 ); // Call ISRcounter function every 100 us.
}

void loop() {
// Simulate a left mouse click
Mouse.click(MOUSE_LEFT);

// Release the left mouse button
Mouse.release(MOUSE_LEFT);

// Introduce a small delay to ensure ISR has time to increment isrCount
delay(10); // Adjust this delay as needed

// Record the time when mouse clicked using ISR-based counter
clickTime = isrCount;

// Wait for the color reading
while (!APDS.colorAvailable()) {
// Wait for 0.01ms (or 10 microseconds)
// Delaying less than 1 millisecond is not practical in Arduino,
// so using micros() is not appropriate here.
}

int r, g, b;

// Read the color
APDS.readColor(r, g, b);

// Calculate sum of RGB values
int rgbSum = r + g + b;

// Check if sum exceeds 250
if (rgbSum > 100) {
Serial.println("RGB values exceed 250!");

// Print elapsed time since the program started using ISR-based counter

// Print elapsed time since the program started using ISR-based counter
Serial.print("Elapsed time: ");
Serial.print((isrCount - clickTime) / 10); // Convert ISR count to milliseconds
Serial.println(" ms");

Serial.print("Click time: ");
Serial.println(clickTime);
Serial.print("RGB: ");
Serial.print(r);
Serial.print(", ");
Serial.print(g);
Serial.print(", ");
Serial.println(b);

}

// Wait a bit before reading again (1000 milliseconds)
delay(1000);
}

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