reset of variable after first loop

Hi, my variable resets after the first loop, I have an if statement that only needs to be run at power up, then ignored thereafter. I have had a break for a few months after an operation and just can not remember how to keep the toggled variable. Should I tried placing it in void setup() but I get an error.

Below is the basic problem… the test=1 and then test=0; when I finish the “do something A” but after " do something B" test is reset back to “1” rather than keeping “0”.

test=1;

void setup() {
}

void loop() {

if (test==1){
//if (…){
//do something A
//}
test=0;
}

//if (…){
//do something B
//}

}

 if test==1 {

is incorrect. Should be

if (test==1) {

Thank you, Sorry about that, I just did this as an example for the problem,I will edit that. My program works perfectly except I want to retain the change from the test=1 to test =0, but on the second loop test is set back test =1...

You really need to post your code.
In code tags.

I don't know what you are doing at A and B but either of these work

int test = 1;

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

void loop()
{
  if (test == 1)
  {
    Serial.println("test is 1");
    test = 0;
  }
}
void setup()
{
  Serial.begin(115200);
}

void loop()
{
  static int test = 1;
  if (test == 1)
  {
    Serial.println("test is 1");
    test = 0;
  }
}

and so does this

int test = 1;

void setup()
{
  Serial.begin(115200);
  if (test == 1)
  {
    Serial.println("test is 1");
    test = 0;
  }
}

void loop()
{
  if (test == 1)
  {
    Serial.println("test is 1");
  }
}

Thanks Bob, the first code is nearer to what I am doing, I will do a debug and see where I am going wrong. Could be a glitch on the PSU doing a reset or some other problem with my programing :confused: I have three motors controlled and could be that more smoothing/filtering is required..more work for tomorrow... :slight_smile:

Had to dig out an Arduino to check

This is simplistic version of what I have and as you have mentioned works OK

int test = 1;

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

void loop()
{
  if (test == 1)
  {
    Serial.print(" If test ...  ");
    Serial.println(test);
  }
test = 0;
Serial.print("  Loop test ...  ");
    Serial.println(test);
delay(10000);
}

JohnWood337:
Thanks Bob, the first code is nearer to what I am doing, I will do a debug and see where I am going wrong. Could be a glitch on the PSU doing a reset or some other problem with my programing :confused: I have three motors controlled and could be that more smoothing/filtering is required..more work for tomorrow... :slight_smile:

Had to dig out an Arduino to check

This is simplistic version of what I have and as you have mentioned works OK

int test = 1;

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

void loop()
{
  if (test == 1)
  {
    Serial.print(" If test ...  ");
    Serial.println(test);
  }
test = 0;
Serial.print("  Loop test ...  ");
    Serial.println(test);
delay(10000);
}

Are you by any chance powering the motors directly from the arduino?

No, they are two driven by dual "H" bridge driver and the other direct to the batteries, loads of decoupling both low and high value caps on the inputs of the drivers and PSU's. The Arduino is fed by a separate PSU program works great and I added this variable to do one action and then hoped it would be bypassed on the next loop.

I felt a bit deflated when it occurred as I added it late this afternoon and could not see why it occurred.

My programing skills are not that good and all the program additions have been done on the fly.
I would post it but after seeing what other folk do I would be to embarrassed and would certainly not want to be the cause of any injury with people falling off their chairs...

It is a lawn mower and does do what I need, just need to get the edge cutting amalgamated with the random cutting, but as it stands it keeps going back to the edge cutting after the first pass has the toggle variable keeps returning to "1", Fresh brains tomorrow... :slight_smile: