Sensor + LED average values

For some reason the average value of the orange light isnt working properly when I have high values of light, but works really well with low values of light for some reason

#include <Wire.h>
#include "Adafruit_AS726x.h"

//create the object
Adafruit_AS726x ams;

//buffer to hold raw values
uint16_t sensorValues[AS726x_NUM_CHANNELS];

//buffer to hold calibrated values (not used by default in this example)
//float calibratedValues[AS726x_NUM_CHANNELS];

int numReads = 10;
int cont;
int sensor;
float sum;
float average;

int pinoRed = 9; //PINO DIGITAL UTILIZADO PELO TERMINAL VERMELHO
int pinoGreen = 10; //PINO DIGITAL UTILIZADO PELO TERMINAL VERDE
int pinoBlue = 11; //PINO DIGITAL UTILIZADO PELO TERMINAL AZUL

int val; //VARIÁVEL DO TIPO INTEIRA

void setup() {
  Serial.begin(9600);
  while(!Serial);

  pinMode(pinoRed, OUTPUT); //DEFINE O PINO COMO SAÍDA
  pinMode(pinoBlue, OUTPUT); //DEFINE O PINO COMO SAÍDA
  pinMode(pinoGreen, OUTPUT); //DEFINE O PINO COMO SAÍDA  

  analogWrite(pinoRed, 255); //PINO RECEBE O VALOR
  analogWrite(pinoGreen, 223); //PINO RECEBE O VALOR
  analogWrite(pinoBlue, 0); //PINO RECEBE O VALOR
  delay (10); //INTERVALO DE 10 MILISSEGUNDOS
  
  //initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);

  //begin and make sure we can talk to the sensor
  if(!ams.begin()){
    Serial.println("could not connect to sensor! Please check your wiring.");
    while(1);
  }

  do {
   //read the device temperature
   //uint8_t temp = ams.readTemperature();
  
   //ams.drvOn(); //uncomment this if you want to use the driver LED for readings
   ams.startMeasurement(); //begin a measurement
  
   //wait till data is available
   bool rdy = false;
   while(!rdy){
     delay(1000);
     rdy = ams.dataReady();
   }
   //ams.drvOff(); //uncomment this if you want to use the driver LED for readings

   //read the values!
   ams.readRawValues(sensorValues);
   //ams.readCalibratedValues(calibratedValues);

   for (int k = 0; k < numReads; k++){
    sensor = sensorValues[AS726x_ORANGE];
    sum += sensor;
   }

   //Serial.print("Temp: "); Serial.print(temp);
   //Serial.print(" Violet: "); Serial.print(sensorValues[AS726x_VIOLET]);
   //Serial.print(" Blue: "); Serial.print(sensorValues[AS726x_BLUE]);
   //Serial.print(" Green: "); Serial.print(sensorValues[AS726x_GREEN]);
   //Serial.print(" Yellow: "); Serial.print(sensorValues[AS726x_YELLOW]);
   Serial.print(" Orange: "); Serial.print(sensorValues[AS726x_ORANGE]);
   //Serial.print(" Red: "); Serial.print(sensorValues[AS726x_RED]);
   Serial.println();
   Serial.println();
   cont++;
  } while(cont < numReads); 

   average = sum/(numReads*10);
   Serial.print(" Orange Average: "); Serial.print(average);
}

void loop() {
}

Summing the same value n times is NOT an average.

I feel I've pointed this out to you before

You need to be more specific.

Perhaps you mean

average = sum/(numReads*numReads);

When I'm using the RGB LED in front of the sensor this happens:

But without the led the average part works well:

In both prints I'm running the same code!

That looks like signed values wrapping round - are you sure the code you posted is the code producing those results?

Yes!

Ah yes
Right there

I just change this part of the code and worked well, I guess its because of how the sensor channel was defined in the beginning (uint16_t sensorValues[AS726x_NUM_CHANNELS])

uint16_t sensor;

If the variable sensor didn't exist, you wouldn't have had the problem :wink:

The code is still dumb.

Of course, but as you can see I'm not into programming or something, for now I just need the simplest way to make the arduino run the code to use in the laboratory after this I'll take care of how I should properly programming in a smarter way