I need help With returning my function to a int

bool calibrationDone = false;
int lightConfig1;
int lightConfig2;
int lightConfig3;
int calibrationCounter;
int calibratedAverage;
int calibrationSum;
  
 void setup() {
    pinMode(A0, INPUT);
    pinMode(A1, INPUT);
    pinMode(A2, INPUT);
    Serial.begin(9600);
  }

int configureLight(int photoresistorPort,int cycleCount)
{
  for (calibrationCounter = 0; calibrationCounter < cycleCount; calibrationCounter ++)
  {
    calibrationSum = calibrationSum + analogRead(photoresistorPort);
    delay(500);
  }
  calibratedAverage = calibrationSum / cycleCount;
  Serial.print("calibratedAverage ");
  Serial.print(photoresistorPort);
  Serial.print(":");
  Serial.println(calibratedAverage);
  return calibratedAverage;
}

void loop() {
  if (calibrationDone == false){
  int lightConfig1 = configureLight(A0,5);
  Serial.print("inside Loop");
  Serial.println(lightConfig1);
  //int lightConfig2 = configureLight(A1,5);
  //int lightConfig3 = configureLight(A2,5);
  calibrationDone  = true;
 }
 Serial.println(lightConfig1);
//int phtoResinput1 = analogRead(A0);
//int phtoResinput2 = analogRead(A1);
//int phtoResinput3 = analogRead(A2);

//long phtoMapped1 = map(phtoResinput1,1/2,lightConfig1,0,100);
}

I'm saving the function to a variable and as soon as it leaves the loop it sets itself to zero.

Scope issue.

can you elaborate?

You had two variables with the same name.
I fixed that.

thanks so much

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