Quantcast
Channel: Arduino Forum - Latest topics
Viewing all articles
Browse latest Browse all 15287

Questions about functions and datatypes

$
0
0

I have a few questions about functions. Can anyone tell me why when I remove the line ' Serial.println("About to return true for int");' from my function 'validateDataType', that the serial out freezes and does not get past the area where I have 'Serial.println("Freeze Before");' ?

Also, can anyone see any issues with my function itself?

function validateDataType

bool validateDataType(String dataType, char* data)
{
    if (dataType == "string")
    {
        for (int i = 0; i < strlen(data); i++)
        {
            if (isAlpha(data[i]))
            {
                // continue
            }
            else
            {
                return false;
            }
        }

        return true;
    }    
    else if (dataType == "int")
    {
        for (int i = 0; i < strlen(data); i++)
        {
            if (isDigit(data[i]))
            {
                // continue
            }
            else
            {
                return false;
            }
        }
        Serial.println("About to return true for int"); // This line stops it from freezing???

        return true;
    }
    else if (dataType == "float")
    {
        int decimalPointCount = 0;
        for (int i = 0; i < strlen(data); i++)
        {
            if (isDigit(data[i]))
            {
                // continue
            }
            else if (data[i] == '.')
            {
                decimalPointCount++;
                if (decimalPointCount > 1)
                {
                    return false;
                }
            }
            else
            {               
                return false;
            }
        }

        if (decimalPointCount != 1)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
}

Code to test/call function validateDataType


String stringToTest = "isthisanactualstring";
    bool isDataType = validateDataType("string", stringToTest.c_str()); // true is indeed 1, and false is 0.    
    Serial.print(stringToTest);
    Serial.print(" isDataType of (string): ");
    Serial.println(isDataType);
    Serial.println("");
    stringToTest = "isthisanactual1string";
    isDataType = validateDataType("string", stringToTest.c_str());
    Serial.print(stringToTest);
    Serial.print(" isDataType of (string): ");
    Serial.println(isDataType);
    Serial.println("");
    stringToTest = "isthisanactu*lstring";
    isDataType = validateDataType("string", stringToTest.c_str());
    Serial.print(stringToTest);
    Serial.print(" isDataType of (string): ");
    Serial.println(isDataType);
    Serial.println("");
    stringToTest = "isthisan actualstring";
    isDataType = validateDataType("string", stringToTest.c_str());
    Serial.print(stringToTest);
    Serial.print(" isDataType of (string): ");
    Serial.println(isDataType);
    Serial.println("");

    String intToTest = "123456789101112";
    Serial.println("Freeze Before");
    isDataType = validateDataType("int", intToTest.c_str()); // true is indeed 1, and false is 0.
    Serial.println("Freeze After");
    Serial.print(intToTest);
    Serial.print(" isDataType of (int): ");
    Serial.println(isDataType);
    Serial.println("");
    intToTest = "12345678910!112";
    isDataType = validateDataType("int", intToTest.c_str());
    Serial.print(intToTest);
    Serial.print(" isDataType of (int): ");
    Serial.println(isDataType);
    Serial.println("");
    intToTest = "123A56789101112";
    isDataType = validateDataType("int", intToTest.c_str());
    Serial.print(intToTest);
    Serial.print(" isDataType of (int): ");
    Serial.println(isDataType);
    Serial.println("");
    intToTest = "123456 789101112";
    isDataType = validateDataType("int", intToTest.c_str());
    Serial.print(intToTest);
    Serial.print(" isDataType of (int): ");
    Serial.println(isDataType);
    Serial.println("");

    String floatToTest = "12345.6789";
    isDataType = validateDataType("float", floatToTest.c_str()); // true is indeed 1, and false is 0.
     Serial.print(floatToTest);
    Serial.print(" isDataType of (float): ");
    Serial.println(isDataType);
    Serial.println("");
    floatToTest = "123456789";
    isDataType = validateDataType("float", floatToTest.c_str());
    Serial.print(floatToTest);
    Serial.print(" isDataType of (float): ");
    Serial.println(isDataType);
    Serial.println("");
    floatToTest = "12345.67.89";
    isDataType = validateDataType("float", floatToTest.c_str());
    Serial.print(floatToTest);
    Serial.print(" isDataType of (float): ");
    Serial.println(isDataType);
    Serial.println("");
    floatToTest = "12345.67A89";
    isDataType = validateDataType("float", floatToTest.c_str());
    Serial.print(floatToTest);
    Serial.print(" isDataType of (float): ");
    Serial.println(isDataType);
    Serial.println("");
    floatToTest = "12345*6789";
    isDataType = validateDataType("float", floatToTest.c_str());
    Serial.print(floatToTest);
    Serial.print(" isDataType of (float): ");
    Serial.println(isDataType);
    Serial.println("");
    floatToTest = "123 45.6789";
    isDataType = validateDataType("float", floatToTest.c_str());
    Serial.print(floatToTest);
    Serial.print(" isDataType of (float): ");
    Serial.println(isDataType);
    Serial.println("");

Lastly, I need to validate data saved in EEProm addresses, which is why I'm writing this function. I need to be able to read the values, determine if they are legitimate/expected and then send them over a Bluetooth module to a receiver. Although I still want critiques on my function, is there a better way to do this?

The code below with the function 'readFromEEPROMAll' gives this output

00:22:27.351 -> **
00:22:27.351 -> ***
00:22:27.384 ->
00:22:27.384 -> **
00:22:27.384 -> 3|1023|-�
00:22:27.384 -> ***
00:22:27.384 ->

function readFromEEPROMAll and code to call

    Serial.println(" ** ");
    readFromEEPROMAll();
    Serial.println(" *** ");

    Serial.println("");
    writeToEEProm("11", "Test");

    Serial.println(" ** ");
    Serial.println(readFromEEPROMAll());
    Serial.println(" *** ");
    Serial.println("");
    Serial.println("");


float Target;
String readFromEEPROMAll()
{
        int address = 11;
        int EEPromValue11 = ((EEPROM.read(address) << 8) + EEPROM.read(address + 1));
        Target = ((EEPROM.read(address) << 8) + EEPROM.read(address + 1));
        if (isnan(Target))
        {
            Serial.println("EEPromValue11 was empty");
        }

        address = 12;
        int EEPromValue12 = ((EEPROM.read(address) << 8) + EEPROM.read(address + 1));
        Target = ((EEPROM.read(address) << 8) + EEPROM.read(address + 1));
        if (isnan(Target))
        {
            Serial.println("EEPromValue12 was empty");
        }
   
        address = 13;
        int EEPromValue13 = ((EEPROM.read(address) << 8) + EEPROM.read(address + 1));
        Target = ((EEPROM.read(address) << 8) + EEPROM.read(address + 1));
        if (isnan(Target))
        {
            Serial.println("EEPromValue13 was empty");
        }
    
    

    String returnValue = String(EEPromValue11) + "|" + String(EEPromValue12) + "|" + String(EEPromValue13);
    return returnValue;
}

6 posts - 4 participants

Read full topic


Viewing all articles
Browse latest Browse all 15287

Trending Articles