We have two color sensors on a robot. We are trying to use the Robotc Graphical interface to determine when both color sensors are black, which requires a boolean “and” condition. From a programming perspective, this means that we could test the robot’s sensors for either both being black, or conversely, we could test when one turns black and the other is not (either case will work for us with the proper programming).
Can the above be done using Robotc Graphical interface? We have tried to nest repeat statements, while statements, and if statements, but all of these only accept a single condition. I know that we can do it using Robotc Text, but we are trying to use only the Graphical interface.
… which only works for if(), not for while()
If your students came to the complex condition point, the best course of action is to “convert to text” (a robotC menu item) and let them use more of the language.
You may need to help them with a missing semicolon or unbalanced parents from time to time, but the language underneath is the same. I understand the desire to stay in graphical, my students still start new programs with graphical first, but once they get over some threshold, they switch. Simple enough programs still stay graphical.
The alternative, if they need a loop, is to set up a loop outside and use something else to leave it.
For TeleOp, such an outer loop is pretty natural repeat(forever) {} construct anyway, but for autonomous, they need to be creative, since there is no break; and no variables in graphical.
For a simple program, my kids once used a timer as this “something else”, resetting it while the compound condition is still true:
resetTimer(T4);
while ( getTimer(T4, milliseconds) < 100) {
if (getColorGrayscale(colorDetector1) < 20) {
if (getColorGrayscale(colorDetector2) < 20) {
// Both dark
doSomething();
resetTimer(T4);
}
}
}
which would behave like:
while ( (getColorGrayscale(colorDetector1) < 20) && (getColorGrayscale(colorDetector2) < 20) {
// Both dark
doSomething();
}
Avoiding code duplication in the OR case is harder, even if you apply boolean algebra and negate all three conditions (a OR b == ^( ^a AND ^b)).
Nenik hit the nail on the head with his statement that " … for autonomous, they need to be creative, since there is no break; and no variables in graphical." I have enclosed the nested if statements in a repeat forever loop, and the program works; however, we are still looking at the way to “break” out of that repeat loop to continue with the autonomous mode. I think some sort of timer or distance measure might work inside the repeat loop, so we’ll play around with it more.
Although we can make the “leap” to text from graphical, it would be much easier for the kids (who are all 5th graders) to grasp the concepts with the graphical without having to worry about the syntax that is often the biggest problem with getting the text programming working right.