LSR value different on lcd vs serial monitor

I am using a light sensitive resistor to determine a relative light value. I average 5 readings and write the result to both the serial monitor and to a serial lcd. (sparkfun lcd-9066 serial enabled 16x2 lcd black on green 3.3v). Another section activites a relay which will turn a boiler off and on , but that is not an issue.

My problem is the value that posts the serial monitor and the lcd is the suppopsed to be same calculated value, but the values i see differ. For example, the lcd right now is 224 on the lcd but 898 on the monitor. its like they are almost the inverse of each other, as i think the highest value possible is 1023

I am using an arduino uno and nothing else is connected except a breadboard with the lsr setup and the lcd. its powered via a usb port on a pc.

I’ve attached the sketch which compiles and loads fine, and a diagram of how this is wired.
thanks for the help

all_together.ino (2.11 KB)

My problem is the value that posts the serial monitor and the lcd is the suppopsed to be same calculated value, but the values i see differ.

 int sval = analogRead(0);
  //for serial monitor review of light sensor
  Serial.println(" New Measurement");
  Serial.print(sval);

Read the sensor once and display the value on the serial monitor

 for (int i = 0; i < 5; i++)
  {
    sval = sval + analogRead(0); // sensor on analog pin 0 .
    //Writing avg lumen value to LED screen
    sval = sval / 5; // average

Then without resetting the variable to zero read it 5 times and attempt to calculate the average reading

well, you are correct, but the magnitude of the difference over such a short period of time between the average calculation and the single point in time value points to something else. i am wondering if there is a hardware problem. the value on the serial monitor seems correct. maybe the lcd is shorted out.

mtmtntop:
maybe the lcd is shorted out.

Then load a test sketch and test the LCD independently.

when i take out the averaging language, the lcd and serial read the same. what i was trying to do was take 5 readings and get an average, then show that on the lcd. that value was also used to determine if a boiler relay should be turned on or not. can someone tell me how i should rewrite the code to average 5 values? clearly i did it wrong. thanks for your help and the suggestion to test the lcd by itself.

Then please post the sketch in code tags, not as an attachment. Almost nobody is going to download 437kb of who knows what.

Here sval is declared global variable

int sval, high = 1023,low=0;

Then sval is declared again. As it is in loop it becomes a local sval. I assume it is this variable that is used in all of loop, but I don’t know for sure.

  int sval = analogRead(0);

sval gets its first value, as seen in serial monitor.

Then you are supposed to read your LSR 5 more times and add all the values together before you divide by 5. Actually it would be 6 readings. Not a huge difference.

  for (int i = 0; i < 5; i++){
    sval = sval + analogRead(0); // sensor on analog pin 0 .
   
//Writing avg lumen value to LED screen

  sval = sval / 5; // average

Wait a minute, there is no closing bracket before you do the division. Lets have a closer look at the code.

  for (int i = 0; i < 5; i++){
    sval = sval + analogRead(0); // sensor on analog pin 0 .
   
//Writing avg lumen value to LED screen

  sval = sval / 5; // average
  mySerial.write(254); // move cursor to beginning of first line
  mySerial.write(128);
  mySerial.print(" "); // clear display
  mySerial.print(" ");
  mySerial.write(254); // move cursor to beginning of first line
  mySerial.write(129);
  mySerial.write("Avg Light Level");
  mySerial.write(254);//move to second line
  mySerial.write(192);//center
  mySerial.write("      ");//clear second line
  mySerial.write(254);
  mySerial.write(199);
  mySerial.print(sval);

//Turns on boiler when light level falls below 750

 if (sval <750)            //Light level threshold is 750. Greater, turn boiler power off; less, turn boiler power on
digitalWrite(CH1, HIGH);
//digitalWrite(CH13, HIGH); //turns on LED indicating boiler power is on
else if (sval >=750); 
                          //delay(5000);
digitalWrite(CH1, LOW);
//digitalWrite(CH13, LOW);   
  // delay(5000);
}

Oh dear, closing bracket is waaaaaay down. You will start with 2 LSR readings for sval.
Divide by 5, add one more reading, divide by 5 again, add one reading, divide by… [repeated 5 times].
No wonder your LCD show a very low average.

i forgot about that code icon and thought attachment was ok.

i cleaned up the code to only do a single averaging calc and both print it to the serial monitor and display it on the lcd. After making the delay the same for both, they are in sync now. its been a steep learning curve for me but i am slowly getting this. and best of all, the code is actually doing what i need it to do!

and i did find that bracket error. thanks for the details on multiple divisions- that was the root of the problem. by doing it only once for both, it solves that problem.