The value of the integer

I want to ask is why my "value w"

if(dataReceived[i] == test1[i])//比對回傳值
            {
              w++;
            }

come's out the at 5 but the other's like x,y,z come's out at 3?

//apart of T
char test1[3]={'O','K','A'};
char test2[3]={'O','K','B'};
char test3[3]={'O','K','C'};
char test4[3]={'O','K','D'};
char failed[6]={'f','a','i','l','e','d'};

void getData() {

int a=5;
int i=0,x=0,y=0,z=0,f=0,w=0;
    if ( radio.available() )//如果通道裡有資料 
    {
        radio.read( &dataReceived, sizeof(dataReceived) );//讀取資料
        newData = true;
        for(i=0;i<a;i++)
        {
            if(dataReceived[i] == test1[i])//比對回傳值
            {
              w++;
            }
            if(dataReceived[i] == test2[i])
            {
              x++;
            }
            if(dataReceived[i] == test3[i])
            {
              y++; 
            }
            if(dataReceived[i] == test4[i])
            {
              z++;
            }
            if(dataReceived[i] == failed[i])
            {
              f++;
            }
         }
//apart of R
char replyData1[3]= {'O','K','A'}; 
char replyData2[3]= {'O','K','B'};
char replyData3[3]= {'O','K','C'};
char replyData4[3]= {'O','K','D'};
char replyDatafalse[6]={'f','a','i','l','e','d'};

void send() {
    if (newData == 1) {
        radio.stopListening();
            bool rslt;
            rslt = radio.write( &replyData1, sizeof(replyData1) );
        radio.startListening();

        Serial.print("Reply Sent: ");
        Serial.print("OK \n");
        
        if (rslt) {
            Serial.println("Received...OK\n");
        }
        else {
            Serial.println("Received...failed\n");
        }
        Serial.println();
        newData = 0;
    }
    else if(newData==2)
    {
      radio.stopListening();
            bool rslt;
            rslt = radio.write( &replyData2, sizeof(replyData2) );
        radio.startListening();

        Serial.print("Reply Sent: ");
        Serial.print("OK \n");
        

        if (rslt) {
            Serial.println("Received...OK\n");
        }
        else {
            Serial.println("Received...failed\n");
        }
        Serial.println();
        newData=0;
    }
    else if(newData==3)
    {
      radio.stopListening();
            bool rslt;
            rslt = radio.write( &replyData3, sizeof(replyData3) );
        radio.startListening();

        Serial.print("Reply Sent: ");
        Serial.print("OK \n");

        if (rslt) {
            Serial.println("Received...OK\n");
        }
        else {
            Serial.println("Received...failed\n");
        }
        Serial.println();
        newData=0;
    }
    else if(newData==4)
    {
      radio.stopListening();
            bool rslt;
            rslt = radio.write( &replyData4, sizeof(replyData4) );
        radio.startListening();

        Serial.print("Reply Sent: ");
        Serial.print("OK \n");

        if (rslt) {
            Serial.println("Received...OK\n");
        }
        else {
            Serial.println("Received...failed\n");
        }
        Serial.println();
        newData=0;
    }
    else if(newData==5)
    {
      radio.stopListening();
            bool rslt;
            rslt = radio.write( &replyDatafalse, sizeof(replyDatafalse) );
        radio.startListening();

        Serial.print("Reply Sent: ");
        Serial.print("failed \n");

        if (rslt) {
            Serial.println("Received...OK\n");
        }
        else {
            Serial.println("Received...failed\n");
        }
        Serial.println();
        newData=0;
    }
}

How long is test1?
test2?
test3?
test4?

1 Like

char test1[3]={'O','K','A'};
char test2[3]={'O','K','B'};
char test3[3]={'O','K','C'};
char test4[3]={'O','K','D'};

It was a rhetorical question.
I know how long they are - it is really obvious.

I was hoping to get you to think about the problem.

OK,thank's

I think if I use it like this,the w,x,y,z may have to be 5 but why the x,y,z still be 3?

You have three boxes in front of you.
Each has a different kind of chocolate bar in it
What type of bar is in the fourth and fifth boxes?

Uh.......I don't know? random?

I changed 'a' to 3 it's OK, but the 'failed' will only compare the first 3 letters

Failed has six letters

why not just properly use strings?

consider the following which attempt to simulates receiving data over the radio using the serial interface.

it's not obvious when radio.read() returns. how does it recognize the last char?

in the code below, readData() which attempts to simulate radio.read() has a 2 msec delay so that Serial.available() is called after the next char is received and readData() continues to read chars until the last char

const char *str [] = {
    "OKA",
    "OKB",
    "OKC",
    "OKD",
};
#define N_STR   (sizeof(str)/sizeof(char*))

char     s [80];

char *
readData() {
    static char buf [80];
    unsigned i = 0;

    while (Serial.available () && i < (sizeof(buf)-1))  {
        buf [i++] = Serial.read ();
        delay (2);
    }
    buf [i] = '\0';

    return buf;
};

int
chkData (
    char *s )
{
    for (unsigned n = 0; n < N_STR; n++)
        if (! strncmp (s, str [n], strlen (str[n])))
            return n;

    return -1;
}

void
loop ()
{
    if (Serial.available ())  {
        int n = chkData ( readData ());

        if (0 <= n)
            Serial.println (str [n]);
        else
            Serial.println ("unknown");
    }
}

void
setup ()
{
    Serial.begin (9600);
}
1 Like

Maybe random, but more likely you're looking at the boxes on the desk of the next guy along.