Pages: [1]   Go Down
Author Topic: analogWrite sensor LDR  (Read 425 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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  smiley
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 129
Posts: 8524
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26328
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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".
Logged

"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.

Pages: [1]   Go Up
Jump to: