Software Theory
Dec 6th, 2012 by munica
There are a variety of possible methods for interpreting user input. Using a number of complex conditional If statements could work for some cases. However, they are computationally expensive and scale poorly. As a result two methods are used that are based around the same key concept which avoids unnecessary If statements, function calls and comparison operations. One is usable in all situations, but may not be as convenient as the other which is used in only certain situations.
For our device there are 12 inputs that are being read and seven possible states, one for each of the six answers and another for any incorrect combination of inputs. Therefore six Ifs (and one else in the generic case that the user has incorrect input) are required. For every If all 12 inputs must be checked. For small scale project with a small number of inputs, doing this way will work, but it scales quite poorly. Instead if repeatedly reading the state of the inputs, the two registers that hold the state of the pins are read to form a two byte number. This can scale up as necessary, reading multiple bytes at a time. This number is XORed with the previous known state, so the software will only update if there is a change in the pin states. At this point the two methods diverge based on what how a correct answer should be determined. In our case, each bit in the two-byte number corresponded to a value held in an array. This value corresponds to the fraction the students see on the board. Due to possible floating point arithmetic errors, instead of storing fractions and integer is used. It is the quotient of the fraction and the board’s lowest common denominator. Every fraction we used was the result of an integer divided by six. If the bit corresponding to a value is high, it is added to a total. By combining positive and negative numbers, the correct answer is produced when the choice inputs sum together with one or two answer inputs resulting in a total of zero. This method is used because it enables a variety of different combinations of inputs to be used as a correct answer, just as long as the total is correct.
However this method will not work for many situations in which math is not the subject. If there is only one possible correct combination for an answer then this method is preferred regardless of subject. The bytes corresponding to the input pins can simply be checked against bytes that represent the correct configuration. While this results in one If statement for every answer, there is only one comparison occurring in each if statement. In situations where there are multiple correct combinations, an additional comparison is needed for each possible correct combination. This can have some scaling issues, but it is far better than reading and comparing each pin dozens if not hundreds of times. Shortcuts can be created if the correct combinations are sequential however.