[unsolved]beginner doubt about variables

so I have 4 values or variables va vb vc vv;

and I want to change va-c by just changing vv.
highly doubted it will work;
can someone explain to me why it won't work,
and how to go around it?

vv++; // let's assume vv is second count; For every sec vv=vv+1; 

case 1:
vv=va; //update vv with whatever va is;
va=vv; //update va with vv, basically add 1 to va every sec, just like vv
break;

case 2:
vv=vb; //same thing
vb=vv;
break;

case 3:
vv=vc; //same thing
vc=vv;
break;

and I want to change va-c by just changing vv.
highly doubted it will work;

Correct. It will not.

case 1:
vv=va; //update vv with whatever va is;
va=vv; //update va with vv, basically add 1 to va every sec, just like vv
break;

Once you've assigned the value in va to vv, there is no point in then assigning the same value to va.

can someone explain to me why it won't work,

Think of a variable as a box you can put data in. Putting a value in one box will not affect the value in any other box.

If you want to exchange the value of two variables you need to use a third variable to temporarily hold one of the values.

va = 1
vb = 2

temp = vb
vb = va
va = temp

now va will have 2 in it and vb will have 1 in it

...R

PaulS:

and I want to change va-c by just changing vv.
highly doubted it will work;

Correct. It will not.

case 1:

vv=va; //update vv with whatever va is;
va=vv; //update va with vv, basically add 1 to va every sec, just like vv
break;



Once you've assigned the value in va to vv, there is no point in then assigning the same value to va.



> can someone explain to me why it won't work,


Think of a variable as a box you can put data in. Putting a value in one box will not affect the value in any other box.

will it work like this? highly think it will work. so the problem here is that the code is running so fast, that it just ignore vv++; and va is not being updated with the new value of vv after vv++;
vv=va; //update vv with va
vv++; //add one to vv
va=vv; //update va with vv, basically add 1 to va every sec, just like vv

so the problem here is that the code is running so fast, that it just ignore vv++;

The Arduino is not running code like a labrador puppy. It does not "run code so fast that it will ignore the vv++".

basically add 1 to va every sec,

It will do it muuuuuuuuuuuuuuuuuuuuuuuuch faster than every second. I don't know how many clock cycles it will take, but it's doing 16 million of them in a second to it will breeze through loop() a zillion times before you can blink.

Why don't you describe what it is you're trying to do?- what are those variables (what do they represent), where do they get their values from (other than each other), what's your actual application?

Robin2:
If you want to exchange the value of two variables you need to use a third variable to temporarily hold one of the values.

va = 1
vb = 2

temp = vb
vb = va
va = temp

now va will have 2 in it and vb will have 1 in it

...R

This is more complicated than I can handle. I actually just want to change one value of va with a temporary value of vv; vv is my temp value, and it's adding 1 to itself for every sec; I just need to have va does the something as vv, which is adding one to itself

so will it work like this?

va=5;
vv=0;
vv++; // for every sec +1
temp;

temp=vv; // update temp with whatever value vv is; which is 0 when time is 0;
vv=va; // update vv with whatever va is, which is 5;
va=temp;// temp= vv, which is 0 for time =0; va=0; but va should be 5 instead of 0.

JimboZA:

basically add 1 to va every sec,

It will do it muuuuuuuuuuuuuuuuuuuuuuuuch faster than every second. I don't know how many clock cycles it will take, but it's doing 16 million of them in a second to it will breeze through loop() a zillion times before you can blink.

Why don't you describe what it is you're trying to do?- what are those variables (what do they represent), where do they get their values from (other than each other), what's your actual application?

yep. I understand it is running so fast that one day of my time equals to many years in the machine world.

I have 2 values: Va and Vv; Va is a value that doesn't change a lot, and Vv is the one adding 1 to itself for every 1 sec; so I want to add 1 to Va by Vv for every second; that is basically what I want to do;

need to keep the original Va value, add 1 to Va because Vv is adding 1 to itself;

so

Vv=Va; //update Vv with Va;

Va=Vv; // since Vv is constantly adding one to itself, won't this Va=(Vv++); or this Va is merely equal to Va=(Vv=Va) ?

Sorry but you lost me totally there.

Could you have another go at explaining what you're trying to achieve? Not how you think you should do it - you seem to have some misconceptions about how assignment and variables work that are making it hard to grasp what you're after.

wildbill:
Could you have another go at explaining what you're trying to achieve?

I already tried to get that from the OP 8)

JimboZA:
Why don't you describe what it is you're trying to do?- what are those variables (what do they represent), where do they get their values from (other than each other), what's your actual application?

If you want to exchange the value of two variables you need to use a third variable to temporarily hold one of the values.

I seem to remember that there are ways of doing it without a third variable.
[Google]

   a = a + b;
   b = a - b;
   a = a - b;

[/Google]

UKHeliBob:
I seem to remember that there are ways of doing it without a third variable.

Jeez, the OP said he couldn't understand the simple way !

I have 2 values: Va and Vv; Va is a value that doesn't change a lot, and Vv is the one adding 1 to itself for every 1 sec; so I want to add 1 to Va by Vv for every second; that is basically what I want to do;

What is wrong with something simple like Vv = Vv + 1;

If you want to change it every second

unsigned long prevMillis = millis();

void setup() {
    Serial.begin(9600);
}

void loop() {
     if (millis() - prevMillis >= 1000) {
        Vv = Vv + 1;
        prevMillis += 1000;
        Serial.println(Vv);
    }
}

...R

arduinomagbit , can you explain what you are trying to do in a non-programming way? What are you trying to accomplish? What is your goal the code is trying to solve? The problem people are having is they have no context on what the code is suppose to accomplish.

Think your your program like a recipe (hence why many coding books have "cookbook" in their titles). Tell us what you are trying to make, what your ingredients are (inputs), what steps are involved to accomplish the final product, and the expected final product (outputs).

UKHeliBob, I am going to add a caveat to your suggestion as new coders shouldn't use that unless they understand it. That code will work for numeric values, and only if added the value stay with in the limit of the data type. If the data type is, let's say int, then if a + b is greater than what an int can hold that won't work. This is a fairly common problem to summing values, having the result datatype be too small for the sum.

SirPoonga:
UKHeliBob, I am going to add a caveat to your suggestion as new coders shouldn't use that unless they understand it. That code will work for numeric values, and only if added the value stay with in the limit of the data type. If the data type is, let's say int, then if a + b is greater than what an int can hold that won't work. This is a fairly common problem to summing values, having the result datatype be too small for the sum.

Really?... Maybe you should try it.

UKHeliBob:

If you want to exchange the value of two variables you need to use a third variable to temporarily hold one of the values.

I seem to remember that there are ways of doing it without a third variable.
[Google]

   a = a + b;

b = a - b;
   a = a - b;



[/Google]

va=5;
vv=0;
vv++;

//time=0
va=va+vv; //5+0=5;
vv=va-vv; //5-0=5;
va=va-vv; //5-5=0; //va should be 5 when time ==0; it doesn't work,

wildbill:
Could you have another go at explaining what you're trying to achieve? Not how you think you should do it - you seem to have some misconceptions about how assignment and variables work that are making it hard to grasp what you're after.

.....I think I have said it already. Basically, I want to add 1 to 3 different values in 3 different cases every second, but I want to keep the original value from those 3 values and add 1 based on their original value.

I know I can use 3 temporary values for these 3 timing values, but I don't want to waste memory, and I want to learn to write smarter code, so, I want to have this 1 temporary value, vv, to control 3 other values.

SirPoonga:
arduinomagbit , can you explain what you are trying to do in a non-programming way? What are you trying to accomplish? What is your goal the code is trying to solve? The problem people are having is they have no context on what the code is suppose to accomplish.

Think your your program like a recipe (hence why many coding books have "cookbook" in their titles). Tell us what you are trying to make, what your ingredients are (inputs), what steps are involved to accomplish the final product, and the expected final product (outputs).

UKHeliBob, I am going to add a caveat to your suggestion as new coders shouldn't use that unless they understand it. That code will work for numeric values, and only if added the value stay with in the limit of the data type. If the data type is, let's say int, then if a + b is greater than what an int can hold that won't work. This is a fairly common problem to summing values, having the result datatype be too small for the sum.

basically I am trying to simplify my code using one input to control 3 outputs in 3 different cases; There is no practically real thing that I want to accomplish, but merely want to learn how to code better.

will this works??

int va, vb, vc, vv;
va=1;
vb=2;
vc=3;

(for every sec) vv++;

case 1: // want to add 1 to va (1) for every sec;
(for every sec) va++; // I know I can do this, but I want to try using the value from vv instead;

case 2: // want to add 1 to vb (2) for every sec;
(for every sec) vb++;

case 3: // want to add 1 to vc (3) for every sec;
(for every sec) vc++;

arduinomagbit:
but merely want to learn how to code better.

So, better coding might be not doing what you are trying to do.

the key to better coding is simple coding.

boil your idea down to the basic requirement and write as little code as possible.

Notice that your thread has been read over 150 times and no one can yet understand what you are trying to communicate.

I agree, very hard to work out what you are really trying to do.

if you want to remove the switch, you can use an array:

int vv = 0 ;

int vs[ 3 ] = { 1, 2, 3 }; //instead of va, vb, vc

And the value you pass to the switch, you can index the array with:

vs[ case - 1 ]++; //remove the -1 if case is range 0 to 2, instead of 1 to 3

It still has no reliance on vv, why should it?

Or do you want to increment va, vb & vc all at the same time... if so dump vb and vc and just use the one variable ( va ), or dump all three and add an offset to vv when you use it.