0

I connected an Adafruit color sensor to my Arduino and am having it make calculations. However, when the calculations are done and sent to the serial monitor, it says "NDVI: " without printing the value. The code is attached below. Thanks!

Code:

/* TSL2591 Digital Light Sensor */
/* Dynamic Range: 600M:1 */
/* Maximum Lux: 88K */
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_TSL2591.h"
#define IR850 5
#define IR940 6
#define BLUE 9
#define GREEN 10
#define RED 11
// Example for demonstrating the TSL2591 library - public domain!
// connect SCL to I2C Clock
// connect SDA to I2C Data
// connect Vin to 3.3-5V DC
// connect GROUND to common ground
Adafruit_TSL2591 tsl = Adafruit_TSL2591(2591); // pass in a number for the sensor identifier (for your use later)
/**************************************************************************/
/*
 Displays some basic information on this sensor from the unified
 sensor API sensor_t type (see Adafruit_Sensor for more information)
*/
/**************************************************************************/
void displaySensorDetails(void)
{
 sensor_t sensor;
 tsl.getSensor(&sensor);
 Serial.println(F("------------------------------------"));
 Serial.print (F("Sensor: ")); Serial.println(sensor.name);
 Serial.print (F("Driver Ver: ")); Serial.println(sensor.version);
 Serial.print (F("Unique ID: ")); Serial.println(sensor.sensor_id);
 Serial.print (F("Max Value: ")); Serial.print(sensor.max_value); Serial.println(F(" lux"));
 Serial.print (F("Min Value: ")); Serial.print(sensor.min_value); Serial.println(F(" lux"));
 Serial.print (F("Resolution: ")); Serial.print(sensor.resolution, 4); Serial.println(F(" lux")); 
 Serial.println(F("------------------------------------"));
 Serial.println(F(""));
 delay(500);
}
/**************************************************************************/
/*
 Configures the gain and integration time for the TSL2591
*/
/**************************************************************************/
void configureSensor(void)
{
 // You can change the gain on the fly, to adapt to brighter/dimmer light situations
 //tsl.setGain(TSL2591_GAIN_LOW); // 1x gain (bright light)
 //tsl.setGain(TSL2591_GAIN_MED); // 25x gain
 tsl.setGain(TSL2591_GAIN_HIGH); // 428x gain
 
 // Changing the integration time gives you a longer time over which to sense light
 // longer timelines are slower, but are good in very low light situtations!
 //tsl.setTiming(TSL2591_INTEGRATIONTIME_100MS); // shortest integration time (bright light)
 // tsl.setTiming(TSL2591_INTEGRATIONTIME_200MS);
 // tsl.setTiming(TSL2591_INTEGRATIONTIME_300MS);
 // tsl.setTiming(TSL2591_INTEGRATIONTIME_400MS);
 // tsl.setTiming(TSL2591_INTEGRATIONTIME_500MS);
 tsl.setTiming(TSL2591_INTEGRATIONTIME_600MS); // longest integration time (dim light)
 /* Display the gain and integration time for reference sake */ 
 Serial.println(F("------------------------------------"));
 Serial.print (F("Gain: "));
 tsl2591Gain_t gain = tsl.getGain();
 switch(gain)
 {
 case TSL2591_GAIN_LOW:
 Serial.println(F("1x (Low)"));
 break;
 case TSL2591_GAIN_MED:
 Serial.println(F("25x (Medium)"));
 break;
 case TSL2591_GAIN_HIGH:
 Serial.println(F("428x (High)"));
 break;
 case TSL2591_GAIN_MAX:
 Serial.println(F("9876x (Max)"));
 break;
 }
 Serial.print (F("Timing: "));
 Serial.print((tsl.getTiming() + 1) * 100, DEC); 
 Serial.println(F(" ms"));
 Serial.println(F("------------------------------------"));
 Serial.println(F(""));
}
/**************************************************************************/
/*
 Program entry point for the Arduino sketch
*/
/**************************************************************************/
void setup(void) 
{
 pinMode(IR850, OUTPUT);
 pinMode(IR940, OUTPUT);
 pinMode(RED, OUTPUT);
 pinMode(GREEN, OUTPUT);
 pinMode(BLUE, OUTPUT);
 digitalWrite(IR850, LOW);
 digitalWrite(IR940, LOW);
 digitalWrite(RED, LOW);
 digitalWrite(GREEN, LOW);
 digitalWrite(BLUE, LOW);
 Serial.begin(9600);
 Serial.println(F("Starting Adafruit TSL2591 Test!"));
 
 if (tsl.begin()) 
 {
 Serial.println(F("Found a TSL2591 sensor"));
 } 
 else 
 {
 Serial.println(F("No sensor found ... check your wiring?"));
 while (1);
 }
 
 /* Display some basic information on this sensor */
 displaySensorDetails();
 
 /* Configure the sensor */
 configureSensor();
 
 // Now we're ready to get readings ... move on to loop()!
}
/**************************************************************************/
/*
 Shows how to perform a basic read on visible, full spectrum or
 infrared light (returns raw 16-bit ADC values)
*/
/**************************************************************************/
void simpleRead(void)
{
 // Simple data read example. Just read the infrared, fullspecrtrum diode 
 // or 'visible' (difference between the two) channels.
 // This can take 100-600 milliseconds! Uncomment whichever of the following you want to read
 uint16_t x = tsl.getLuminosity(TSL2591_VISIBLE);
 //uint16_t x = tsl.getLuminosity(TSL2591_FULLSPECTRUM);
 //uint16_t x = tsl.getLuminosity(TSL2591_INFRARED);
 Serial.print(F("[ ")); Serial.print(millis()); Serial.print(F(" ms ] "));
 Serial.print(F("Luminosity: "));
 Serial.println(x, DEC);
}
/**************************************************************************/
/*
 Show how to read IR and Full Spectrum at once and convert to lux
*/
/**************************************************************************/
uint16_t ir, full;
void advancedRead(void)
{
 // More advanced data read example. Read 32 bits with top 16 bits IR, bottom 16 bits full spectrum
 // That way you can do whatever math and comparisons you want!
 uint32_t lum = tsl.getFullLuminosity();
 uint16_t ir, full;
 ir = lum >> 16;
 full = lum & 0xFFFF;
 Serial.print(F("[ ")); Serial.print(millis()); Serial.print(F(" ms ] "));
 Serial.print(F("IR: ")); Serial.print(ir); Serial.print(F(" "));
 Serial.print(F("Full: ")); Serial.print(full); Serial.print(F(" "));
 Serial.print(F("Visible: ")); Serial.print(full - ir); Serial.print(F(" "));
 Serial.print(F("Lux: ")); Serial.println(tsl.calculateLux(full, ir), 6);
}
/**************************************************************************/
/*
 Performs a read using the Adafruit Unified Sensor API.
*/
/**************************************************************************/
void unifiedSensorAPIRead(void)
{
 /* Get a new sensor event */ 
 sensors_event_t event;
 tsl.getEvent(&event);
 
 /* Display the results (light is measured in lux) */
 Serial.print(F("[ ")); Serial.print(event.timestamp); Serial.print(F(" ms ] "));
 if ((event.light == 0) |
 (event.light > 4294966000.0) | 
 (event.light <-4294966000.0))
 {
 /* If event.light = 0 lux the sensor is probably saturated */
 /* and no reliable data could be generated! */
 /* if event.light is +/- 4294967040 there was a float over/underflow */
 Serial.println(F("Invalid data (adjust gain or timing)"));
 }
 else
 {
 Serial.print(event.light); Serial.println(F(" lux"));
 }
}
int lightvalue;
int NDVIred;
int NDVIinfrared;
int NDVI;
boolean (hasrun) = false;
/**************************************************************************/
/*
 Arduino loop function, called once 'setup' is complete (your own code
 should go here)
*/
/**************************************************************************/
void loop(void)
{ 
 lightvalue = 255;
 //simpleRead(); 
 //advancedRead();
 //unifiedSensorAPIRead();
 if(hasrun==false){
 analogWrite(RED, lightvalue);
 advancedRead();
 NDVIred = full;
 delay(5000);
 analogWrite(IR940,lightvalue);
 advancedRead();
 NDVIinfrared=full;
 NDVI=(NDVIinfrared-NDVIred)/(NDVIinfrared+NDVIred);
 Serial.print("NDVI: "); Serial.write(NDVIred);
 delay(1000);
 exit(0);
 }
}
sempaiscuba
1,0429 gold badges21 silver badges32 bronze badges
asked Jul 3, 2023 at 14:45

1 Answer 1

1

You wrote:

Serial.print("NDVI: "); Serial.write(NDVIred);

You are misusing here the Serial.write() method. This method is meant for sending raw bytes through the serial port. Since you are printing text, you probably want the numeric value NDVIred to be printed out as text, as a decimal number.

For printing text, use Serial.print() and Serial.println():

Serial.print("NDVI: "); Serial.println(NDVIred);
answered Jul 3, 2023 at 14:55
3
  • I did that and it now prints "NDVI: 0". Is there a problem with the variables? Commented Jul 3, 2023 at 14:57
  • @Chris: No, no problem with the variable. You are just seeing its value, which happens to be zero. Commented Jul 3, 2023 at 15:44
  • 2
    No, the code does what it's supposed to do. There is however a problem with the scope of the variable named full. It is defined as a global variable and again as a local variable inside of advancedRead(). Which explains why NVDIred is assigned a value of zero (which is the value of the still unchanged global full variable). Remove the definition of full from advancedRead() to fix this. Commented Jul 3, 2023 at 15:44

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.