How to use a integer as a constant integer

I am using the following code to get analog data from my breadboard ranging from 0-1000 based on room light levels.

int photoRPin = 0; 
int lightLevel;

void setup() {
 Serial.begin(9600);
 lightLevel=analogRead(photoRPin);
}

void loop(){
 lightLevel=analogRead(photoRPin);
 Serial.println(lightLevel);
 //slow down the transmission for effective Serial communication.
 delay(50);
}

My serial monitor shows a continuous list of data that’s never-ending and it changes. I need to operate a motor with this data. I need the motor to move in one direction if the light level is bigger then 600, and in another direction, if the light level is smaller then 600.

The problem is that I cannot use…

if lightLevel < 600 
....
....
else
....
....

Because light level needs to be a constant integer to be put after the if/else function according to the error code I’m getting. lightLevel is a forever changing variable though. Is there a easy way to store lightLevel as a “const int” every 100ms or so in oder to use it in my if/else function?

Thanks!!

Post the real and complete code that will not compile. Post the complete error message(s). You cannot change the value of a constant.

You can use a variable there, assuming you put in the parentheses and curly braces:

  int lightLevel = analogRead(LDRPin);
  if (lightLevel < 600)
  {
    ....
    ....
   }
  else
  {
    ....
    ....
  }

Your statements (like ‘if’ and ‘else’) have to be inside a function.

tbanari:
My serial monitor shows a continuous list of data that’s never-ending and it changes.

Then fix it, by only printing if the level has changed.

int lightLevel, prevLevel;

void loop() {
  lightLevel = analogRead(photoRPin); // read
  if (lightLevel != prevLevel) { // changed?
    Serial.println(photoRPin); // print
    prevLevel = lightLevel; // remember
  }

Changing that delay from 50 to e.g. 250 might also help a bit.
Leo…