LED dimming using a Potentiometer and a Photoresistor

Hey Everyone,

So I am pretty new to Arduino and started off with a simple LED-dimming program (code below). It was working just a few minutes ago, and suddenly (with no changes), it stopped working. The wiring is all correct, and the Serial monitor does, in fact, show that the values are changing, but the LED doesn't light up when using the photoresistor at all and only lights up using the potentiometer after it crosses a certain value (no dimming, just on/off). Can anyone help me? Thanks!

int photoPin = A0;
int ledPin = 2;
int buttonPin = 13;
int potPin = A1;
int baseline = 0;
void setup()
{
// put your setup code here, to run once:
pinMode(ledPin, OUTPUT);
pinMode(potPin, INPUT);
pinMode(buttonPin, INPUT);
pinMode(photoPin, INPUT);
baseline = analogRead(photoPin);
Serial.begin(9600);
}

void loop()
{
// put your main code here, to run repeatedly:
if(digitalRead(buttonPin) == HIGH)
{
int sensorValue = analogRead(potPin);
int outputValue = map(sensorValue, 0, 1023, 0, 255);
analogWrite(ledPin, outputValue);

// print the results to the serial monitor:
Serial.print("poteniometer = " );
Serial.print(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);

delay(2);
}
else
{
//photoresistor
int sensorValue = analogRead(photoPin);
int outputValue = analogRead(photoPin)-baseline;
if(outputValue < 0)
{
outputValue = 0;
}
// print the results to the serial monitor:
Serial.print("photoresistor = " );
Serial.print(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);

delay(2);
}
}

Can you describe the manner in which it is supposed to operate? There are no comments to help. You say, "it's dimming", "it's changing"... but we don't really know what you intend to happen.

Sorry about my vagueness. When the button is pressed, I wanted the potentiometer to determine the brightness of the LED, and when the button isn't pressed, I wanted the photoresistor to control the brightness of the LED (baseline records the baseline lighting in the room as a comparison).

I know that the potentiometer and the photoresistor are working correctly because the values printed out by the Serial Monitor are correct (they increase and decrease when they're supposed to)- however, the LED doesn't respond in the correct way. When the button is pressed (potentiometer), the LED doesn't light up until the POT reaches a certain threshold, and then turns fully on (so it's acting like a digital input as opposed to an analog input?). And the LED doesn't light up at all when the button is released (photoresistor).

Thank you!
kavyakvk

Yes- attached is a picture of my wiring.

arduino.JPG

Don't declare your variables inside an if statement. Well, not until you're a little more familiar with C, anyways...

 int sensorValue = analogRead(potPin);

Declare them globally along with your other globals:

int sensorValue;
int outputValue;

int photoPin = A0;
int ledPin = 2;
int buttonPin = 13;
int potPin = A1;
int baseline = 0;

and then use regular assignment inside the if statements, e.g.:

...
sensorValue = analogRead(potPin);
...

The problem may be with the "scope" of the variables.

It didn't change anything- is there anything else that could be causing the problem?

Thanks!
kavyakvk

kavyakvk:
It didn't change anything- is there anything else that could be causing the problem?

Thanks!
kavyakvk

Ah, too bad... hey, you should really repost the new version, but this time using the code tags as explained in the sticky posts at the top of the forum.

The post above me makes great sense, although you said it was working perfectly a few minutes ago with no changes... :slight_smile: