While loop is resetting the arduino

In my program whenever it goes upto this

stay = false;

then arduino just start again from begining.
Why is it happening? I just go along the code for hours and hours but then also I didn't found anything in it, will you please say what's / where is the problem.

This is my code.

#include <SoftwareSerial.h>

SoftwareSerial ESPserial(4, 5); // RX | TX
SoftwareSerial BTserial(2, 3);  // RX | TX

char *IP[] = {"38E6,A,833A4", "CF3,46,979A3A", "9C85,66,29225C"};
char *ROLL[] = {"021", "030", "040"};

int attend[] = {};
// int absent[] = {};
char que[30] = "";

void resetBT();
void query(char Ip[]);
void isPresent(int k);

int n = sizeof(IP) / sizeof(IP[0]);
// n = sizeof(IP) / sizeof(IP[0]);
int mid;
int loops = 1; // waiting for absent numbers
char buf = ' ';
bool isDataReady = false;
bool stay = true;

void setup()
{
    Serial.begin(38400);
    Serial.println("Initializing...");
    pinMode(8, OUTPUT);
    pinMode(9, OUTPUT);

    digitalWrite(9, HIGH);
    digitalWrite(8, HIGH);
    delay(500);

    BTserial.begin(9600);
    delay(500);
    //    ESPserial.begin(1000000);
    //    delay(500);
    //    resetBT();
    Serial.println("Intialization completed, Scanning started.");
}

void loop()
{
    if (isDataReady)
    {
        // Send the data after the scanning is completed.
        for (int k = 0; k < n; k++)
        {
            Serial.print("PRN: 211102");
            Serial.print(ROLL[k]);
            Serial.print(", Attendance: ");
            if (attend[k] == 1)
                Serial.println("Present.");
            else
                Serial.println("Absent.");
        }
        isDataReady = false;
        Serial.println("WorkDOne.");
        delay(99999);
    }
    else
    {
        mid = sizeof(attend) / sizeof(attend[0]);
        if (mid == 0)
        {
            Serial.println("First Scan started.");
            // For loop to do first scan.
            for (int k = 0; k < n; k++)
            {
                query(IP[k]);
                BTserial.write(que);
                Serial.print("Attendance query: ");
                Serial.println(que);
                que[0] = '\0';
                buf = ' ';
                while (stay)
                {
                    if (BTserial.available())
                    {
                        buf = BTserial.read();
                        //                        Serial.println(buf);
                    }
                    else
                    {
                        if (buf == 'K')
                        {
                            //                            resetBT();
                            delay(1000);
                            attend[k] = 1;
                            //                            break;
                            stay = false;
                        }
                        else
                        {
                            if (buf == 'L')
                            {
                                //                                resetBT();
                                delay(1000);
                                attend[k] = 0;
                                //                                break;
                                stay = false;
                            }
                        }
                    }
                }
                stay = true;
                Serial.println("One");
                delay(3000);
            }
            Serial.println("First Scan ended.");
        }
        else
        {
            // While loop.
            while (loops < 4)
            {
                // Scan for absent students.
                for (int k = 0; k < n; k++)
                {
                    if (attend[k] == 0)
                    {
                        query(IP[k]);
                        BTserial.write(que);
                        que[0] = '\0';
                        isPresent(k);
                    }
                }
                Serial.print("Loop on going: ");
                Serial.println(loops);
                loops++;
                delay(1000);
            }
            isDataReady = true;
        }
    }
}

// Funcs
void resetBT()
{
    digitalWrite(9, LOW);
    digitalWrite(8, LOW);
    delay(100);
    digitalWrite(9, HIGH);
    delay(500);
    digitalWrite(8, HIGH);
}

void query(char Ip[])
{
    int i = 0, j = 0;
    char reqP[] = "AT+RNAME?";
    char reqS[] = "\r\n";

    // Insert the first string in the new string
    while (reqP[i] != '\0')
    {
        que[j] = reqP[i];
        i++;
        j++;
    }

    // Insert the second string in the new string
    i = 0;
    while (Ip[i] != '\0')
    {
        que[j] = Ip[i];
        i++;
        j++;
    }

    // Insert the third string in the new string
    i = 0;
    while (reqS[i] != '\0')
    {
        que[j] = reqS[i];
        i++;
        j++;
    }
    que[j] = '\0';
}

// int iter = 0;
void isPresent(int k)
{
    buf = ' ';
    while (1)
    {
        if (BTserial.available())
        {
            buf = BTserial.read();
            Serial.println(buf);
        }
        else
        {
            if (buf == 'K')
            {
                resetBT();
                delay(1000);
                //                Serial.println("Present");
                attend[k] = 1;
                return 0;
            }
            else
            {
                if (buf == 'L')
                {
                    resetBT();
                    delay(1000);
                    //                    Serial.println("Absent");
                    attend[k] = 0;
                    // absent[iter] = k;
                    // iter++;
                    return 0;
                }
            }
        }
    }
    //    return 0;
}

this array does not have any size...

probably something like this is needed

char *IP[] = {"38E6,A,833A4", "CF3,46,979A3A", "9C85,66,29225C"};
char *ROLL[] = {"021", "030", "040"};
const size_t n = sizeof(IP) / sizeof(IP[0]);
int attend[n];

Okay
Got it.
Let me try.

okay

have fun!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.