LED brightness Change as light change + control with a push button

Hi All,
I’m very new to programming and was trying out a small project but I cannot figure out the code.
I need to have the LED change brightness decrease as ambient light decrease and vice versa. I also need to have a button the can turn off/on the whole system.

As for the code, this is what I have so far:

int light;//to store the current value of light 
int led = 5; // to store led pin config. 
int fade = 50; //pwm value to be deducted 
int photoresistor = A0; //define analog input for the photoresistor
int x; //button status 

  void setup(){  
    pinMode(led, OUTPUT);  //set pin 5 to be an output to the LED (pin 9 is connected to LED)
    pinMode(photoresistor, INPUT); //set pin A0 to be an input 
    Serial.begin(9600); 
  }

void loop() {
  x = digitalRead(9); //button status
  int currentlight = analogRead(photoresistor); //get the current light measurement
  int brightness = fade + (photoresistor/4); 
  delay (500); 
  analogWrite(led, brightness); 

  if(x==1) { 
    analogWrite(5,0); 
  }
}

I know its wrong and missing but I have spent over 20 hours on this and I’m out of ideas. Any help would be highly appreciated.

I was able to make it work by setting ranges (e.g. if light<300, turn on) but I want it to automatically adjust to the reading from the Photoresistor.

I’ve attached my circuit for reference

if(x==1) 
  {
    analogWrite(5,0);  //turn off the LED if the input is HIGH
  }
else
  {
  analogWrite(led, brightness);  //otherwise set the LED brightness
  }

Thanks for the reply. I've just tried adding that code into my code (and amending as necessary) but still didn't work. Do you mind explaining what is missing?

Please post your code as it is now

 int light;//to store the current value of light
int led = 5; // to store led pin config.
int fade = 50; //pwm value to be deducted
int photoresistor = A0; //define analog input for the photoresistor
int x; //button status

  void setup(){ 
    pinMode(led, OUTPUT);  //set pin 5 to be an output to the LED (pin 9 is connected to LED)
    pinMode(photoresistor, INPUT); //set pin A0 to be an input
    Serial.begin(9600);
  }

void loop() {
  x = digitalRead(3);
  int currentlight = analogRead(photoresistor); //get the current light measurement
  int brightness = (photoresistor/4);
  delay (500);
  
 if(x==1)
  {
    analogWrite(5,0);  //turn off the LED if the input is HIGH
  }
else
  {
  analogWrite(led, brightness);  //otherwise set the LED brightness
  }
  }
int photoresistor = A0; //define analog input for the photoresistor
[...]
  int brightness = (photoresistor / 4);

do you see the problem now?

int brightness = (photoresistor/4);

Just out curiosity what is it you were trying to achieve here.

Proietti:

int brightness = (photoresistor/4);

Just out curiosity what is it you were trying to achieve here.

analogRead() has 10 bit resolution, analogWrite has 8 bit resolution. So you can divide by four to scale it.

The OP has probably already figured out the huge mistake of using variable 'photoresistor' for that, though... :slight_smile:

Then a map function would have been more appropriate

Proietti:
Then a map function would have been more appropriate

I disagree. First of all, it's horribly inefficient. map() on the Arduino promotes everything to a 32 bit long, and does two subtractions that aren't necessary if the scalars begin at zero (which they do in this case). So there is both a processing time and memory penalty. map() doesn't exist outside Arduino, which makes it non-portable.

The only thing I don't like about the /4 method in this case, is that it was not documented (commented). You can do it even more efficiently (and arguably more clearly) with a shift operation, >>2. The fact that you had to ask about it, proves my point.