const int ledPin = 13; // LED connected to digital pin 13
const int sensorPin = 0; // connect sensor to analog input 0
// the next two lines set the min and max delay between blinks
const int minDuration = 100; // minimum wait between blinks
const int maxDuration = 1000; // maximum wait between blinks
void setup()
{
pinMode(ledPin, OUTPUT); // enable output on the led pin
Serial.begin(9600); // initialize Serial
}
void loop()
{
int rate = analogRead(sensorPin); // read the analog input
// the next line scales the blink rate between the min and max values
rate = map(rate, 200,800,minDuration, maxDuration); // convert to blink rate
Serial.println(rate); // print rate to serial monitor
digitalWrite(ledPin, HIGH); // set the LED on
delay(rate); // wait duration dependent on light level
digitalWrite(ledPin, LOW); // set the LED off
delay(rate);
}
Hello All,
When running this code with LDR everything seems to be fine, but when I cover the LDR it freeze up on serial with a negative value. Can anyone explain why this is happening please.
Yes you right playing around with that value seems to help. I just dont understand why it will go into the negative range :o . I changed it from 200 to 0 and now it works ok. But still it bothers me that it will go till the negative range. I thought that it will only have a value between 0 and 1023.
Hi,
The map function basically allows you to rescale and offset your input variable.
It does not restrict the output value to the output parameters you have used to rescale.
So if rate input is below 200, then rate output will be below minDuration.
The same if you are larger then 800, output will be larger than maxDuration.