outputting data to excel with data streamer

I'm trying to create a little light sensor for one of my finals along with a standard lux meter to check the accuracy and im having some issues with getting the lux meter to output data to the excel file.
I have

  • an arduino uno R3
  • a BH1750 using CLAWS library
  • a generic LDR between 50k and 100k ohms

I know the issue is code related, I think my issue is with sending a non analog value to the excel file but I dont know how to format in a way that would fix that.

luxandLDR.ino (3.83 KB)

Please post the code here. Use the symbol up and to the left in this window. Use the autoformat function in the IDE before pasting the code. That makes it possible for more helpers to read it.

LDR are quite slow in their reaction to rapid changes. Can that be a reason for the trouble?

Please read instructions for posting code before you post. Also explain what you really want. Do you want to send data to Excel, or data to a file for subsequent use in Excel?

// Program variables ----------------------------------------------------------
 #include <Wire.h>
 #include <BH1750.h>
 int Photo;
 int lizardking;
 BH1750 robertcalifornia;
 int sensorPin = A0;
 int lux;



// Serial data variables ------------------------------------------------------
//Incoming Serial Data Array
const byte kNumberOfChannelsFromExcel = 6; 

// Comma delimiter to separate consecutive data if using more than 1 sensor
const char kDelimiter = ',';    
// Interval between serial writes
const int kSerialInterval = 10000;   
// Timestamp to track serial interval
unsigned long serialPreviousTime; 

char* arr[kNumberOfChannelsFromExcel];

// SETUP ----------------------------------------------------------------------
void setup() {
  // Initialize Serial Communication
  Serial.begin(9600);  
  Wire.begin();
  robertcalifornia.begin();
}

// START OF MAIN LOOP --------------------------------------------------------- 
void loop()
{
  float lux = robertcalifornia.readLightLevel();
  // Gather and process sensor data
  processSensors();

  // Read Excel variables from serial port (Data Streamer)
  processIncomingSerial();

  // Process and send data to Excel via serial port (Data Streamer)
  processOutgoingSerial();

// Compares STR1 to STR2 returns 0 if true.
//   if ( strcmp ("Apple", arr[0]) == 0){ 
//       Serial.println("working");
//   }
}

// SENSOR INPUT CODE-----------------------------------------------------------
void processSensors() 
{
  // Read sensor pin and store to a variable
  Photo = analogRead( sensorPin );
  lizardking = analogRead( lux );
  
  // Add any additional raw data analysis below (e.g. unit conversions)
  
}

// Add any specialized methods and processing code below


// OUTGOING SERIAL DATA PROCESSING CODE----------------------------------------
void sendDataToSerial()
{
  // Send data out separated by a comma (kDelimiter)
  // Repeat next 2 lines of code for each variable sent:

  Serial.print(Photo);
  Serial.print(kDelimiter);
  Serial.print(lizardking);
  Serial.print(kDelimiter);
  
  Serial.println(); // Add final line ending character only once
}

//-----------------------------------------------------------------------------
// DO NOT EDIT ANYTHING BELOW THIS LINE
//-----------------------------------------------------------------------------

// OUTGOING SERIAL DATA PROCESSING CODE----------------------------------------
void processOutgoingSerial()
{
   // Enter into this only when serial interval has elapsed
  if((millis() - serialPreviousTime) > kSerialInterval) 
  {
    // Reset serial interval timestamp
    serialPreviousTime = millis(); 
    sendDataToSerial(); 
  }
}

// INCOMING SERIAL DATA PROCESSING CODE----------------------------------------
void processIncomingSerial()
{
  if(Serial.available()){
    parseData(GetSerialData());
  }
}

// Gathers bytes from serial port to build inputString
char* GetSerialData()
{
  static char inputString[64]; // Create a char array to store incoming data
  memset(inputString, 0, sizeof(inputString)); // Clear the memory from a pervious reading
  while (Serial.available()){
    Serial.readBytesUntil('\n', inputString, 64); //Read every byte in Serial buffer until line end or 64 bytes
  }
  return inputString;
}

