If I put a for loop inside a while loop will other if statements still work?

The title is a bit confusing but I guess ill just put the code here

various deifning variables and button definitions

while(1==1){

  • bunch of if statements here for driving, clamp motors, etc*

if(E_controller_digital_A = 0){
for(ClampOpenVar = 0; ClampOpenVar = 8; ClampOpenVar++){
pros::c::motor_move(8,127)
delay(1000)
pros::c::motor_move(8,0)
}
}
}

It basically opens the clamp so that it stays open whenever the button isn’t pressed. However, I cant have it run forever because it will overturn the gears and break the clamp. The issue is I don’t know if other if statements will run while the for loop is active. Will it? or is this entire idea just useless?

background info I forgot to add

*programmed in C++
*I free handed this code from memory, ill post the actual code later due to me not having access to the computer rn

  • If you have another solution that isn’t as complex, please tell me.

Think about it… code runs line by line. Not block by block. I suggest you look into pros::task for multithreading options if you must do this.

4 Likes

Here is a concept. If the button is pressed then you want it to clamp down. Otherwise open to a resting position. Sound correct?

while(1){
    Motor1.spin(forward);
    if (Controller1.ButtonL1.pressing()) {
      Motor1.setVelocity(50.0, percent);
    }
    else {
      Motor1.stop();  //Not sure if a stop is necessary.
      //Assuming that the resting position is the same as starting position
      Motor1.spinToPosition(0.0, degrees, false);
    }
    wait(20, msec); 
}
1 Like

This is correct but this needs to function in a while loop. A while loop inside a while loop wont work in ym situation

timeval t1, t2;
	gettimeofday(&t1, NULL);
timeval ClampTimerStart;
		bool clampTimerStarted = false;
		bool clampTimerStarted2 = false;


//below is inside the the loop 
 gettimeofday(&t2, NULL);
	    double interval = ((t2.tv_sec - t1.tv_sec) * 1000.0 + (t2.tv_usec - t1.tv_usec)/1000.0) / 1000.0; //time since start of program in seconds

			if (ClampBtn == 1)
			{
				if (!clampTimerStarted) 
				{
					clampTimerStarted = true;
					gettimeofday(&ClampTimerStart, NULL);
				}
			double interval2 = ((t2.tv_sec - ClampTimerStart.tv_sec) * 1000.0 + (t2.tv_usec - ClampTimerStart.tv_usec)/1000.0) / 1000.0; //time since clamp timer started in seconds
			pros::c::motor_move(8,-127);
		} else if (clampTimerStarted)
		{
			if (!clampTimerStarted2) 
			{
				clampTimerStarted2 = true;
				gettimeofday(&ClampTimerStart, NULL);
			}
			double interval2 = ((t2.tv_sec - ClampTimerStart.tv_sec) * 1000.0 + (t2.tv_usec - ClampTimerStart.tv_usec)/1000.0) / 1000.0; //time since clamp timer started in seconds
			pros::c::motor_move(8,127);
			if (interval2 >= 8) 
			{
				pros::c::motor_move(8,0);
				clampTimerStarted = false;
				clampTimerStarted2 = false;
			}
		}

this is a solution one of the senior programmers on my team came up with. This might work but im unsure if it will. will make this or another response the solution if it works. It’s also WAY out of ym skill level of programming

When programming, the first step is to identify what needs to be done, not how it should be done.

The implementation provided seems needlessly complex. While “correctness” is always desired in a program, “clearness” and simplicity are also desired.

(Editted to add:)

Without documentation, it is difficult to understand the desired behavior. After reading thru this several times, it seems that the desired behavior is to:

  • Spin the motor
  • wait some period of time
  • Spin the motor to a different position (or stop it, I’m a bit rusty on the pros functions)

If so, while this code may do what is desired, it is difficult to read and therefore difficult to change. This is not something that is outside your skill level of programming (I think), it is just difficult to understand. This is why it is important to write clear code, with good comments.

4 Likes

Generally, you do not want to do nested loops unless you have to. You are correct in your assumption that while in the inner loop none of the code that is part of the outer loop (e.g. drivetrain, lift, etc.) will not work until the system has existed the inner loop. If you need these to be independent of each other, then you should implement tasks.

If you are looking to close a clamp by a press of a button, I’m not sure why you need a timer? A typical if/else statement for checking a button being pressed would work where you set if condition to close the clamp if the button is pressed, and the else section to open the clamp,

2 Likes