我的Arduino Nano发送错误的(传感器)值

时间:2014-10-29 08:05:37

标签: arduino xbee

我想开发一个无线VOC传感器系统。我使用了TGS2602Arduino NanoXBee Pro S1。

这是我的代码:

int value ;
const int gasSensor = A0;

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

void loop() {
    int value = analogRead (gasSensor);

    float volt = (value/1023.0)*5.0 ;   // Rechne die von ADC ausgegebenen Werten in Volt um
    float Rs1 = 10*(5-volt)/volt;       // Rechne den Widerstandswert
    //Serial.print("40B7DEF1");         // Sensorname
    //Serial.print("\t");               // Tabulator

    // Kalibrierungsarray der Länge N = 17
    float Rs [17] = {57.84260516,  2.200081335,  1.606762875,  1.178596714,  0.944910617,     0.865628395,  0.764262648,  0.687566798,  0.626992561,  0.567101091,  0.544815465,  0.496850952,  0.474860336,  0.474860336,  0.442046642,  0.423905489,  0.402219139};
    float ppmValue [17] = {0,  5,  10,  15,  20,  24,  28.25,  32.5,  37.75,  43.25,  48.75,  53.25,  57.75,  62.50,  67.50,  75.5,  77.5};

    if (Rs1 >= Rs [0])
    {
        float ppm = ppmValue [0];
        //Serial.print ("ppm: ");
        Serial.print("1");            // Sensorname
        Serial.print("\t");           // Tabulator

        Serial.println(ppm);
        delay(30000);
    }

    // Interpolation: ppm [0;5]
    if ( Rs[1] < Rs1 && Rs1 < Rs[0]){
        float ppm = ppmValue [0] + ((-Rs1 + Rs [0])/(Rs[0] - Rs [1] ))*(ppmValue [1] - ppmValue[0]);
        //Serial.print ("ppm: ");
        Serial.print("1");            // Sensorname
        Serial.print("\t");           // Tabulator

        Serial.println (ppm);
        delay(30000);
    }

    // Interpolation: ppm [5;10]
    else if ( Rs[2] < Rs1 && Rs1 < Rs[1]){
        float ppm = ppmValue [1] + ((-Rs1 + Rs [1])/(Rs[1] - Rs [2] ))*(ppmValue [2] - ppmValue[1]);

        //PPM Ausgabe
        //Serial.print ("ppm: ");
        Serial.print("1");            // Sensorname
        Serial.print("\t");           // Tabulator

        Serial.println (ppm);
        delay(30000);
    }

    // Interpolation: ppm [10;15]
    else if ( Rs[3] < Rs1 && Rs1 < Rs[2]){
        float ppm = ppmValue [2] + ((-Rs1 + Rs [2])/(Rs[2] - Rs [3] ))*(ppmValue [3] - ppmValue[2]);

        //PPM Ausgabe
        //Serial.print ("ppm: ");
        Serial.print("1");            // Sensorname
        Serial.print("\t");           // Tabulator

        Serial.println (ppm);
        delay(30000);
    }

    // Interpolation: ppm [15;24]
    else if ( Rs[4] < Rs1 && Rs1 < Rs[3]){
        float ppm = ppmValue [3] + ((-Rs1 + Rs [3])/(Rs[3] - Rs [4] ))*(ppmValue [4] - ppmValue[3]);

        //PPM Ausgabe
        //Serial.print ("ppm: ");
        Serial.print("1");            // Sensorname
        Serial.print("\t");           // Tabulator

        Serial.println (ppm);
        delay(30000);
    }

    // Interpolation: ppm [24;28.25]
    else if ( Rs[5] < Rs1 && Rs1 < Rs[4]){
        float ppm = ppmValue [4] + ((-Rs1 + Rs [4])/(Rs[4] - Rs [5] ))*(ppmValue [5] - ppmValue[4]);

        //PPM Ausgabe
        //Serial.print ("ppm: ");
        Serial.print("1");            // Sensorname
        Serial.print("\t");           // Tabulator

        Serial.println (ppm);
        delay(30000);
    }

    // Interpolation: ppm [28.25;32.5]
    else if ( Rs[6] < Rs1 && Rs1 < Rs[5]){
        float ppm = ppmValue [5] + ((-Rs1 + Rs [5])/(Rs[5] - Rs [6] ))*(ppmValue [6] - ppmValue[5]);
        //PPM Ausgabe
        //Serial.print ("ppm: ");
        Serial.print("1");            // Sensorname
        Serial.print("\t");           // Tabulator

        Serial.println (ppm);
        delay(30000);
    }

    // Interpolation: ppm [32.5;37.75]
    else  if ( Rs[7] < Rs1 && Rs1 < Rs[6]){
        float  ppm = ppmValue [6] + ((-Rs1 + Rs [6])/(Rs[6] - Rs [7] ))*(ppmValue [7] - ppmValue[6]);

        //PPM Ausgabe
        //Serial.print ("ppm: ");
        Serial.print("1");            // Sensorname
        Serial.print("\t");           // Tabulator

        Serial.println (ppm);
        delay(30000);
    }

    // Interpolation: ppm [37.75;43.25]
    else if ( Rs[8] < Rs1 && Rs1 < Rs[7]){
        float  ppm = ppmValue [7] + ((-Rs1 + Rs [7])/(Rs[7] - Rs [8] ))*(ppmValue [8] - ppmValue[7]);

        //PPM Ausgabe
        //Serial.print ("ppm: ");
        Serial.print("1");            // Sensorname
        Serial.print("\t");           // Tabulator

        Serial.println (ppm);
        delay(30000);
    }

    // Interpolation: ppm [43.25;48.75]
    else if ( Rs[9] < Rs1 && Rs1 < Rs[8]){
        float ppm = ppmValue [8] + ((-Rs1 + Rs [8])/(Rs[8] - Rs [9] ))*(ppmValue [9] - ppmValue[8]);

        //PPM Ausgabe
        //Serial.print ("ppm: ");
        Serial.print("1");            // Sensorname
        Serial.print("\t");           // Tabulator

        Serial.println (ppm);
        delay(30000);
    }
    // Interpolation: ppm [48.75;53.25]
    else  if ( Rs[10] < Rs1 && Rs1 < Rs[9]){
        float  ppm = ppmValue [9] + ((-Rs1 + Rs [9])/(Rs[9] - Rs [10] ))*(ppmValue [10] - ppmValue[9]);

        //PPM Ausgabe
        //Serial.print ("ppm: ");
        Serial.print("1");            // Sensorname
        Serial.print("\t");           // Tabulator

        Serial.println (ppm);
        delay(30000);
    }

    // Interpolation: ppm [53.25;57.75]
    else if ( Rs[11] < Rs1 && Rs1 < Rs[10]){
        float ppm = ppmValue [10] + ((-Rs1 + Rs [10])/(Rs[10] - Rs [11] ))*(ppmValue [11] - ppmValue[10]);

        //PPM Ausgabe
        //Serial.print ("ppm: ");
        Serial.print("1");            // Sensorname
        Serial.print("\t");           // Tabulator

        Serial.println (ppm);
        delay(30000);
    }

    // Interpolation: ppm [57.75;62.5]
    else if ( Rs[12] < Rs1 && Rs1 < Rs[11]){
        float  ppm = ppmValue [11] + ((-Rs1 + Rs [11])/(Rs[11] - Rs [12] ))*(ppmValue [12] - ppmValue[11]);

        //PPM Ausgabe
        //Serial.print ("ppm: ");
        Serial.print("1");            // Sensorname
        Serial.print("\t");           // Tabulator

        Serial.println (ppm);
        delay(30000);
    }

    // Interpolation: ppm [62.5;67.5]
    else  if ( Rs[13] < Rs1 && Rs1 < Rs[12]){
        float  ppm = ppmValue [12] + ((-Rs1 + Rs [12])/(Rs[12] - Rs [13] ))*(ppmValue [13] - ppmValue[12]);
        //PPM Ausgabe
        //Serial.print ("ppm: ");
        Serial.print("1");            // Sensorname
        Serial.print("\t");           // Tabulator

        Serial.println (ppm);
        delay(30000);
    }

    // Interpolation: ppm [67.5;75.5]
    else  if ( Rs[14] < Rs1 && Rs1 < Rs[13]){
        float ppm = ppmValue [13] + ((-Rs1 + Rs [13])/(Rs[13] - Rs [14] ))*(ppmValue [14] - ppmValue[13]);

        //PPM Ausgabe
        //Serial.print ("ppm: ");
        Serial.print("1");            // Sensorname
        Serial.print("\t");           // Tabulator

        Serial.println (ppm);
        delay(30000);
    }

    // Interpolation: ppm [75.5;77.5]
    else if( Rs[15] < Rs1 && Rs1 < Rs[14]){
        float ppm = ppmValue [14] + ((-Rs1 + Rs [14])/(Rs[14] - Rs [15] ))*(ppmValue [15] - ppmValue[14]);

        //PPM Ausgabe
        //Serial.print ("ppm: ");
        Serial.print("1");            // Sensorname
        Serial.print("\t");           // Tabulator

        Serial.println (ppm);
        delay(30000);
    }

    else if( Rs[16] < Rs1 && Rs1 < Rs[15]){
        float ppm = ppmValue [15] + ((-Rs1 + Rs [15])/(Rs[15] - Rs [16] ))*(ppmValue [16] - ppmValue[15]);

        //PPM Ausgabe
        //Serial.print ("ppm: ");
        Serial.print("1");            // Sensorname
        Serial.print("\t");           // Tabulator

        Serial.println (ppm);
        delay(30000);}

    else if (Rs1 >= Rs [16])
    {
        float ppm = ppmValue [16];

        //Serial.print ("ppm: ");
        Serial.print("1");            // Sensorname
        Serial.print("\t");           // Tabulator

        Serial.println (ppm);
        delay(30000);
    }

    else if (Rs1 < Rs [16])
    {
        float ppm = 80;

        //Serial.print ("ppm: ");
        Serial.print("1");            // Sensorname
        Serial.print("\t");           // Tabulator

        Serial.println (ppm);
        delay(30000);
    }

    delay(3000);
}

我不知道我做错了什么,但我得到的价值都是错的。似乎这些值不是来自模拟引脚。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

您的代码包含了所有&gt;和&lt;向后。因此,唯一运行的代码是:

else if (Rs1 < Rs [16])

当你在这里时,请允许我帮你改变一下代码。这实际上是以长形式编写的,以这种方式调试/编辑任何代码需要很长时间。

将所有ifs替换为:

for ( int i = 0; i <= 16; i++ ) {
    if (Rs1 < Rs [i]) {
        if (i = 0) {
            float ppm = ppmValue [i];
        }
        else{
            float ppm = ppmValue [i-1] + ((-Rs1 + Rs [i-1])/(Rs[i-1] - Rs [i] ))*(ppmValue [i] - ppmValue[i-1]);
        }
        //Serial.print ("ppm: ");
        Serial.print("1");
        // Sensorname
        Serial.print("\t");
        // Tabulator
        Serial.println (ppm);
        delay(30000);
        break;
    }
    else if (Rs1 > Rs [16]) {

        float ppm = 80;
        //Serial.print ("ppm: ");
        Serial.print("1");
        // Sensorname
        Serial.print("\t");
        // Tabulator
        Serial.println (ppm);
        delay(30000);
    }
}

希望这有帮助!