Dimming with AnalogRead/analogWrite-basic

Hi everyone,

I have a real basic question about dimming LEDs using analogRead/analogWrite. I have zero programming experience, so please no laughter as my vocabulary in regards to this subject may be a little caveman-ish.
I am having no luck getting the code right for what I am trying to accomplish.

Do I need an input pin for analogRead()? Or can it be a int value?

I am using a TFT LCD screen, and have all my color channels set to a int Max_bright between 0-100.

What I am trying to do is analogRead that value, and then analogWrite to the appropriate pwm channel.

Simplistic code example might help clarify what I am talking about:

int white_leds = 7;
Int white_max = 100;

Int ledlevel = 0;

void setup()
{
pinMode(white_leds, OUTPUT);
}

void loop()
{

ledlevel = analogRead(white_max); //when white_max=100, analogRead=1023???

analogWrite(white_leds, ledlevel/4); //converts the read 1023 to the write 255??

}

This is basically how I have been trying to dim the LEDs, but I might be missing something because I've been trying to get similar code working, but I'm stuck.
I want it to read the value of white_max. I might be missing something there. When white_max = 100 then the read is 1023...99 then the read is 1012...98 the read is 1002 ect.
I might be wrong thinking this behaves in this way.

Thanks for a push in any direction.

simplified the code

int white_leds = 7;  //pin

void setup()
{
  pinMode(white_leds, OUTPUT);
}

void loop()
{
  int ledlevel = analogRead(A0);  // you must supply a pin number here
  analogWrite(white_leds, ledlevel/4);  //converts the read 1023 to the write 255 ( YES!)
}

Thanks for the reply!!

Is the pin number (A0) a random pin?

How is the correct dimming/brightness value desired achieved?

Sorry if this is obvious.

The analogRead doesn't work that way. If you are just wanting to dim the LCD backlight, you just use analogWrite(PIN,VALUE);

It might be better if you explaining in detail what you are trying to do (Please include any libraries and the entire code). The way you explained it, I think you are way over complicating it.

Why do you want to read a value then use it?

Is the pin number (A0) a random pin?

No.
It is analogue input pin 0, the clue is in the name.

How is the correct dimming/brightness value desired achieved?

If you want to control it from a pot which you have connected up to analogue pin 0 , then analogWrite(7, reading/ 4) will do it.

What I am trying to do is analogRead that value

What is "that value"? I don't think you need analogRead()*...

You've got:

int white_max = 100;  // Note - int must be lower case, not Int.

In that case, we (and your sketch) know white_max is 100, so if we want to set the brightness to 100, we have two choices:

analogWrite(white_leds, 100);  // The brightness is not variable.

Or since the variable white_max equals 100,

analogWrite(white_leds, white_max);     // Brightness = value of white_max
  • analogRead gets the voltage from an analog pin and converts that voltage to a number between 0 and 1023, and (usually) you assign that value to a variable. analogWrite() accepts values between 0 and 255. That's why you divide by 4 when you dim an LED, based on an analog input voltage (from a pot, etc.).

I got it that (A0) is analog pin 0. I guess my confusion was that I don't have a pot to control the dimming, and was hoping I could just do it through the LCD display. My fault.

The only code I have written at this point is screen navigation from page to page. And, now I want to implement some LED control.

Pictures might help illustrate what I'm trying to do.

I have a led home screen that will just show current status of all led functions.

Navigate to the led channel screen to change the maximum brightness of each led color.

I can change the maximum brightness value here by highlighting the corresponding number, and then use the up/down arrow keys to change this value. Like here I lowered the white channel from 100 to 80.

So, for the 100 displayed on the screen correlates to an analogWrite of 255...the 80 correlates to an analogWrite of 204.

What I am trying to figure out is if I can get a read of this value, and then analogWrite so I can have dimming control of each color channel.

Wow that is a million miles from your initial question and I have no idea what you are doing, what you are doing it with or what you actually want to do.

If you have a number you can use the map function to transform one range of numbers into another range.

I am trying to build a reef/aquarium controller with Mega. I would like it to control all my pumps, lighting, log and graph water temperature and pH...ect. Right now all it's just a clock. Lol!

I just got done with the interface which took me uhhhh 4 months to learn how to do. Trying to teach an old dog new tricks here....cell phones were not readily available when I graduated high school, and it was just the birth of the tech age. So, I'm a little behind the times.

Don't let the interface fool you. I'm a million miles from nowhere. Although I'm pretty proud I was able to get that up to speed, I still don't have the confidence that actually know what I'm doing. It took me 4 months of trial and error to learn how to write that code. I am hoping to cut that in half. 2 months to learn how to code the lighting controls. I am currently working on what I would consider the easy part. Just dimming each color channel so I can get a good blend of color displaying the tank.
After I learn that I will want simulated sunrise/sunset, passing cloud simulation, lightning simulation, moon lighting corresponding to each phase of a lunar cycle, and coral acclimation.

A tall order...

Now..the map function. I was wondering if I should be looking there. I will start there. That's why I was trying to see if I could use an int value instead of a pin.
I need to learn how I can get the int value and translate that to a pwm write.

That's why I was trying to see if I could use an int value instead of a pin.

They are two different things. The pin number tells the computer what input / output pin to do things on. int is short for integer and is a variable with no decimal places.

I need to learn how I can get the int value and translate that to a pwm write.

Again confusing question. You use an int to tell the PWM what to do, that is what on / off ratio to use.
Read this about how PWM works.
http://www.thebox.myzen.co.uk/Tutorial/PWM.html
If you want the power provided by the PWM to be half you set analogWrite(pin, 128) where pin is the pin number you have the LED and resistor attached. However the eye is not linear so halving the power will not necessarily half the perceived brightness.
Have you read this:-
http://arduino.cc/en/Tutorial/Fading

Thanks. You gave me a nudge in a different direction. I've been over that fading tutorial/example at least 100 times, but always passed it up because of the "delay(30)". I had it stuck in my head that I needed a button push to change the brightness because I wanted something I could control and set.

I'm going to start experimenting with this. What can I use instead of "delay"? Maybe "return"?

check the blink without delay example

Grumpy_Mike was right. I need to map. I got the answer from another Reefer doing a similar build.

I think, if I understand him correctly, what I need to do is change all the "int Max_Bright = 100;" to "byte Max_Bright = 100;", and then change "int ledlevel = 0;" to "byte ledlevel = 0;"

and then in the Loop

ledlevel = map(Max_Bright, 0, 100, 0, 255);

analogWrite(ledPin, ledlevel);

My new question about this is there an efficient way to write the code to include all 4 different "byte Max_Bright" , or will I have to write them each separately using "else if"?

What I mean, and what I am thinking, is that I will have:

byte White_max = 100;
byte RBlue_max = 100;
byte Blue_max = 100;
byte UV_max = 100;

byte ledlevel = 0;

then code something like:

if (Enter_new_value == true){ //this is already part of my written code to change the on screen values

if (change_max_bright == 1){ //also a part of my existing code to change values
ledlevel = map(White_max, 0, 100, 0, 255);
analogWrite(All_Whites, ledlevel); // All_Whites is the array for the 3 white led pins
}

else if(change_max_bright ==2){
ledlevel = map(RBlue_max, 0, 100, 0, 255);
analogWrite(All_RBlues, ledlevel);
}

ect. on down the line.

am I on the right track?

Thanks again to all.

That's exactly what I had to do...wrote the code that way, and it works. Getting exactly what I wanted.

what I need to do is change all the "int Max_Bright = 100;" to "byte Max_Bright = 100;", and then change "int ledlevel = 0;" to "byte ledlevel = 0;"

Byte or int should not make any difference, it will still all work with int if you change it back.