// Seperate the data at each delimeter
void parseData(char data[])
{
    char *token = strtok(data, ","); // Find the first delimeter and return the token before it
    int index = 0; // Index to track storage in the array
    while (token != NULL){ // Char* strings terminate w/ a Null character. We'll keep running the command until we hit it
      arr[index] = token; // Assign the token to an array
      token = strtok(NULL, ","); // Conintue to the next delimeter
      index++; // incremenet index to store next value
    }
}
// Program variables ----------------------------------------------------------
#include <Wire.h>
#include <BH1750.h>
int Photo;
int lizardking;
//BH1750 robertcalifornia;
int sensorPin = A0;
int lux;



// Serial data variables ------------------------------------------------------
//Incoming Serial Data Array
const byte kNumberOfChannelsFromExcel = 6;

// Comma delimiter to separate consecutive data if using more than 1 sensor
const char kDelimiter = ',';
// Interval between serial writes
const int kSerialInterval = 10000;
// Timestamp to track serial interval
unsigned long serialPreviousTime;

char* arr[kNumberOfChannelsFromExcel];

// SETUP ----------------------------------------------------------------------
void setup() {
  // Initialize Serial Communication
  Serial.begin(9600);
  Wire.begin();
  //robertcalifornia.begin();
}

// START OF MAIN LOOP ---------------------------------------------------------
void loop()
{
  //float lux = robertcalifornia.readLightLevel();
  // Gather and process sensor data
  processSensors();

  // Read Excel variables from serial port (Data Streamer)
  processIncomingSerial();

  // Process and send data to Excel via serial port (Data Streamer)
  processOutgoingSerial();

  // Compares STR1 to STR2 returns 0 if true.
  //   if ( strcmp ("Apple", arr[0]) == 0){
  //       Serial.println("working");
  //   }
}

// SENSOR INPUT CODE-----------------------------------------------------------
void processSensors()
{
  // Read sensor pin and store to a variable
  Photo = analogRead( sensorPin );
  //lizardking = analogRead( lux );

  // Add any additional raw data analysis below (e.g. unit conversions)

}

// Add any specialized methods and processing code below


// OUTGOING SERIAL DATA PROCESSING CODE----------------------------------------
void sendDataToSerial()
{
  // Send data out separated by a comma (kDelimiter)
  // Repeat next 2 lines of code for each variable sent:

  Serial.print(Photo);
  Serial.print(kDelimiter);
  //Serial.print(lizardking);
  //Serial.print(kDelimiter);

  Serial.println(); // Add final line ending character only once
}

//-----------------------------------------------------------------------------
// DO NOT EDIT ANYTHING BELOW THIS LINE
//-----------------------------------------------------------------------------

// OUTGOING SERIAL DATA PROCESSING CODE----------------------------------------
void processOutgoingSerial()
{
  // Enter into this only when serial interval has elapsed
  if ((millis() - serialPreviousTime) > kSerialInterval)
  {
    // Reset serial interval timestamp
    serialPreviousTime = millis();
    sendDataToSerial();
  }
}

// INCOMING SERIAL DATA PROCESSING CODE----------------------------------------
void processIncomingSerial()
{
  if (Serial.available()) {
    parseData(GetSerialData());
  }
}

// Gathers bytes from serial port to build inputString
char* GetSerialData()
{
  static char inputString[64]; // Create a char array to store incoming data
  memset(inputString, 0, sizeof(inputString)); // Clear the memory from a pervious reading
  while (Serial.available()) {
    Serial.readBytesUntil('\n', inputString, 64); //Read every byte in Serial buffer until line end or 64 bytes
  }
  return inputString;
}

// Seperate the data at each delimeter
void parseData(char data[])
{
  char *token = strtok(data, ","); // Find the first delimeter and return the token before it
  int index = 0; // Index to track storage in the array
  while (token != NULL) { // Char* strings terminate w/ a Null character. We'll keep running the command until we hit it
    arr[index] = token; // Assign the token to an array
    token = strtok(NULL, ","); // Conintue to the next delimeter
    index++; // incremenet index to store next value
  }
}

The LDR is working fine for the time scale Im using. Im trying to get the data streamer aspect of excel to give me a file with time, analog vale from the LDR, and the correct lux value from the 1750. Currently i can get the LDR value but im having trouble getting the lux value in a format that excel can write, if i try to write it as an analog value it just returns a constant value even under different light conditions and if I try to do it as a floating value excel cant "see" it . I made the various attempts into comments in the code for when i was trying various combinations its the lines with robertcaliforna and lizardking

I managed to figure it out it ended up not being a code issue and instead being a connection issue with my breadboard even though i had tested it and and had it work before changing the code to the excel version.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.