Capturing Data Help.

Hi, I need help for my robot project.
I’m using a for loop with multiple if statement in the loop.
eg:
for(ArmPos=ArmPos;ArmPos>0;ArmPos -= 1)
{
if(sensor_value1 < 300)
{ Servo1.write(ArmPos)}

if(sensor_value2 < 300)
{ Servo2.write(ArmPos)}

if(sensor_value1 > 300)
{ Servo1.write(ArmPos)}

if(sensor_value2 > 300)
{ Servo2.write(ArmPos)}
}

Logically, if the condition in ‘if’ statement is satisfied, then it will skip the ‘if’ statement rite? and the ArmPos value will not change in the
‘if’ statement. I’ve tried it and it doesn’t work.

What i want is that when the sensor_value1 >300, i need to capture the value of ArmPos at that instant and
write it in Servo1(become fixed value; ‘red ArmPos’). While sensor_value2 <300, Servo2 is feed with ArmPos running values(‘blue ArmPos’).
I have no idea how to do this…Can somebody help?

The problem is not in the if statements. It is in the for loop.

for(ArmPos=ArmPos;ArmPos>0;ArmPos -= 1)

What does ArmPos start at?

Logically, if the condition in 'if' statement is satisfied, then it will skip the 'if' statement rite? and the ArmPos value will not change in the
'if' statement. I've tried it and it doesn't work.

If the condition in the if statement is satisfied (i.e. is true), the body of the if statement WILL be executed. None of the if statement bodies modifies the value of ArmPos. ArmPos is modified by the for loop, completely independent of any sensor values, which are not read during the for loop, either.

Code:
for(ArmPos=ArmPos;ArmPos>0;ArmPos -= 1)

What does ArmPos start at?

The ArmPos start from 150 to 0.

Logically, if the condition in ‘if’ statement is satisfied, then it will skip the ‘if’ statement rite? and the ArmPos value will not change in the
‘if’ statement. I’ve tried it and it doesn’t work.

What I mean is that when the if statement is NOT satisfied,
eg:
for(ArmPos =150; ArmPos > 0 ; ArmPos -=1)
{
if (sensor_value1 < 300)
{ Servo1.write(ArmPos)}
}

If the current value of sensor_value1 is 200 : ArmPos will be 150 - 0
If the sensor_value1 is 400 : the if statement will not run the body(skip) thus the value ArmPos in the Servo1 did not change rite?

What I need is to capture ArmPos value so that i can write the value in Servo1 and make it stop at any value even though ArmPos keep decreasing…

eg:
Servo1 and Servo2 start at 150 :
ArmPos : 150 149 148 147 146 145 144 143 142 141 140…
| |
here sensor_value1 >300 |
here sensor_value2 >300
So, Servo1 stop at 145 even though ArmPos keep decreasing. Then Servo2 stop at 141 while ArmPos keep decreasing

That is what I do not know how to do…

What I need is to capture ArmPos value so that i can write the value in Servo1 and make it stop at any value even though ArmPos keep decreasing…

You’ll need to add some flags, initially set to false, to record whether the servo was moved to a specific position, and then not move the servo again when that flag is true.

bool servoOnePosNoted = false;
bool servoTwoPosNoted = false;

for(int armPos = 150; armPos > 0; armPos--)
{
   sensorValue1 = analogRead(sensor1pin);
   if(sensorValue1 < 300 && !servoOnePositionNoted)
   {
      servoOnePositionNoted = true;
      servoOne.write(armPos);
   }

   sensorValue2 = analogRead(sensor2pin);
   if(sensorValue2 < 300 && !servoTwoPositionNoted)
   {
      servoTwoPositionNoted = true;
      servoTwo.write(armPos);
   }
}

Notice in this code that it actually reads new sensor values during the loop, on the assumption that it is the movement of the servo that causes the sensor value to change.

If this is not the case, the loop approach doesn’t make sense.