PLEASE HELP! Need to add a TARE function to program

So in my code, is it possible to rearrange at put the setup() and loop() at the top??

If you look at my sketch there is a lot of code before the setup() and loop() commands come into play and therefore i would be introducing an int tareValue command after it is all calculated in the sketch.

So is there another place i should place the command in my sketch? and i also don't want the button. I just want it to happen automatically.

The program does not run in the order that the functions appear in the IDE. Basically any variable declarations that appear at the top of the program cause the variables to be made available globally, then setup() runs once and loop() runs over and over again. As long as other functions are defined outside of setup(), loop() or any other function they can appear anywhere in the editor and the compiler will take care of creating the correct code.

Try this

byte aGlobalByte = 123;

void aFunction()
{
  Serial.println("In aFunction()");
  Serial.print("aGlobalByte is currently ");
  Serial.println(aGlobalByte);
}

void setup()
{
  Serial.begin(115200);
  Serial.println("In setup()\n");
}

void loop()
{
  aFunction();
  aGlobalByte++;
  anotherFunction();
  delay(1000);
}

void anotherFunction()
{
  Serial.println("In anotherFunction()\n");
}

So i have tried putting the 'tareValue' code in several spots of the program and it uploads just fine, but will not run. Why is this?

Post one of your attempts here.
What do you mean by

will not run.

Have you tried putting some Serial.print()s into the program to see which parts are running and the value of relevant variables at various points ?

Nevermind, thank you very much for your help :slight_smile:

I was missing the

int tareValue = 0;

at the very beginning.
Why exactly do you need this, aswell as the analogRead of the pin for the tareValue?

In C, you need to 'declare' a variable, as in tell the arduino what type of variable it is before you can use it.

By setting it to zero (or any valid number for an int type) you are 'initialising' it.

Some see it as good coding practice to do it at the declaration (you have to do it somewhere).

The more you learn code and expose yourself to other languages the more you'll start to see why things are the way they are (or aren't) - until then, just remember it !

Taylor_woods:
Nevermind, thank you very much for your help :slight_smile:

I was missing the

int tareValue = 0;

at the very beginning.
Why exactly do you need this, aswell as the analogRead of the pin for the tareValue?

If by "at the very beginning" you mean "outside any function", then that variable has global scope, and the compiler/crt0 will automatically assign it the value zero, unless you specify another value before the rest of your sketch runs.