Read light sensor?

#1

Hi all,

I am working on a scanner that uses just the ambient light sensor and the screen as a camera scanner. The way this would work is that this psuedocode:

turn all screen black.
turn pixel 1 white full brightness.
read ambient light sensor data reflected by white paper to be scanned
turn all screen black
read ambient light sensor data again.
if old sensor data is different from new light sensor data, turn that pixel green.
loop to next pixel.

This is what I have so far but it just loads a black screen.

import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;

Context context;
SensorManager manager;
Sensor sensor;
LightListener listener;
float lux;

void setup() {
  fullScreen();
  
  context = getActivity();
  manager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
  sensor = manager.getDefaultSensor(Sensor.TYPE_LIGHT);
  listener = new LightListener();
  manager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_FASTEST);
  
  textFont(createFont("SansSerif", 30 * displayDensity));
}

void draw() {
background(0);
loadPixels();  
// Loop through every pixel
for (int i = 0; i < pixels.length; i++) {
  color c = color(255);
  // Set pixel at that location to random color
  pixels[i] = c;
float checkone = lux;
//set all black
color d = color(0);
pixels[i] = d;
//check sensor again.
float checktwo = lux;
if (checkone != checktwo) {
//if changed, set pixel to blue.
color e = color(255,0,0);
pixels[i] = e;
}

}
// When we are finished dealing with pixels
updatePixels(); 
}

class LightListener implements SensorEventListener {
  public void onSensorChanged(SensorEvent event) {
    lux = event.values[0];   
  }
  public void onAccuracyChanged(Sensor sensor, int accuracy) {
  }
}
#2
float checkone =lux;
float checktwo =lux;
if (checkone != checktwo) {
   ...
}

This is why your screen does not change. Not sure if I understand your applciation, but I think this is what you want:

final color BLACK = 0xff00ff00;
final color GREEN = 0xff000000;

float prevLux;

void draw() {

  if(lux != prevLux){
    background(GREEN);
    prevLux=lux
  }
  else{
     bacground(BLACK);
  }  
}

Note there is no need to iterate through all the screen pixels as the light sensor will provide you only one value. For completeness for other forum goers: Android environmental sensor. I suggest you print into the screen the value of the sensor so you can verify it is working properly for you.

If you determine the range of the value read by this sensors (aka. the min and max allowed values), then you could use lerpColor to define shades of green. For instance, assuming the lowest value from the light sensor is zero, then:

float lux_percentage = lux / MAX_LIGHT_VALUE;
background(lerpColor(BLACK,GREEN,lux_percentage));

Kf

#3

@socraticbot ===
_ i am very surprised that you can get some “black screen” (or green!) with this code; normally you have only to get a bunch of errors because a) your imports are not ok b)context is not at all the same than “getActivity()”;

  • now the first thing to know is wether your phone has or has not this kind of sensors: a lot of phones have not all the sensors; you can verify it looking at the values returned by the sensorEventListener or, better, getting the sensorList from the manager, using “getSensorList(Sensor.TYPE_ALL)”, adding them to some List and asking for sensorInfo in a loop.
#4

No no, I’m not certain if I was clear enough based on my original post.
The way this should work is that it acts as a camera without using the actual camera ie; it turns on the first pixel in the 1d pixel array of the screen bright and looks for that light to be detected by the ambient light sensor (reflected by paper to be scanned). If it is detected, turn that pixel green and then go to the next pixel. If it is not detected, it probably saw the black ink from the paper not reflecting the light from that pixel at the coordinate.

This would work light a reverse camera; instead of the hardware cam being able to see many pixels, it can only see one at a time and the pixels turned on one at a time with screen act as the pointer.

I am still not understanding where my code is incorrect with the sensor listener setup. My phone does have an ambient light sensor.

#5

@socraticbot ===
and so you are sure that your code does note fire any error: copy it and try it!