Need help creating a counter for an LED

Hello, I'm trying to complete a project where I have to trigger 5 levels of brightness for an LED light from 0 to 255. I'm having some difficultly trying to create a counter using a photoresistor (I'm very new to Arduino). What is the proper syntax to use for creating a counter with a photoresistor and LED, how should I do it in this situation?

[code]
#define PinPWM 11
#define Photoresistor A0 

int PinPWMValue = 0; 

a[6] = { 0, 50, 100, 150, 200, 250 };   // 6 elements 

void setup() {
  // put your setup code here, to run once:
pinMode(PinPWM, OUTPUT); 
pinMode(Photoresistor, INPUT); 

Serial.begin(9600); 

}

void loop() {
  // put your main code here, to run repeatedly:

int PinPWMValue = analogRead(Photoresistor); 
PinPWMValue = map(PinPWMValue,0 ,1023 ,0 ,255);

analogWrite(PinPWM, PinPWMValue);  // will be between 0 and 255 

 
}

[/code]

What is the purpose of the counter? And what will it count?
Paul

What is wrong with that code? It looks rather functional.

PLEASE do not cross post.

I want to use a counter to create 5 levels of brightness for the pin, so when I tap the sensor the pin gets dimmer till it reaches zero

What reading do you get when and when not the sensor is covered ?

apologies, I've been having trouble figuring this part out. I get a reading of 9 when not closed and then 0 when closed

Will you please show us a good image of your actual wiring ?

Please post a little wiring showing the connection of the LDR as well as its designation.
Reading 0 - 9 comes from a mismatch of components.

What is the value of the LDR series resistor ?

What is the resistance of the LDR (use a DMM) when covered and when not covered. (do this with the LDR removed from the circuit) ?

Please, no wall paintings. How can we read what LDR and what resistor value from that picture? Please tell!

the resistance I get 9.99 kOhm, when covered I get no reading. The photoresistor value is suppose to be 10kOhm, the other two resistors are 220 ohm

You get 9.9k when not covered and infinity when covered ?

Do you have a 10k resistor (brown/black/orange) ?

yes, for some reason when I covered the sensor no value would appear. I do have 10K resistors

Try:
+5V----[LDR]----[A0]----[10k]----GND


What is the reading using the above components:
Covered ?
Not covered ?

replacing it with 10kohm I get about 500 when not covered and around 200 when covered when looking on my serial monitor

What other two resistors?
Please stop using that word sallad, grab a pen, paper and post a wiring!
16 replies to help You get the question right. Sorry, I dump Your question for doing something better.

Here's the image of my wiring, and sorry I'm really just looking for help on creating a counter, I look at multiple examples but I'm still having difficulty on making one for this project

Thanks! Okey. I would try an R2 somewhere in the range of 4.7 kOhm - 10 KOhm. Measure the LDR at ambient, none solar light indoors. Select an R2 near that value. Know that the readings will not be linear. Using
if(( value > 0 ) && (value <= counter1Limit) counter = 1;
if(( value > counter1Limit ) && (value <= counter2Limit) counter = 2;

Etc....

1 Like