Go Down

### Topic: Standard Dimming Circuit Help (Read 66 times)previous topic - next topic

#### johncshaw2

##### Nov 19, 2019, 12:32 am
Hey guys,
I'm new to coding and Arduino and am working on a simple LED dimming circuit that is logarithmic and smooth to the eyes.

I'm using a potentiometer to control the brightness.
This is the code I have now, and even though it brightens and dims the LEDs, it doesn't do it smoothly as LED brightness doesn't directly correspond to how we perceive it.

I've read online that people can achieve an effect that dims it smoother to the eyes by doing it logarithmically, however, I'm still so new that I can't figure out how to incorporate it into my code.

Here is my code:

Code: [Select]
`#define LED 3#define KNOB 0void setup() {  pinMode(LED, OUTPUT);}void loop() {  // Read the value of the potentiometer knob  int knobValue = analogRead(KNOB);  // Map the potentiometer value to 1-255  int intensity = map(knobValue, 0, 1024, 0, 255);  // Output the respective value to the LED  analogWrite(LED, intensity);}`

#### Englishscone

#1
##### Nov 19, 2019, 01:56 amLast Edit: Nov 19, 2019, 01:57 am by Englishscone
Hey there! Welcome to the Arduino world!

Word of caution: Your going to find some people edgy on this forum, not willing to help you to the best of their ability or putting you down as someone new. This fact is not dispersed across the board, but still. Keep in mind that you have the ability to learn as far as you want, so never doubt what you can do!

Ok, lets look at your code. I'm comparing to AnalogInOutSerial, under File>Examples>Analog, it really looks the same other than you don't have a short delay, such as delay(2); at the end of your loop. Also, compare this section of the example code to yours:

Code: [Select]
`const int analogInPin = A0;  // Analog input pin that the potentiometer is attached toconst int analogOutPin = 9; // Analog output pin that the LED is attached toint sensorValue = 0;        // value read from the potint outputValue = 0;        // value output to the PWM (analog out)`
They didn't define the pins, they made then variables.
If you wanted to, you could try out this code and see what is different.

This might be the problem, but it might also be how you set up your circuit. If its on a breadboard, some wires may not have a good connection.

Hope it works out!

~Englishscone

Go Up