# How can I declare a value from an analogueread

Im trying to make it so that when I have a voltage going into the arduino it will multiply that voltage by 10 and declare that value as a variable..

So lets say I have 5 volts going in. It will take that 5 and multiply it by 10 and assign it as D1.

So D1 will now be set to the value 50

How will this be written on arduino?

``````int d1=analogRead(pinWhatever) * 50/1024;
``````
``````int d1=analogRead(pinWhatever) * 50/1024;
``````

Well, let's see. 50/1024 is 0. So, regardless of the value read from the pin, d1 will be 0. Probably not what OP intended.

PaulS:

``````int d1=analogRead(pinWhatever) * 50/1024;
``````

Well, let's see. 50/1024 is 0. So, regardless of the value read from the pin, d1 will be 0. Probably not what OP intended.

But analogRead(pinWhatever) * 50 could easily be more than 1024. and since * and / have equal precedence, the expression should be evaluated left to right.

Aren't mathematical operations of the same level performed left to right?

Unless you say that the constant expression will be evaluated first, though I don't think so since that can (as you state) alter the value of the expression.

KenF:
But analogRead(pinWhatever) * 50 could easily be more than 1024. and since * and / have equal precedence, the expression should be evaluated left to right.

... and it will easily go beyond the boundaries of an int. An unsigned int would be sufficient if there are no further calculations involving negative values. I would also suggest parentheses to make the order of evaluation obvious (to the reader of the sketch and to the compiler)

olf2012:
I would also suggest parentheses to make the order of evaluation obvious (to the reader of the sketch and to the compiler)

"to the compiler?" Just in case it forgets the order of precedence?

OK Let's do the corrections

``````//dear compiler please remember to evaluate the following expression left to right
unsigned int d1=analogRead(pinWhatever) * 50/1024;
//Thank you.
``````

Unfortunately, it doesn't work:

``````const byte pinWhatever = A0;

void setup ()
{
Serial.begin (115200);
Serial.println ();
}  // end of setup

void loop ()
{
//dear compiler please remember to evaluate the following expression left to right
unsigned int d1=analogRead(pinWhatever) * 50/1024;
//Thank you.
Serial.println (d1);
delay (1000);
}  // end of loop
``````

Connecting to 5V, I get:

``````65522
65522
65522
65522
65522
65522
65522
65522
65522
65522
65522
65522
65522
65522
65522
65522
``````

Clearly not in the range 0 to 50.

Plus, I got a warning:

``````sketch_dec03b.ino: In function ‘void loop()’:
sketch_dec03b.ino:11: warning: you don't need to tell me how to evaluate expressions!
``````

Your problem here is not operator precedence, but integer evaluation.

Adding an L helps:

``````  unsigned int d1=analogRead(pinWhatever) * 50L/1024;
``````
``````49
49
49
49
49
49
``````

Love this!

This is a valid point!

Your problem here is not operator precedence, but integer evaluation.

That and reading comprehension. I swear I saw a set of parentheses in that statement, KenF. Since they are not actually there, can I have my red herring back? They are in short supply, you know.

PaulS:
That and reading comprehension. I swear I saw a set of parentheses in that statement, KenF. Since they are not actually there, can I have my red herring back? They are in short supply, you know.

It's the spacing. 50/1024 seems so tightly packed that you see it as a single expression.

Misplaced your avatar, PaulS?

Misplaced your avatar, PaulS?

It shows up for me.

PaulS:
It shows up for me.

Doesn't show for me either. But it's only happened recently.

Must be because I am in Australia.

I looked at the page source.

Your avatar is rendered thus:

``````<img class="avatar" src="http://forum.arduino.cc/avatars/tigger.gif" alt="avatar_PaulS">
``````

I load up "http://forum.arduino.cc/avatars/tigger.gif" in my browser, and get:

``````<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.2.1</center>
</body>
</html>
``````

The forum must like you more than me.

Old bug.

Looks OK here

The image in LarryDs post I have NEVER seen EVER. Was it changed recently. The one I remember was a portrait shot.

Looks OK here

Well played, sir.