Need guidance for efficient and compact code

I am learning to code using arduino Ide. I have following code which compile fine for arduino but is there any better way of coding this so it is more time efficient and compact code?

Predefined sensor type table is below:
volatage = 6, temperature = 16, humidity= 26,
pressure= 36, light= 46, OpenClose = 56, distance = 66,
motion = 76, wind = 86, rain = 96 etc.

uint8_t sensorType[6]; 

uint8_t sensorData[6];  

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

void loop() {

sensorValues();
delay(500);

}    

void sensorValues() {

  float voltage = ESP.getVcc() / (float)1023 * 50; 
 
  sensorData[0] = 1;
  sensorData[1] = voltage;
  
 //Values received from sensors replaces last 4 random values of sensorData array.

    
    if (sensorType[2] == 16) 
    {

      sensorType[3] = 26;
      sensorType[4] = 36;
      Serial.println();
      Serial.println("Activate function for BME280");
      sensorData[2] = random(90);         //temperature;
      sensorData[3] = random(100);        //humidity;
      sensorData[4] = random(1024) / 4;   //pressure;
    
    } else if (sensorType[2] == 46) {

      sensorData[2] = random(100);  // lightlevel
      Serial.println("Activate function for APDS9960 Light Sensor");
      Serial.println();
    
    } else if (sensorType[2] == 56) {

      sensorData[2] = random(1);  // 0 or 1
      Serial.println("Activate function for OpenClose sensor");
      Serial.println();
    } else if (sensorType[2] == 66) {

      sensorData[2] = random(256);  // 0 to 256 cm.
      Serial.println("Activate function for HCSR04 distance sensor");
      Serial.println();
    
    } else if (sensorType[2] == 76) {

      sensorData[2] = random(1);  // 0 or 1
      Serial.println("Activate function for motion (rcwl-0516 or hc-sr505) sensor");
      Serial.println();
      
    } else if (sensorType[2] == 86) {

      sensorData[2] = random(90);  
      Serial.println("Activate function for wind  sensor");
      Serial.println();

    } else if (sensorType[2] == 96) {

      sensorData[2] = random(90);  
      Serial.println("Activate function for rain sensor");
      Serial.println();
    }

    if (sensorType[3] == 46) {

      sensorData[3] = random(100);  
      Serial.println("Activate function for APDS9960 Light Sensor");
      Serial.println();
    
    } else if (sensorType[3] == 56) {

      sensorData[3] = random(1);  // 0 or 1
      Serial.println("Activate function for OpenClose sensor");
      Serial.println();
    } else if (sensorType[3] == 66) {

      sensorData[3] = random(256);  // 0 to 256 cm.
      Serial.println("Activate function for HCSR04 distance sensor");
      Serial.println();
    
    } else if (sensorType[3] == 76) {

      sensorData[3] = random(1);  // 0 or 1
      Serial.println("Activate function for motion (rcwl-0516 or hc-sr505) sensor");
      Serial.println();
      
    } else if (sensorType[3] == 86) {

      sensorData[3] = random(90);  
      Serial.println("Activate function for wind sensor");
      Serial.println();

    } else if (sensorType[3] == 96) {

      sensorData[3] = random(256);  
      Serial.println("Activate function for rain sensor");
      Serial.println();
    }

    if (sensorType[4] == 46) {

      sensorData[4] = random(100);  
      Serial.println("Activate function for APDS9960 Light Sensor");
      Serial.println();
    
    } else if (sensorType[4] == 56) {

      sensorData[4] = random(1);  // 0 or 1
      Serial.println("Activate function for OpenClose sensor");
      Serial.println();
    } else if (sensorType[4] == 66) {

      sensorData[4] = random(256);  // 0 to 256 cm.
      Serial.println("Activate function for HCSR04 distance sensor");
      Serial.println();
    
    } else if (sensorType[4] == 76) {

      sensorData[4] = random(1);  // 0 or 1
      Serial.println("Activate function for motion (rcwl-0516 or hc-sr505) sensor");
      Serial.println();
      
    } else if (sensorType[4] == 86) {

      sensorData[4] = random(90);  
      Serial.println("Activate function for wind sensor");
      Serial.println();

    } else if (sensorType[4] == 96) {

      sensorData[4] = random(90);  
      Serial.println("Activate function for rain sensor");
      Serial.println();
    }

    if (sensorType[5] == 46) {

      sensorData[5] = random(100);  
      Serial.println("Activate function for APDS9960 Light Sensor");
      Serial.println();
    
    } else if (sensorType[5] == 56) {

      sensorData[5] = random(1);  // 0 or 1
      Serial.println("Activate function for OpenClose sensor");
      Serial.println();
    } else if (sensorType[5] == 66) {

      sensorData[5] = random(256);  // 0 to 256 cm.
      Serial.println("Activate function for HCSR04 distance sensor");
      Serial.println();
    
    } else if (sensorType[5] == 76) {

      sensorData[5] = random(1);  // 0 or 1
      Serial.println("Activate function for motion (rcwl-0516 or hc-sr505) sensor");
      Serial.println();
      
    } else if (sensorType[5] == 86) {

      sensorData[5] = random(90);  
      Serial.println("Activate function for wind sensor");
      Serial.println();

    } else if (sensorType[5] == 96) {

      sensorData[5] = random(90);  
      Serial.println("Activate function for rain sensor");
      Serial.println();
    }
 }

Thanks.

In your sensorValues() function you are populating the sensorData based on sensor types within the sensorType array. It is confusing because you test sensor types of the 2nd thru 5th elements of the sensorType array and populate sensorData based on the sensor type, but you will overwrite data from a previous element in the sensorType array. Also you don't process elements 0 and 1 of the sensorType array. I can't tell what the intent of processing multiple sensor types while overwriting sensor data.

I don't know about faster but you could make it much more readable using an enum for the sensor type and classes (or structs) to define the sensor type/data relationship.

uint8_t sensorData[6];   //an array of ints
  float voltage = ESP.getVcc() / (float)1023 * 50;
 
  sensorData[0] = 1;
  sensorData[1] = voltage;

Saving a float into an array of ints ?
The only value ever assigned to sensorData[0] is 1 so is it needed ?

Since I can’t pretend to know what you intend maybe this will get you started in terms of crafting more readable code. The code could be made even more readable by using classes and inheritance. I didn’t have much time to spend on the code I just wanted to illustrate the use of struct, enum, and iterating the arrays of structs.

//volatage = 6, temperature = 16, humidity= 26,
//pressure= 36, light= 46, OpenClose = 56, distance = 66,
//motion = 76, wind = 86, rain = 96 etc.

enum sensorTypeEnum
{
  VOLTAGE = 6,
  TEMPERATURE = 16,
  HUMIDITY = 26,
  PRESSURE = 36,
  LIGHT = 46,
  OPEN_CLOSE = 56,
  DISTANCE = 66,
  MOTION = 76,
  WIND = 86,
  RAIN = 96
};

struct sensorInfo
{
  sensorTypeEnum sensorType; 
  uint8_t sensorData[6];  
};

sensorInfo sensorArray[6];

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

void loop() {


}    

void sensorValues() 
{
  for (int i = 0; i < 6; i++)
  {
    switch(sensorArray[i].sensorType)
    {
      case VOLTAGE:
        // populate sensor data
        sensorArray[i].sensorData[0] = 1;
        break;
      case TEMPERATURE:
        // populate sensor data
        sensorArray[i].sensorData[0] = 1;
        break;
      case HUMIDITY:
        // populate sensor data
        sensorArray[i].sensorData[0] = 1;
        break;
      case PRESSURE:
        // populate sensor data
        sensorArray[i].sensorData[0] = 1;
        break;
      case LIGHT:
        // populate sensor data
        sensorArray[i].sensorData[0] = 1;
        break;
      case OPEN_CLOSE:
        // populate sensor data
        sensorArray[i].sensorData[0] = 1;
        break;
      case DISTANCE:
        // populate sensor data
        sensorArray[i].sensorData[0] = 1;
        break;
      case MOTION:
        // populate sensor data
        sensorArray[i].sensorData[0] = 1;
        break;
      case WIND:
        // populate sensor data
        sensorArray[i].sensorData[0] = 1;
        break;
      case RAIN:
        // populate sensor data
        sensorArray[i].sensorData[0] = 1;
        break;
      } // end switch
   } // end for
}

@ToddL1962 & @UKHeliBob,

Thank you for your help. In sensorType array my intent is to change last four value of sensor types as first & second value always will be same which I will use for device id & battery monitor (voltage) sensor. Last four values willchange dynamically using MQTT message received by this device.I want to activate or deactivate sensor functions based on sensor type chosen in last four sensor types in sensorType array.

I did not link my whole code as it could be confusing.But if anyone willing to help in creating low power sensor network using ESP8266 devices my code is at:

https://github.com/happytm/BatteryNode/blob/master/ProbeSender.ino

There is receiver (gateway) code also in same repository.

Please help me improve the code.

Thanks.

Please help me improve the code.

Please explain the data type of the sensorData array

UKHeliBob,

I had to use uint8_t array of 6 bytes for both sensorData array and sensorType array because I am sending these arrays to receiver via esp8266 device's wifi mac address so it has to be same as wifi mac address format.

Thanks.

Then why are you saving a float to the array of ints ?

I think I wanted to get decimal values for voltage at receiving end I thought this is the only way to do it.Receiver does receive correct voltage value.

I use following at sender side:

float voltage = ESP.getVcc() / (float)1023 * 50;

I use following at receiver side:

voltage = voltage * 2 / 100;

Thank you.

Looking at my serial debug it sends voltage value around .146 from sender and it is decoded at receiver as 2.92 volts.

Thanks.

Looking at my serial debug it sends voltage value around .146

What are you printing when you see .146 ?

Following are debug messages:

At sender side:

Sensor Type values sent to controller: 24:06:10:1A:24:2E

Activate function for BME280
Activate function for APDS9960 Light Sensor

Sensors values sent to controller: 24:92:1C:01:3A:02
Message received from Controller: 24:06:10:1A:24:2E

Gateway Name is: ESP
Gateway Channel is: 7

I think second value of 92 in sensorValues array is hex for 146.

At receiver side:

Received Topic 'SensorData/36/6/2.92
Received Topic 'SensorData/36/16/3
Received Topic 'SensorData/36/26/16
Received Topic 'SensorData/36/36/892
Received Topic 'SensorData/36/46/6
Received Topic 'DeviceStatus/36/DeviceMode/0
Received Topic 'DeviceStatus/36/DeviceIP/36
Received Topic 'DeviceStatus/36/WiFiChannel/7
Received Topic 'DeviceStatus/36/SleepTime/1
Received Topic 'DeviceStatus/36/UpTime/132

Signal Strength of remote sensor: -51
Received Topic 'Sensordata/Signal/-51

Thanks.

The code provided by ToddL1962 make it compact code but as you can see I have to use 6 byte arrays to send my data to the receiver.I am not well versed to figure out how to use his code to produce 2 arrays to send sensor values and sensor types.

As it is the code works fine but it is too long.

Thanks

I think second value of 92 in sensorValues array is hex for 146.

0x92 is inmdeed 146 decimal, but neither of them are a float and certainly not .146, so why is the voltage variable a float ?

Ok I now understand this variable could be integer and it will still send same voltage data.But larger question is still how I can make my code compact ?

Thanks.

happytm:
But larger question is still how I can make my code compact ?

Maybe get the current version working properly first

sensorData[2] = random(1);  // 0 or 1

This statement does not do what the comment says it does

This is just a test code to figure out what could be the better solution so I think I will remove the comment for your satisfaction and politely try to ask for help for main goal of my original post. As I said earlier the my code as it is working properly for what it is intended to do but I wanted to make it compact and more time efficient as this is low power consumption device.

Thanks.

happytm:
This is just a test code to figure out what could be the better solution so I think I will remove the comment for your satisfaction and politely try to ask for help for main goal of my original post. As I said earlier the my code as it is working properly for what it is intended to do but I wanted to make it compact and more time efficient as this is low power consumption device.

Thanks.

What I posted should be easily adaptable to your application and much more readable.

ToddL1962,

What I posted should be easily adaptable to your application and much more readable.

Please see my earlier response. Can you please help me with this?

Thank you.

The code provided by ToddL1962 make it compact code but as you can see I have to use 6 byte arrays to send my data to the receiver.I am not well versed to figure out how to use his code to produce 2 arrays to send sensor values and sensor types.

As it is the code works fine but it is too long.