Issue with a Calculation in Arduino

Hello,
First off, I’d like to apologize in advance if this issue ends up being some small syntax error on my part. I’m new to programming with Arduino and can’t figure out why my program isn’t working.

The issue I am experiencing is that a calculation I have set up in a program that reads data from a voltage divider does not output the value I am expecting. I have set up formula to calculate the resistance of a photocell based on the analog reading of a voltage divider. The problem is that the value of resistance the formula calculates is different than the value I get from working out the calculation by hand.

Here is the entirety of my program

#include <stdio.h>

/*
Practice Colorimeter v. 3
ZPfaffen
6_10_2016
 */

//Gathering Data Variables
const int LIGHT_PIN = A0;
const float VCC = 4.98;
const float R_DIV = 4660.0;
String color = "";
char intensity[15] = "Null";

//Input Pin Variables
 int message = 0;
 int led1pin = 9;
 int led2pin = 10;
 int led3pin = 11;
 

void setup() {
  Serial.begin(9600);
  pinMode(led1pin, OUTPUT);
  pinMode(led2pin, OUTPUT);
  pinMode(led3pin, OUTPUT);
  pinMode(LIGHT_PIN, INPUT);
 
}

void loop() 
  {
  
  if(Serial.available() > 0)
      {
        message = Serial.read();
        ledTurnOn();

      }
 
  }
     
//Reading the Resistance Functions
float readResistance()
{
        /*
        int lightADC = analogRead(LIGHT_PIN);
        float lightV = lightADC * VCC / 1023.0;
        */
        float lightV = 4.66;
        return lightV;
          
}


String getAvgVoltage(int avgAmount)
{
    float V = 0;
    char b[20];
    for (int kk = 0; kk < avgAmount; kk++) 
        {
          V = V + readResistance();
          delay(5);
        }
    V = V / avgAmount;
    double lightR = R_DIV * (VCC / V - 1.00);
    // I printed lightR to the Serial port here
    // and found that it was 320.0. It should be
    // around 6340 according to my calculations.
    int d3 = lightR;
    float f2 = lightR - d3;
    int d4 = floor((f2*100) + 0.5);
    int d1 = V;           
    float f1 = V - d1;
    int d2 = floor((f1 * 100) + 0.5);     //Have to call the floor command to get the right value after the decimal
    sprintf( b, "%d,%d.%d,%d.%d", avgAmount, d1, d2, d3, d4 );
    return b;  
}




void outputResistance()
{
  Serial.print((float)millis()/1000.0);
  Serial.print(",");
  Serial.print(analogRead(LIGHT_PIN));
  Serial.print(",");
  Serial.print(color);
  Serial.print(",");
  Serial.print(intensity);
  Serial.print(",");
  Serial.print(getAvgVoltage(5));
  Serial.print("\n");
 
}


//Control of LED Functions
void ledTurnOn()
{  

      if (message == 'A')
      {
        color = "Red";
        outputResistance();
        digitalWrite(led3pin, HIGH);
        digitalWrite(led2pin, LOW);
        digitalWrite(led1pin, LOW);
        delay(50);
        outputResistance();
        
      }

     if (message == 'B')
       {
        color = "Blue";
        outputResistance();
        digitalWrite(led2pin, HIGH);
        digitalWrite(led1pin, LOW);
        digitalWrite(led3pin, LOW);
        delay(200);
        outputResistance();
       }
       
       if (message == 'C')
       {
        color = "Green";
        outputResistance();
        digitalWrite(led1pin, HIGH);
        digitalWrite(led2pin, LOW);
        digitalWrite(led3pin, LOW);
        delay(200);
        outputResistance();
       }
       
       if (message == 'D')
       {
        color = "Blue+Green";
        outputResistance();
        setColor(0,150, 80);
        delay(200);
        outputResistance();
       }

      if (message == 'F')
      {
        outputResistance();
        colorCycle();
        delay(200);
        outputResistance();
      }
      
      if (message == 'E') 
      {
        digitalWrite(led3pin, LOW);
        digitalWrite(led2pin, LOW);
        digitalWrite(led1pin, LOW);
      }
}

