analogWrite sensor LDR

hello everyone.
I wanted to ask about the value of "4" on the analogWrite (ledPin, val / 4). what is the function of a "4" and the explanation.
complete the following code:

int ledPin = 9;      // LED connected to digital pin 9
int analogPin = 3;   // potentiometer connected to analog pin 3
int val = 0;         // variable to store the read value

void setup()
{
  pinMode(ledPin, OUTPUT);   // sets the pin as output
}

void loop()
{
  val = analogRead(analogPin);   
  analogWrite(ledPin, val / 4);  
}

The code I will use as a light detection sensor with LDR and Led as the indicator, if the LDR is exposed to light and the LED will be ON if the LDR is not exposed to light then the LED will be OFF.
Thanx 4all :slight_smile:

analogRead() returns a value from 0-1023, analogWrite() expects a value from 0-255, so you div the read value by 4.


Rob

The code I will use as a light detection sensor with LDR and Led as the indicator, if the LDR is exposed to light and the LED will be ON if the LDR is not exposed to light then the LED will be OFF.

You'll probably want some threshold ( a value in the range 0..1023) defined to decided what is, and what is not, "exposed to light".