Simple Question

Is this command legal?

analogWrite(2, analogRead(A3)/4.0);

Or is it preferred to use this:

float temp = analogRead(A3)/4; analogWrite(2, temp);

I am seeking efficiency

EDIT: forgot to divide by 4

analogWrite(2, analogRead(A3)/4.0);

Legal? Yes. Sensible? No. Pin 2 is not a PWM pin (unless you're on a Mega), and a floating point division is plain wasteful.

analogWrite(3, analogRead(A3)/4); would make more sense.

pyroblast:
Is this command legal?

analogWrite(2, analogRead(A3)/4.0);

Or is it preferred to use this:

float temp = analogRead(A3)/4;
analogWrite(2, temp);

I am seeking efficiency

EDIT: forgot to divide by 4

Yes it’s perfectly legal. In the spirit of my favorite programming book “Practical C Programming”, I would suggest “put parentheses around everything”, like this:

analogWrite (A2, (analogRead (A3)/4.0));

and note that analogRead() returns an INT value and analogWrite accepts an INT value, so forcing the numbers to convert to float by specifically saying “4.0” instead of “4” is pointless.

(edit to add): You can use floating point if you average multiple analog reads, like this:

float averageAnalog (uint8_t port)
{
#define _AVG_COUNT 100 // average 100 readings
    float accumulator = 0;
    float x;
    for (x = 0; x < _AVG_COUNT; x++) { // loop "avgcount" times
        accumulator += analogRead (port); // add up readings
    }
    return (accumulator / x); // return averaged reading
}

When you do an analog read, you will sometimes see the value “wiggle” up and down by 1 bit (due to noise and/or quantization error). Averaging removes noise and quantization errors and gives you more accurate fractional results. Note though analogRead() is S-L-O-W so keep the averaging count reasonable (or do it with direct port access).

AWOL: Legal? Yes. Sensible? No. Pin 2 is not a PWM pin (unless you're on a Mega), and a floating point division is plain wasteful.

analogRead() auto converts "A0" style physical pin numbers to actual pin numbers (look inside " ``` arduino-xxx/hardware/arduino/cores/arduino/wiring_analog.c ``` " and you'll see lines like this:

    if (pin >= 54) { pin -= 54; } // allow for channel or pin numbers

Therefore, "analogRead (A2)" and "analogRead (2)" are the same thing.

. . . but the original reads

analogWrite(2, analogRead(A3)/4.0);

I repeat; unless you're on a Mega, pin 2 is not a PWM pin.

EDIT: Decided to open a new topic since my question is seemly unrelated

AWOL: I repeat; unless you're on a Mega, pin 2 is not a PWM pin.

I never disputed that.

AWOL: . . . but the original reads

analogWrite(2, analogRead(A3)/4.0);

I repeat; unless you're on a Mega, pin 2 is not a PWM pin.

Ah... PWM. I should pay more attention. I was thinking "analogREAD" in my mind, but of course PWM is "analogWRITE" and you are RIGHT Arduino MEGA pin 2 (chip pin 6, PORTE, bit 4) is indeed an analog out (PWM).

pyroblast:
Thank you for your answers as they did help.

So to conclude, I get your point now. It is pointless indeed as the analogRead takes integer values between 0-1023

Now, let’s say I want to analogRead a value from an LED. I want to see how dim the LED is. Let’s say this is LED1. Now, I want to take this value and make LED2 as dim as LED1.

Let’s say that LED1 is connected to pin 1, and LED2 is connected to pin 2. Can I do this?

analogWrite(2, (analogRead(1)/4));

Thinking about it… I’m not so sure you can do an analog READ of a raw (unfiltered) PWM analog WRITE value. I don’t know what the ADC circuitry would do…

You may be better off to use “shadow registers” (actually shadow VARIABLES).

For example:

int value;
int analog2; // will save the value of A2
int analog3; // will save the value of A3

// your code......
value = 128; // set LED 1/2 brite
analogWrite (A3, value); // set A3 pin PWM duty cycle
analog3 = value; // save the current value of A3
//
// later on........
//
value = analog3; // get a copy of the value in A3
analogWrite (A2, value); // set A2 the same
analog2 = value; // save the current value of A2

And, no need to divide by 4 since you are saving the WRITTEN values, not the analogRead() values.

Get it?

Yep.. shadow variables.. totally got it, and I think I will nail this.. thanks

I am seeking efficiency

An analog read takes around 1600 instruction cycles, most of which are a busy wait on end-of-conversion; losing a few cycles here or there on data conversion are down in the noise, relatively speaking.

I'm not so sure you can do an analog READ of a raw (unfiltered) PWM analog WRITE value.

Of course you can, but I can't see why you've brought the topic up.

Not sure of the purpose of "reading" back the brighness setting of an LED.

Detecting a failed LED? Would need to monitor the voltage at the anode.

Just to determine the brightness level? This is already known, its the value used for analogWrite.

Anyways, purpose aside, you could:

With the LED/resistor still connected to the PWM signal, take another branch off the PWM signal, use an RC filter, then connect to an analog input for readback.

Take the PWM signal and connect it to a digital input. Use "pulseIn" to determine the pulse width so you could calculate the brightnes level.

Both methods are not useful as it provides no new information ... you already have the analogWrite value.

Detecting if the LED is burned out, or reverse connected, or checking for short circuit might be useful. In that case, you could connect a wire from its anode to an analog input, then monitor its peak value.