Re: C Programming

johnoaxl (slong@interchange.ubc.ca) Sat, 02 Dec 2000 00:10:36 -0800


Date: Sat, 02 Dec 2000 00:10:36 -0800
From: johnoaxl <slong@interchange.ubc.ca>
Subject: Re: C Programming

William, for your interest, you could use the following FOR loop statement to elimate the 16 IF statements. for ( i=0; i < BITSHIFT; i++) { for ( n=0; n < BITSHIFT; n++ ) { spoke ( DPORT, C << i ); if ( speek(DPORT) & R << n ) { j = i + 4*n ; } } } This method will always start scaning/checking each number from column 1 row 1, row 2, row 4, and row 8, then shift to column 2 and check the number in each row and so on until the number matches up with the corresponding button pressed. It then returns a value j which is the number of the button pressed. Say, if you pressed 4, j will give 4. Although this method would save you some typing, it actually takes longer time to find the match of the button pressed. So, i suggest to stick with your 16 IF statements for later use, say for Lab 3. Seng Ong