-1

Is there a way to shorten it D==1 && C==1 && B==0 && A==0 to just a name like Key1? as I have many different set of those that i wished to make

#include<xc.h>


#define Key1    (D==1 && C==1 && B==0 && A==0)

char K1[]       =   "Key 1 was pressed";

void main() {

    unsigned char i;
    while(DA==1){


      if(Key1){  
         sendfunction(0b10000000);

                for (i=0; K1[i]!=0; i++){
                sendtext(K1[i]);
                }   //message
       }//for loop

    }

/*But if I put in this way, it works 
and it will display "Key 1 was pressed"
*/

 if((D==1 && C==1 && B==0 && A==0){  
         sendfunction(0b10000000);

                for (i=0; K1[i]!=0; i++){
                sendtext(K1[i]);
                }   //message
       }//for loop

Kelvin Poh
  • 11
  • 1
  • 1
    Does this even compile? Where are any variables declared? Please sort out the indentation – Ed Heal Jul 20 '19 at 12:14
  • Can you be more specific about what exactly is the problem with your code? Is it not compiling? Is it doing something unexpected? etc... – Bob Jul 20 '19 at 12:17
  • Hello, I'm sorry to mislead you but above is not the code. its like some sort of a logic about how i want to attempt to do. but i'm not sure how to approach it. – Kelvin Poh Jul 20 '19 at 12:18
  • Have a look into `select()`. It can be used to check for availability of data to be read from a stream. It can be used along with a time-out. – alk Jul 20 '19 at 12:19
  • I would start with using meaningful variable names? Also - Is `pwd` and `password` the same thing? – Ed Heal Jul 20 '19 at 12:20
  • 3
    The posted psuedocode is very confusing and most of it doesn't make sense. Can you clean it up? – Spikatrix Jul 20 '19 at 12:21
  • Although a C++ answer, its function `inputAvailable()` is expected to work in C as well: https://stackoverflow.com/a/1280659/694576 – alk Jul 20 '19 at 12:27
  • 1
    The `for` loop shown — `for(a=60; a<0; a--){` is never going to execute the body of the loop because `60` is greater than zero, so the condition is not true when it is first tested. Presumably you should change the `<` to `>` or perhaps `>=`. – Jonathan Leffler Jul 20 '19 at 14:46

2 Answers2

1

This is how I got it to work on Visual Studio 2019. From what I understand you are asking how to make a macro to substitute an if statement.

#define KEY1(A,B,C,D)   (A==0 && B==0 && C==1 && D==1) ? 1 : 0

int main(void)
{
    int a = 0, b = 0, c = 1, d = 1;

    if (KEY1(a, b, c, d))
        printf("Key1 pressed\n");

    printf("%d\n", KEY1(0, 0, 0, 0));
    printf("%d\n", KEY1(0, 0, 1, 1));

    return 0;
}
RobotMan
  • 605
  • 1
  • 5
  • 10
0

I'm assuming that your A, B, C, D are some kind of input lines from a keyboard and you want to check which key was pressed.

If that is the case, and they can only take values 0 and 1 I think it is clearer if you combine these bits into a single value and then just check with a constant:

#define KEY_1 0b1100
#define KEY_2 0b1010
#define KEY_3 0b1001
//...

unsigned char key = (A << 3) | (B << 2) | (C << 1) | D; // or the other way around
//now you can even use a switch
switch (key)
{
    case KEY_1:
        send_text1(...);
        break;
    case KEY_2:
        send_text2(...);
        break;
    case KEY_3:
        send_text3(...);
        break;
    //...
}
rodrigo
  • 94,151
  • 12
  • 143
  • 190
  • Hello rodrigo, thank you for your help. But what does it mean to shift the bits. (A<<3) so on. – Kelvin Poh Jul 21 '19 at 11:35
  • @KelvinPoh: I'm guessing that every value such as `A` is either `1` or `0`, so you shift each value a different number of bits, `A` is shifted 3 bits, `B` is shifted 2 bits, and so on. Thus, `A` becomes `0bA000`, `B` becomes `0b0B00` etc. Then in the key constants each value is mapped to one combination of values `0bABCD`. – rodrigo Jul 21 '19 at 14:14