Turn Sensor Values into High and Low

Hello group!

Pardon my posting this question in this forum as I answered my own question in another forum. From the following code how can make it so instead of using a threshold of numbers (which will change depending on the different lighting levels in a room) to determine high and low can I just use High or Low? Thanks in advance!

void loop(){
qtrrc.read(sensorValues);

for (unsigned char i = 0; i < NUM_SENSORS; i++){
Serial.print(sensorValues[i]);
Serial.print('\t');
Serial.print('\t');
}
Serial.println(); 
delay(250);
if(sensorValues[0] && sensorValues[1] < 600) {  // I WOULD LIKE TO KNOW HOW TO CHANGE THIS
if(sensorValues[0] && sensorValues[1] == LOW) { // TO THIS SO IT WILL WORK
digitalWrite(13, HIGH);
} else {
digitalWrite(13, LOW);
}
}

if you read an analog value you must compare it to soem threshold value if you want to produce soem HIGh or LOW value. I cant think of any other route or why that is a problem

The fragment of code you provide looks a bit wierd ; why " unsigned char" and not the more usual "int" or "byte" in your For loop ??

int threshold =97;
....etc

void loop
{

if (sensorValue > threshold)
{
digitalWrite(13, LOW);
}
else
{
digitalWrite(13, HIGH);
}

...... etc

@hammy:

The code is from Pololu QTRX IR sensors…the complete code is below. The problem…correct me if I am wrong. If I define an int threshold = 97; This would change in different lighted environments correct? 97 may light the LED in one room or it may not even light in another room correct?

#include <QTRSensors.h>

#define NUM_SENSORS   2                     
#define TIMEOUT       2500                   
#define EMITTER_PIN   QTR_NO_EMITTER_PIN     

QTRSensorsRC qtrrc((unsigned char[]) {3, 4, 5, 6, 7, 8, 9, 10},
  NUM_SENSORS, TIMEOUT, EMITTER_PIN);
unsigned int sensorValues[NUM_SENSORS];

void setup(){
Serial.begin(9600);
delay(500);
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);       
for (int i = 0; i < 400; i++){ 
qtrrc.calibrate();         
} 
digitalWrite(13, LOW);         
for (int i = 0; i < NUM_SENSORS; i++){
Serial.print(qtrrc.calibratedMinimumOn[i]);
Serial.print('\t');
Serial.print('\t');
}
Serial.println();

for (int i = 0; i < NUM_SENSORS; i++){
Serial.print(qtrrc.calibratedMaximumOn[i]);
Serial.print('\t');
Serial.print('\t');
} 
Serial.println();
delay(1000);
}


void loop(){
qtrrc.read(sensorValues); 

for (unsigned char i = 0; i < NUM_SENSORS; i++){
Serial.print(sensorValues[i]);
Serial.print('\t');
Serial.print('\t');
} 
Serial.println(); 
delay(250);
if(sensorValues[0] && sensorValues[1] < 600) { // THIS WOULD CHANGE IN DIFFERENT LIGHTED ENVIRONMENTS

digitalWrite(13, HIGH);
} else {
digitalWrite(13, LOW);
}
}

If I define an int threshold = 97; This would change in different lighted environments correct? 97 may light the LED in one room or it may not even light in another room correct?

True. That is why you normally use that sensor in combination with an LDR, to detect the amount of light in the room, and adjust the threshold accordingly.

You could create a function, isAboveThreshold(), that takes two arguments and returns true or false.

   if(isAboveThreshold(sensorValues[0], 600) && isAboveThreshold(sensorValues[1], 600))
   {
       // Both values are above the threshold
   }
bool isAboveThreshold(int value, int threshold)
{
   return value > threshold;
}

@PaulS: thanks for that code tip it does work as expected making a function and defining a threshold. Now is there a way to establish a dynamic threshold or is there such a thing to accomplish? Basicially put if the sensor is covered LED ON if the sensor is not covered LED OFF...or close to it....these sensors have a 10mm detecting range. The code below is what I've used before (on other IR Sensors) and in doing so the LED does turn on and off....however my detection range is 0mm-3mm which is odd. If I must use calibration with the current sensors how do I find and keep the 500 threshold as an example in any lighting condition?

const int xing1  = 3;
const int xing2  = 4;

int xingstate1   = 0;
int xingstate2   = 0;

void setup() {
Serial.begin(9600);
pinMode(13,        OUTPUT);
pinMode(xing1,      INPUT);
pinMode(xing2,      INPUT);
digitalWrite(xing1,  HIGH);
digitalWrite(xing2,  HIGH);
} 

void loop(){
xingstate1 = digitalRead(xing1);
xingstate2 = digitalRead(xing2);

Serial.print(xingstate1);
Serial.print('\t');
Serial.println(xingstate2);

if(xingstate1 == LOW && xingstate2 == LOW){
digitalWrite(13, HIGH);
} else {
digitalWrite(13, LOW);
}
}

Now is there a way to establish a dynamic threshold

Just use a variable instead of a hard coded value. Write some function that determines what the threshold value should be and store that value in said variable.

@Delta_G: wouldn't the threshold value variable be the same as the hard coded value? For example. If 500 in one room causes the LED to turn on it might not be the same value in another room. What I am making is a little night light that will be placed into a ceramic house and placed in different rooms with different lighting in each room: florescent, incandescent, etc. And if placed in different rooms the programming should be the same.

No the variable isn't the same because you can change the value of a variable. How will your program determine what the right threshold level will be? Surely you will need something like:

PaulS:
True. That is why you normally use that sensor in combination with an LDR, to detect the amount of light in the room, and adjust the threshold accordingly.

Whatever method you decide to use to determine the threshold, then you write a function that takes whatever input from or about the room it is in and sets your threshold value from that.

@Delta_G: ok this is the sensor breakout board I am using. This board contains 2 sensors so should I use one sensor to determine room lighting and the other sensor to sensor light or dark? Pololu - QTRX-HD-02RC Reflectance Sensor Array: 2-Channel, 4mm Pitch, RC Output, Low Current

I don't know, I'm a code writer. I don't know a lot about these sensors. But if there is some method to determine a threshold then you do that. If there's not some method to determine the threshold then you're hosed.

@Delta_G: I’ve been using their Arduino library and the sample code from one of their examples is below that I’ve based some of my code off of to read the sensors:

#include <QTRSensors.h>

#define NUM_SENSORS   2                   // number of sensors used
#define TIMEOUT       2500                // waits for 2500 microseconds for sensor outputs to go low
#define EMITTER_PIN   QTR_NO_EMITTER_PIN    

// sensors 0 through 7 are connected to digital pins 3 through 10, respectively
QTRSensorsRC qtrrc((unsigned char[]) {3, 4, 5, 6, 7, 8, 9, 10},
  NUM_SENSORS, TIMEOUT, EMITTER_PIN);
unsigned int sensorValues[NUM_SENSORS];


void setup()
{
  delay(500);
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);         // turn on Arduino's LED to indicate we are in calibration mode
  for (int i = 0; i < 400; i++)   // make the calibration take about 10 seconds
  {
    qtrrc.calibrate();            // reads all sensors 10 times at 2500 us per read (i.e. ~25 ms per call)
  }   
  digitalWrite(13, LOW);          // turn off Arduino's LED to indicate we are through with calibration

  // print the calibration minimum values measured when emitters were on
  Serial.begin(9600);
  for (int i = 0; i < NUM_SENSORS; i++)
  {
    Serial.print(qtrrc.calibratedMinimumOn[i]);
    Serial.print(' ');
  }
  Serial.println();

  // print the calibration maximum values measured when emitters were on
  for (int i = 0; i < NUM_SENSORS; i++)
  {
    Serial.print(qtrrc.calibratedMaximumOn[i]);
    Serial.print(' ');
  }
  Serial.println();
  Serial.println();
  delay(1000);
}


void loop()
{
  // read calibrated sensor values and obtain a measure of the line position from 0 to 5000
  // To get raw sensor values, call:
  qtrrc.read(sensorValues);

  for (unsigned char i = 0; i < NUM_SENSORS; i++)
  {
    Serial.print(sensorValues[i]);
    Serial.print('\t');
  }
  Serial.println(); 

  delay(250);
}

Since you said you’re a code write hopefully you can point me in the right direction to accomplish what I need to do since the room lighting will be different and the sensors should calibrate for each lightening difference without havig to manipulate the code each time.