How do you Control the Light Sensor to Avoid Constant Readings?

Hi everyone,

I’m working on a project in MIT App Inventor where I use the light sensor to detect flashes of light. Instead of having the sensor running constantly, I want to store multiple flashes and activate the phone’s flash when I detect four "1s" (four flashes).

My idea is to:

  1. Detect a flash and store a value of 1.
  2. Keep a count of detected flashes.
  3. Activate the phone's flash once four flashes (four "1s") have been detected.

How can I implement this logic efficiently in MIT App Inventor? Is there a way to control when the sensor reads values to avoid continuous processing?

Any help or suggestions would be greatly appreciated!

Thanks!

Here's the Help on the Light Sensor:

https://ai2.appinventor.mit.edu/reference/components/sensors.html#LightSensor

Detecting a flash requires you to keep a history of lux readings by time.
The Clock1.SystemTime block will return milliseconds from 1970, which (if adjusted to app start time) can give you an x axis for timestamped lux readings in a two column table (timestamp, reading).

You only need keep the last minute or so of readings, since the count should reset after you reach 4 flashes.

A flash might be detected as a sequence of three readings in the table, where
lux(n-1) < lux(n) and
lux(n+1) < lux(n)
( a local peak in the lux values.)

If you clear the table when you reach 4 peaks, you need not bother keeping the timestamp column.

Here's my sample, with a quack every four flashes.

You would need an extension for controlling the phone's flash, though.



flash_counter.aia (6.4 KB)

Let me try and i'll give you feedback. THKU very much for your reply and how fast you were :slight_smile: