Array itself is the one index of an 2d array ie, strings[i][so_far[i]]

saj's picture
saj

Offline

Joined: Sep 28, 2018

Replies: 13
Array itself is the one index of an 2d array ie, strings[i][so_far[i]]
April 1, 2019 - 5:54pm

When I was terying to understand the tutorials from, https://circuitdigest.com/microcontroller-projects/gsm-interfacing-with-..., the below 2d array is found. 

if (strings[i][so_far[i]] == received)

but how this line being implemented in the code.

could anybody explain plaese??

tnx 

Bradyen's picture

Offline

Joined: Aug 14, 2018

Replies: 15
unsigned char so_far[6] = {0
April 2, 2019 - 7:33am
unsigned char so_far[6] = {0,0,0,0,0,0};
    unsigned const char lengths[6] = {2,12,5,4,6,6};
    unsigned const char* strings[6] = {"OK", "+CPIN: READY", "ERROR", "RING", "NO CARRIER", "Unlink"};
  unsigned const char responses[6] = {SIM900_OK, SIM900_READY, SIM900_FAIL, SIM900_RING, SIM900_NC, 

    

while (continue_loop) {
        received = _SIM900_getch();
        for (unsigned char i = 0; i < 6; i++) {
            if (strings[i][so_far[i]] == received) {
                so_far[i]++;
                if (so_far[i] == lengths[i]) {
                    response = responses[i];
                    continue_loop = 0;
                }
            } else {
                so_far[i] = 0;
            }
        }
    }
 
To understand the code you should not just look into the line, but rather the complete code block shown below. As you can see so_far, lengths, strings, responses these all are variable types that have been declared already.
The purpose of the code is to check what character is being received from the GSM module. The received information should be any one form the 6 string variable thats is "OK", "+CPIN: READY", "ERROR", "RING", "NO CARRIER" or "Unlink".
 
The length of these information is also defined with the variable lengths and the output that should be given to each of these information is also defined in the variable responces. Now coming to your line
 
strings[i][so_far[i]]
 
As you might have already noticed the information from the GSM module is read charector by charector, so to check if we have received any particular string we have to breakdown the string to that particular charector and compare it to the received charector. So when i=0
 
strings[0][so_far[0]] = "O"
 
 
 
Bradyen's picture

Offline

Joined: Aug 14, 2018

Replies: 15
unsigned char so_far[6] = {0
April 2, 2019 - 7:33am
unsigned char so_far[6] = {0,0,0,0,0,0};
    unsigned const char lengths[6] = {2,12,5,4,6,6};
    unsigned const char* strings[6] = {"OK", "+CPIN: READY", "ERROR", "RING", "NO CARRIER", "Unlink"};
  unsigned const char responses[6] = {SIM900_OK, SIM900_READY, SIM900_FAIL, SIM900_RING, SIM900_NC, 

    

while (continue_loop) {
        received = _SIM900_getch();
        for (unsigned char i = 0; i < 6; i++) {
            if (strings[i][so_far[i]] == received) {
                so_far[i]++;
                if (so_far[i] == lengths[i]) {
                    response = responses[i];
                    continue_loop = 0;
                }
            } else {
                so_far[i] = 0;
            }
        }
    }
 
To understand the code you should not just look into the line, but rather the complete code block shown below. As you can see so_far, lengths, strings, responses these all are variable types that have been declared already.
The purpose of the code is to check what character is being received from the GSM module. The received information should be any one form the 6 string variable thats is "OK", "+CPIN: READY", "ERROR", "RING", "NO CARRIER" or "Unlink".
 
The length of these information is also defined with the variable lengths and the output that should be given to each of these information is also defined in the variable responces. Now coming to your line
 
strings[i][so_far[i]]
 
As you might have already noticed the information from the GSM module is read charector by charector, so to check if we have received any particular string we have to breakdown the string to that particular charector and compare it to the received charector. So when i=0
 
strings[0][so_far[0]] = "O"