Knight rider with Photocell controller

hello im brand new to arduino and ive been tasked with creating a code that makes 6 LEDs run a knight rider circuit with the speed depending on the value of the photocell. it was working fine before i put in the photocell but now it just keeps all lights on.
heres my code

const int sensorMin = 50;
const int sensorMax = 200;
int photocellPin = A0;
int timer = analogRead(photocellPin); 



void setup() 

{ 

  for (int thisPin = 2; thisPin < 8; thisPin++) 

  { 

    pinMode(thisPin, OUTPUT); 

  } 

} 

void loop() 

{ for (int thisPin = 2; thisPin < 8; thisPin++) 

  { 

    digitalWrite(thisPin, HIGH); 

    delay(timer); 

    digitalWrite(thisPin, LOW); 

 

  } 

for (int thisPin = 7; thisPin < 1; thisPin--) 

  { 

    digitalWrite(thisPin, HIGH); 

    delay(timer); 

    digitalWrite(thisPin, LOW); 

    } 

} 

The right hand side of that expression needs to be in a function, like setup() or loop().

This is the default, above setup():
int timer = 0;

I'm not spotting that error...

But, what analog readings are you getting? (Use the serial monitor as in the Analog Read Serial Example.)

If the code is otherwise working, but the delay is zero or small, the LEDs will flash too quickly to perceive as "moving" and they will all appear on and dim.

You're only reading the photo cell once before setup() so the delay will never change once you're in the main loop. Is that what you want?

And you're not using your min & max variables.

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