Go Down

Topic: analogWrite sensor LDR (Read 455 times) previous topic - next topic

joniqonex

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:
Code: [Select]
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  :)

Graynomad

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

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

AWOL

Quote
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".
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up