This is a response to the post made in the Official Tech Support Channel by @CHINESEISASIAN .
There are many ways that you could accomplish this, one way that you could go about this is to maintain a variable with your current iteration through a set point array (or use something like switch statements). This means that you just need to refer back to the variable to tell which action to take next with your program.
Here are 2 examples of this in code. Both untested but intended to show the concepts at work:
Using an array:
// We are going to assume that you have a PID controller position setup with
// a setpoint interface of some description. This is just an eg.
bool setArmControllerSetpoint(int setPointPos);
unsigned byte armIterationStep = 0; // Init Step var
int armIterationSetpoints[2] = {120, 130}; // 2 example Setpoints provided
bool Btn5DPressed = false;
bool Btn5UPressed = false;
bool result; // E.g. debug info as returned by setArmControllerSetpoint
// Main loop
while(true) {
// We are going to be using something similar to a toggle switch here
// to make sure we only accept each individual press.
if(vexRT[Btn5D] && !Btn5DPressed) {
Btn5DPressed = false; // Toggle Switch Code
// Here we will set the setpoint to the controller to the setpoint that
// corrosponds to the armIterationSetpoints array.
// If this is the first time you press the button, armIterationStep
// will be 0 so we will get the 1st element of armIterationSetpoints (which is 120).
result = setArmControllerSetpoint(armIterationSetpoints[armIterationStep]);
// Check that we sucessfully set the setpoint
if(result)
writeDebugStreamLine("Error setting setpoint for ARMPID with value: %d", armIterationSetpoints[armIterationStep]);
// Then we increase armIterationStep so that next time we will use the 2nd element of the armIterationSetpoints array.
armIterationStep++; // Add 1
}
// We will make it possible to press the button again once it has been released.
else if(!vexRT[Btn5D] && Btn5DPressed) {
Btn5DPressed = true;
}
// Reset button
if(vexRT[Btn5U] && !Btn5UPressed) {
Btn5UPressed = true; // Toggle Switch Code
// We reset armIterationStep to the default of 0
armIterationStep = 0;
// Will be 0 so we will get the 1st element of armIterationSetpoints (which is 120).
result = setArmControllerSetpoint(armIterationSetpoints[armIterationStep]);
// Check that we sucessfully set the setpoint
if(result)
writeDebugStreamLine("Error setting setpoint for ARMPID with value: %d", armIterationSetpoints[armIterationStep]);
}
else if(!vexRT[Btn5U] && Btn5UPressed) {
Btn5UPressed = false;
}
}
Using switch statements:
// We are going to assume that you have a PID controller position setup with
// a setpoint interface of some description. This is just an eg.
bool setArmControllerSetpoint(int setPointPos);
unsigned byte armIterationStep = 0; // Init Step var
bool Btn5DPressed = false;
bool Btn5UPressed = false;
bool result; // E.g. debug info as returned by setArmControllerSetpoint
const int DEFAULT_ARM_VALUE 120
/ Main loop
while(true) {
// We are going to be using something similar to a toggle switch here
// to make sure we only accept each individual press.
if(vexRT[Btn5D] && !Btn5DPressed) {
Btn5DPressed = true; // Toggle Switch Code
// Setup a switch statement that will switch what this button does based
// what iteration step it is on and choose what to run based on that.
switch(armIterationStep)) {
case 0: // First time
result = setArmControllerSetpoint(120);
break;
case 1: // Second time
result = setArmControllerSetpoint(130);
break;
// case 2,3,4,5,6 etc
case default: // This will run if none of the other cases suit
// In this e.g we reset the arm to its default value
result = setArmControllerSetpoint(DEFAULT_ARM_VALUE);
break;
}
// Check that we sucessfully set the setpoint
if(result)
writeDebugStreamLine("Error setting setpoint for ARMPID with case ID: %d", armIterationSetup);
// Then we increase armIterationStep so that next time we will use the 2nd switch case.
armIterationStep++; // Add 1
}
// We will make it possible to press the button again once it has been released.
else if(!vexRT[Btn5D] && Btn5DPressed) {
Btn5DPressed = true;
}
// Reset button
if(vexRT[Btn5U] && !Btn5UPressed) {
Btn5UPressed = true; // Toggle Switch Code
// We reset armIterationStep to the default of 0
armIterationStep = 0;
// Reset to default value
result = setArmControllerSetpoint(DEFAULT_ARM_VALUE);
// Check that we sucessfully set the setpoint
if(result)
writeDebugStreamLine("Error setting setpoint for ARMPID with case ID: %d", armIterationSetup);
}
else if(!vexRT[Btn5U] && Btn5UPressed) {
Btn5UPressed = false;
}
}
In its simplest form the two programs above are just this pseudocode:
if someone presses Btn5D button:
use the value that we have to tell us what to do
Add 1 to the value so we do something different next time.
if someone presses the reset button (Btn5U):
reset the value to 0
use the value at 0 to tell us what to do (to reset position on the arm)
Best of luck!