How can I get my function to run 3 attempts before requesting my WaitForUserButton() funciton?

Hello All!

I am trying to get my variable HomeNotReachedCount to reach a value of 3 before allowing the function WaitForUserButton() to be called.


int CycleAttempt = 3;
int HomeNotReachedCount=0;

void WaitForUserButton(){
  Serial.println("Button needs to be pressed");
  delay(2000);
}

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

}

void loop(){
  CycleAttemptCounter(HomeNotReachedCount);
}

void CycleAttemptCounter(int FailureCount){       
  FailureCount = FailureCount + 1;
	Serial.print("Attempt ");
	Serial.print(FailureCount);
	Serial.println(" failed.");
		if(FailureCount = CycleAttempt){
			WaitForUserButton();
		}
}

Currently I get printed every time:

Attempt 1 failed.
Button needs to be pressed

Welcome to the forum

Your topic was MOVED to its current forum category as it is more suitable than the original


  if (FailureCount = CycleAttempt)

= is for assignment
== is for comparison

1 Like

Once you have changed your assignment operator to a comparator then you will also have to think about resetting cycleAttempt unless you want to continuously run the function. Also I presume delay is to be removed once this segment of code works

Fixed, current result constantly writes:

Attempt 1 Failed.

So CycleAttempt should stay constant at 3 (or user defined). The button function will have the code run a while loop until it detects the button being pressed.

Right now I cannot get HomeNotReachedCount to count up.

Corrected code:

int CycleAttempt = 3;
int HomeNotReachedCount=0;

void WaitForUserButton(){
  Serial.println("Button needs to be pressed");
  delay(2000);
}

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

}

void loop(){
  CycleAttemptCounter(HomeNotReachedCount);
}

void CycleAttemptCounter(int FailureCount){       
  FailureCount = FailureCount + 1;
	Serial.print("Attempt ");
	Serial.print(FailureCount);
	Serial.println(" failed.");
		if(FailureCount == CycleAttempt){
			WaitForUserButton();
		}
}

Might be worth writing a flow diagram of exactly what you want the program to do. Being stuck in loops is often an intuitive but disadvantageous way to progress through multi-branch code.

Try
void CycleAttemptCounter(int &FailureCount)

1 Like

very small but very "effective" bug:

you are using a single equalsign
a single equalsign is assinging a value
your code is doing
FailureCount = 3
after assigning value 3
the if-condition gets evaluated which means
if(3)
any non-zero value is seen as a true
this means your code
if(FailureCount = CycleAttempt)
is the same as
if(true)

you simply change from a single equalsign to a

doubled

equalsign

==

if(FailureCount == CycleAttempt)

and then it should work

best regards Stefan

... that was pointed out in post #2, and fixed as per post #4.

The problem is that the OP is expecting a parameter passed by value to be changed in the calling routine.

This worked @red_car !!! Thank you, if you would not mind explaining the logic behind adding the '&'.

The "&"-symbol is the ampersand-operator

The ampersand operator enables to modify the variable that is "handed over" inside the paranthesis

This is done this way
the ampersand-operator does not handover the value it does handover the RAM-location where the value is found in RAM.
with ampersand-operator modifying the value inside the function results in modifying the variable that was used to hand-over the value

void CycleAttemptCounter(int FailureCount){

create a local variable inside function CycleAttemptCounter and assign the value given at call

CycleAttemptCounter(HomeNotReachedCount);
void CycleAttemptCounter(int &FailureCount){

use the RAM-adress of the variable that is handed over by the function-call

CycleAttemptCounter(HomeNotReachedCount);

use

FailureCount

as RAM-adress (which is the RAM-adress of HomeNotReachedCount
This means any modifications inside function CycleAttemptCounter
are done on RAM-adress of variable HomeNotReachedCount

result: modify the variable that is used in the function-call

best regards Stefan

1 Like

When calling a function you have a couple of options for how the parameters are passed - consider the following.

int x = 2;
int y = 5;

myFunction (x, y);

void myFunction (int a, int &b)
{

}

Variable a is being "passed by value". That is, the value contained x is passed to the routine and a copy is maintained in variable a in the routine. Variable x remains untouched.

Variable b is being "passed by reference". That is, the memory location of y is provided to the routine, and the routine also refers to the same location with variable b. Any changes to b, will also impact y, as internally they are both pointing to the same location where the actual value is stored..

1 Like

Nice explanation of passing by reference thanks @red_car ; I never understood it before.

So I took the liberty of adding some prints to that code, in a full sketch and changing the variables inside myFunction:

int x = 2;
int y = 5;

void myFunction (int a, int &b)
{

  Serial.print("Original a in function: "); Serial.println(a);
  Serial.print("Original b in function: "); Serial.println(b);
  a = 10 * a;
  b = 10 * b;
  Serial.print("New a in function: "); Serial.println(a);
  Serial.print("New b in function: "); Serial.println(b);

}

void setup()
{
  Serial.begin(9600);
  Serial.print("Original x before function: "); Serial.println(x);
  Serial.print("Original y before function: "); Serial.println(y);
  myFunction (x, y);
  Serial.print("Final x after function: "); Serial.println(x);
  Serial.print("Final y after function: "); Serial.println(y);

}

void loop() {}

As you can see in the output below @josuecampos931 the value of x is the same before and after running myFunction, but y has changed:

image

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.