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
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.
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.
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)
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
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..
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: