简体   繁体   中英

Corrupted value in Arduino array

The program's supposed to constantly listen for 433 MHz messages coming from soil moisture sensors in plant pots and decide whether or not to activate a pump based on those readings.
Additionally it's supposed to check wired water sensors while it's listening.

All 433 MHz Received Messages should be stored within the array sensor_data[i] .
At startup the positions 1 to NUM_Sensors (in this case 3) is filled with the constant int NO_DATA (500).
The Problem is, that for some reason I get corrupted Number in the array number 3:

Serial Print:

Wired Flower Pots Checked
All Sensors or Timeout reached
Array_Print: 500
Array_Print: 500
Array_Print: 30001

In this case the 30001 appears in the array with no specific reason (I guess).. there's no received message.
Here's the code changed to the minimum so that the error occurs:

#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();
//Receiver Setup for wireless soil moisture readings
unsigned long last_ground_check = 0;
const int NUM_SENSORS = 3;
const uint32_t SENSOR_TIMEOUT = 30000;
int sensor_data[NUM_SENSORS];
uint32_t last_message_time = 0;
uint32_t elapsed = 0;
float total_value = 0;
float real_value = 0;
int count = 0;
const int NO_DATA = 500;
boolean received = false;
//###################//
void setup()
{
    Serial.begin(9600);
    mySwitch.enableReceive(INT1); // Interrupt 2 = Pin 2
    delay(1500);
    Serial.println("<><><><><><><><><><><><>");
    Serial.println("          Start         ");
    Serial.println("<><><><><><><><><><><><>");
    for (int i = 0; i <= NUM_SENSORS; i++) {
        sensor_data[i] = NO_DATA;
    }
} // Setup END
void loop()
{
    if (received == false) {
        if (millis() - last_ground_check > 10000) {
            Serial.println("Checking Wired Flower Pot");
        }
        Serial.println("Wired Flower Pots Checked");
        last_ground_check = millis();
    }
    if (mySwitch.available()) { // Start whenever a 433 MHz Message is received
        received = true;
        double value = mySwitch.getReceivedValue();
        delay(1000);
        int sensor_id = 1;
        int sensor_value = 2;
        if (sensor_value >= 0 && sensor_value <= 100) {
            sensor_data[sensor_id] = sensor_value;
            last_message_time = millis();
            mySwitch.resetAvailable();
        }
    }
    byte sensors_reported = 0;
    for (int i = 0; i <= NUM_SENSORS; i++) {
        if (NO_DATA != sensor_data[i]) {
            sensors_reported += 1; // CODE Gets here because of corrupted Array Value although no message was received
        }
    }
    if (sensors_reported != 0) {
        uint32_t elapsed = millis() - last_message_time;
        if (NUM_SENSORS == sensors_reported || elapsed > SENSOR_TIMEOUT) {
            Serial.println("All Sensors or Timeout reached");

            for (int i = 1; i <= NUM_SENSORS; i++) {
                Serial.print("Array_Print: ");
                Serial.println(sensor_data[i]);
            }
            for (int i = 1; i <= NUM_SENSORS; i++) {
                if (sensor_data[i] < NO_DATA) {
                    count++;
                    total_value += sensor_data[i];
                }
            }

            real_value = total_value / count;
            Serial.print("Soil Moisture: ");
            Serial.println(real_value);
            if (real_value <= 20) {
                //Set Pump ON
            }
            for (int i = 1; i <= NUM_SENSORS; i++) {
                sensor_data[i] = NO_DATA;
            }
            total_value = 0;
            real_value = 0;
            sensors_reported = 0;
            count = 0;
            received = false;
            Serial.println("RESET #### RESET ####");
            delay(5000);
        }
    }
} //LOOP

You first allocate an array of size NUM_SENSORS=3 , and then you continue to use it as if it was of size 4.

Your array has 3 elements sensor_data[0] , [1] and [2] . Your loop condition i <= NUM_SENSORS results in accessing sensor_data[3] which is just some memory after your last array element. Even if you set sensor_data[3] in your setup, if the same memory is referenced by some other variable, the NO_DATA will be overwritten.

Index your loops over the data array from i = 0 to i < NUM_SENSORS .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM