Light Measurement Graphic Display

The Serial Monitor screencapture below shows an example of a graphical display of the ndShade variable.  The asterisk will be in the center of the -0.5 to +0.5 scale if the light or shade is the same over both sensors.  If the shade is darker over the BOE Shield-Bot’s right sensor, the asterisk will position to the right in the scale.  If it’s darker over the left, the asterisk will position toward the left.  A larger shade/light contrast (like darker shade over one of the sensors) will result in the asterisk positioning further from the center.

Serial Monitor output of the sketch LightSensorDisplay

  • Load the LightSensorDisplay sketch into the Arduino.
  • Try casting different levels of shade over each light sensor, and watch how the asterisk in the Serial Monitor responds.  Remember that if you cast equal shade over both sensors, the asterisk should still be in the middle; it only indicates which sensor sees more shade if there’s a difference between them.
/*
 * Robotics with the BOE Shield - LightSensorDisplay
 * Displays a scrolling graph of ndShade.  The asterisk positions ranges
 * from 0 to 40 with 20 (middle of the display) indicating same light on
 * both sides.
 */

void setup()                                 // Built-in initialization block
{
  tone(4, 3000, 1000);                       // Play tone for 1 second
  delay(1000);                               // Delay to finish tone

  Serial.begin(9600);                        // Set data rate to 9600 bps
}  
 
void loop()                                  // Main loop auto-repeats
{
  float tLeft = float(rcTime(8));            // Get left light & make float
  float tRight = float(rcTime(6));           // Get right light & make float
 
  float ndShade;                             // Normalized differential shade
  ndShade = tRight / (tLeft+tRight) - 0.5;   // Calculate it and subtract 0.5
 
  for(int i = 0; i<(ndShade * 40) + 20; i++) // Place asterisk in 0 to 40
  {
    Serial.print(' ');                       // Pad (ndShade * 40) + 20 spaces
  }
  Serial.println('*');                       // Print asterisk and newline

  delay(100);                                // 0.1 second delay
}

long rcTime(int pin)                         // rcTime measures decay at pin
{
  pinMode(pin, OUTPUT);                      // Charge capacitor
  digitalWrite(pin, HIGH);                   // ..by setting pin ouput-high
  delay(5);                                  // ..for 5 ms
  pinMode(pin, INPUT);                       // Set pin to input
  digitalWrite(pin, LOW);                    // ..with no pullup
  long time  = micros();                     // Mark the time
  while(digitalRead(pin));                   // Wait for voltage < threshold
  time = micros() - time;                    // Calculate decay time
  return time;                               // Returns decay time
}

How LightSensorDisplay Works

The loop function starts by taking the two rcTime measurements for the left and right light sensors, and stores them in tLeft and tRight.

void loop()                                  // Main loop auto-repeats
{
  float tLeft = float(rcTime(8));            // Get left light & make float
  float tRight = float(rcTime(6));           // Get right light & make float

After declaring ndShade as a floating-point variable, tLeft and tRight are used in an expression to get that zero-justified normalized differential measurement. The result will be between –0.5 and +0.5, and gets stored in ndShade.

  float ndShade;                             // Normalized differential shade
  ndShade = tRight / (tLeft+tRight) - 0.5;   // Calculate it and subtract 0.5

Next, this for loop places the cursor in the right place for printing an asterisk.  Take a close look at the for loop’s condition.  It takes ndShade and multiples it by 40.  It also has to add 20 to the value because if ndShade is –0.5, we want that to print with zero leading spaces.  So (–0.5 × 40) + 20 = 0.  Now, if ndShade is 0, we want it to print 20 spaces over:  (0 × 40) + 20 = 20.  If it’s +0.5 we want it to print 40 spaces over:  (0.5 × 40) + 20 = 40.  Of course, if it’s something in between, like 0.25, we have (0.25 × 40) + 20 = 30.  So, it’ll print half way between center and far right.

  for(int i = 0; i<(ndShade * 40) + 20; i++) // Place asterisk in 0 to 40
  {
    Serial.print(' ');                       // Pad (ndShade * 40) + 20 spaces
  }

After printing the spaces, a single asterisk prints on the line.  Recall that println prints and also adds a newline so that the next time through the loop, the asterisk will display on the next line.

  Serial.println('*');                       // Print asterisk and newline

  delay(100);                                // 0.1 second delay
}