Hi, i have a problem. I want to make the LED light up and stay lit up for 5 seconds, when the photoresistor's value reaches the threshold.
Here's the code:
const int analogPin = A2;
const int ledPin = 13;
const int threshold = 480;
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
int analogValue = analogRead(analogPin);
if (analogValue > threshold) {
digitalWrite(ledPin, HIGH);
}
else {
digitalWrite(ledPin,LOW);
}
Serial.println(analogValue);
delay(1);
}
Need more definition of exactly what you want to happen. Looking at your code it looks like you want the turn the LED on when the analogRead() return for the analog input of the photoresistor becomes larger than your threshold. (Your original description didn't specify in what direction you reach the threshold...) Is this correct?
Assuming you want "greater than the threshold", now for some of the more nitty-gritty... What if, while the LED is on for your timeout period (5 seconds in this case), the photoresistor drops below the threshold and then goes back above the threshold. Should the timer reset and count from the new rising threshold crossing point (would result in the LED being on more than your timeout)? Or, should it ignore rising threshold crossings until after the LED turns off? Should there be any delay after the LED turns of where it may ignore rising threshold crossings, or should it respond to the very next one even if it is nearly immediate?
Erdin:
I would use an integer number 2 for the analog port.
const int analogPin = 2;
Actually, I would use A2 instead of the digit 2. Simply for consistency (and allowing cross-board compatibility) I use A for any analog pin. That way when later looking at my code I know what Arduino pins I'm using. No letter before the pin number (well, pin name because those names don't map directly to the same pin number of the actual uP) and I'm referring to a digital pin and would mentally throw a warning flag if I saw analogRead(2); in my code (i.e. "why am I trying to take an analog reading off a digital pin?"). One of the (IMHO positive) side effects of this consistency is if I want to use an analog pin for digital functions, doing a digitalWrite(A0); will digital write to A0 on all Arduino boards. (If I tried the valid-on-uno trick of digitalWrite(14); for A0, it wouldn't map to A0 on a mega and may confuse wiring issues in the future.)
But, those are my arguments for this portion of my own programming style. YMMV
Also to rephrase AWOL, pin names (for the foreseeable future on the Arduino platform) will not exceed 255 individual I/O pins, so why not use the more memory conscious byte data type to only use 1 byte of precious SRAM to store the value for the pin number instead using 2 bytes of precious SRAM (int is 2 bytes) to store the same value range? Or, use the compiler #define to replace a name with a literal value at compile time (takes no (additional) SRAM).
Thank you Sembazuru,
I agree "digitalWrite(14);" on a Uno is asking for trouble.
Perhaps it should be added to the reference, that both '0...5' and 'A0...A5' are valid.