void setColor(int red, int green, int blue)
{
  
  #ifdef COMMON_ANODE
    red = 255 - red;
    green = 255 - green;
    blue = 255 - blue;
  #endif
  analogWrite(led1pin, red);
  analogWrite(led2pin, green);
  analogWrite(led3pin, blue);
  sprintf( intensity, "%d,%d,%d", red, green, blue);

}

void colorCycle()
{
  for(int red = 0; red < 255; red++)
  {
    analogWrite(led3pin, red);
    outputResistance();
  }
}

The specific pieces relevant are in the code tags below.

/*
Practice Colorimeter v. 3
ZPfaffen
6_10_2016
 */

//Gathering Data Variables
const int LIGHT_PIN = A0;
const float VCC = 4.98;
const float R_DIV = 4660.0;
String color = "";
char intensity[15] = "Null";

//Input Pin Variables
 int message = 0;
 int led1pin = 9;
 int led2pin = 10;
 int led3pin = 11;
 

void setup() {
  Serial.begin(9600);
  pinMode(led1pin, OUTPUT);
  pinMode(led2pin, OUTPUT);
  pinMode(led3pin, OUTPUT);
  pinMode(LIGHT_PIN, INPUT);
 
}

void loop() 
  {
  
  if(Serial.available() > 0)
      {
        message = Serial.read();
        ledTurnOn();

      }
 
  }
     
//Reading the Resistance Functions
float readResistance()
{
        /*
        int lightADC = analogRead(LIGHT_PIN);
        float lightV = lightADC * VCC / 1023.0;
        */
        float lightV = 4.66;
        return lightV;
          
}


String getAvgVoltage(int avgAmount)
{
    float V = 0;
    char b[20];
    for (int kk = 0; kk < avgAmount; kk++) 
        {
          V = V + readResistance();
          delay(5);
        }
    V = V / avgAmount;
    double lightR = R_DIV * (VCC / V - 1.00);
    // I printed lightR to the Serial port here
    // and found that it was 320.0. It should be
    // around 6340 according to my calculations.
    int d3 = lightR;
    float f2 = lightR - d3;
    int d4 = floor((f2*100) + 0.5);
    int d1 = V;           
    float f1 = V - d1;
    int d2 = floor((f1 * 100) + 0.5);     //Have to call the floor command to get the right value after the decimal
    sprintf( b, "%d,%d.%d,%d.%d", avgAmount, d1, d2, d3, d4 );
    return b;  
}




void outputResistance()
{
  Serial.print((float)millis()/1000.0);
  Serial.print(",");
  Serial.print(analogRead(LIGHT_PIN));
  Serial.print(",");
  Serial.print(color);
  Serial.print(",");
  Serial.print(intensity);
  Serial.print(",");
  Serial.print(getAvgVoltage(5));
  Serial.print("\n");
 
}

In the readResistance function, I have hard coded in a piece of voltage data for the purposes of testing this code. I have been able to read the analog pin fine and print out that value. In addition, I can print out the correct voltage from this analog value. The problem I’m experiencing seems to lie in the getAvgVoltage function, specifically the piece where I try to assign a value to the “lightR” double.

double lightR = R_DIV * (VCC / V - 1.00);

I think this is the line that is not working. I get no compile errors and everything with the code works as I expect except for the value printed for this data piece. I mention in a comment that I tried to print the value of lightR by adding in a Serial.println statement just after the calculation to see what the value is at that point. The print to the serial monitor is 320.0. The value I get when plugging in the same values of variables into a calculator by hand is 6340.65 (and some more decimal places). I’m wondering if my issue lies in a data type somewhere? Thanks to every for their help. I apologize if my code is very messy; I’m quite new to programming and am on this project short term so I’m just trying to get things to work.

Can you post a drawing of your voltage divider with Vcc and value of resistance as it is wired to arduino?

If you are unsure about formula type conversion - give it a try by declaring everything as double

Sure! I can post a picture.

I'm almost exactly following the spark fun tutorial for a photocell. The only difference is that my fixed resistor is 100K. I will try declaring all my variables as double and see if that helps.

Hey All,
I figured out the issue. I hadn't placed in enough parentheses. Thanks for your help!