I am trying to:
- Create an array for calibration of multiple analog inputs
- Placing them in multidimensional array to smooth each one
I'm trying to combine these two examples basically:
https://www.arduino.cc/en/Tutorial/Calibration
https://www.arduino.cc/en/Tutorial/Arrays
I managed to compile the first set of code successfully, but in this case the group of calibrated analog inputs are not in an array. They are individually written.
The other issue is SensorValue needs to be placed back into the multidimensional array.
I attempted to do this by writing it as inputPinValue but got more errors.
When I try to place the calibrated values in an array. I get a compile error saying:
In function 'void setup()':
error: invalid types 'uint16_t {aka short unsigned int}[uint8_t {aka unsigned char}]' for array subscript
SensorValue[k] = 0;
error: invalid types 'uint16_t {aka short unsigned int}[uint8_t {aka unsigned char}]' for array subscript
SensorValue[l] = 0;
In function 'void loop()':
error: invalid types 'uint16_t {aka short unsigned int}[uint8_t {aka unsigned char}]' for array subscript
SensorValue[k] = {Azimuth, Elevation};
exit status 1
invalid types 'uint16_t {aka short unsigned int}[uint8_t {aka unsigned char}]' for array subscript
Successful code:
// ESP32 adc conversion
#include <driver/adc.h>
uint16_t Azimuth = 0;
uint16_t AzimuthMin = 4095; //minimum sensor value
uint16_t AzimuthMax = 0; //maximum sensor value
uint16_t Elevation = 0;
uint16_t ElevationMin = 4095; //minimum sensor value
uint16_t ElevationMax = 0; //maximum sensor value
// Define the number of samples to keep track of. The higher the number, the
// more the readings will be smoothed, but the slower the output will respond to
// the input. Using a constant rather than a normal variable lets us use this
// data to determine the size of the readings array.
const uint8_t numReadings = 64;
const uint8_t numSensors = 2;
uint32_t readings[numSensors][numReadings]; // multi-dimensional readings from analog input
uint32_t readIndex = 0; // the index of the current reading
uint32_t total[numSensors] = {0}; // the running total
uint32_t average = 0; // the average
void setup()
{
// initialize serial communication with computer:
Serial.begin(115200);
// initialize all the readings to 0
// multidimensional array takes into account multiple sensors and multiple readings for each sensor
for (uint8_t thisReading = 0; thisReading < numReadings; thisReading++)
{
for ( uint8_t j = 0; j < numSensors; j++) readings[j][thisReading] = 0;
}
}
void loop()
{
// Voltage divider analog in pins
adc1_config_width(ADC_WIDTH_12Bit);
adc1_config_channel_atten(ADC1_CHANNEL_6, ADC_ATTEN_DB_11); //Pin34
adc1_config_width(ADC_WIDTH_12Bit);
adc1_config_channel_atten(ADC1_CHANNEL_7, ADC_ATTEN_DB_11); //Pin35
// calibrate during the first five seconds
while (millis() < 5000)
{
// Read the sensor
Azimuth = adc1_get_raw(ADC1_CHANNEL_6);
Elevation = adc1_get_raw(ADC1_CHANNEL_7);
// record the maximum sensor value
if (Azimuth > AzimuthMax)
{
AzimuthMax = Azimuth;
}
// record the minimum sensor value
if (Azimuth < AzimuthMin)
{
AzimuthMin = Azimuth;
}
if (Elevation > ElevationMax)
{
ElevationMax = Elevation;
}
// record the minimum sensor value
if (Elevation < ElevationMin)
{
ElevationMin = Elevation;
}
}
// Read the sensor
Azimuth = adc1_get_raw(ADC1_CHANNEL_6);
Elevation = adc1_get_raw(ADC1_CHANNEL_7);
// apply the calibration to the sensor reading
Azimuth = map(Azimuth, AzimuthMin, AzimuthMax, 0, 4000);
// in case the sensor value is outside the range seen during calibration
Azimuth = constrain(Azimuth, 0, 4000);
// apply the calibration to the sensor reading
Elevation = map(Elevation, ElevationMin, ElevationMax, 0, 4000);
// in case the sensor value is outside the range seen during calibration
Elevation = constrain(Elevation, 0, 4000);
uint16_t inputPin[ numSensors ] = {Azimuth, Elevation};
uint8_t ai;
for (ai = 0; ai < numSensors ; ai++)
{
// subtract the last reading:
total[ ai ] = total[ ai ] - readings[ai][readIndex];
// read from the sensor:
readings[ai][readIndex] = inputPin[ai];
// add the reading to the total:
total[ai] = total[ai] + readings[ai][readIndex];
// calculate the average:
average = total[ai] / numReadings;
Serial.print( "Average sensor: ");
Serial.print( ai );
Serial.print( " " );
Serial.println(average);
}
// advance to the next position in the array:
readIndex = readIndex + 1;
// if we're at the end of the array...
if (readIndex >= numReadings)
{
// ...wrap around to the beginning:
readIndex = 0;
}
}
Code that is not compiling properly.
// ESP32 adc conversion
#include <driver/adc.h>
uint16_t Azimuth = 0;
uint16_t Elevation = 0;
uint16_t SensorValue = 0;
uint16_t SensorMin = 4095; //minimum sensor value
uint16_t SensorMax = 0; //maximum sensor value
// Define the number of samples to keep track of. The higher the number, the
// more the readings will be smoothed, but the slower the output will respond to
// the input. Using a constant rather than a normal variable lets us use this
// data to determine the size of the readings array.
const uint8_t numReadings = 64;
const uint8_t numSensors = 2;
uint32_t readings[numSensors][numReadings]; // multi-dimensional readings from analog input
uint32_t readIndex = 0; // the index of the current reading
uint32_t total[numSensors] = {0}; // the running total
uint32_t average = 0; // the average
void setup()
{
// initialize serial communication with computer:
Serial.begin(115200);
// initialize all the readings to 0
// multidimensional array takes into account multiple sensors and multiple readings for each sensor
for (uint8_t thisReading = 0; thisReading < numReadings; thisReading++)
{
for ( uint8_t j = 0; j < numSensors; j++) readings[j][thisReading] = 0;
}
for (uint8_t k = 0; k < numSensors; k++)
{
SensorValue[k] = 0;
}
for (uint8_t l = 0; l < numSensors; l++)
{
SensorValue[l] = 0;
}
}
void loop()
{
// Voltage divider analog in pins
adc1_config_width(ADC_WIDTH_12Bit);
adc1_config_channel_atten(ADC1_CHANNEL_6, ADC_ATTEN_DB_11); //Pin34
adc1_config_width(ADC_WIDTH_12Bit);
adc1_config_channel_atten(ADC1_CHANNEL_7, ADC_ATTEN_DB_11); //Pin35
// calibrate during the first five seconds
while (millis() < 5000)
{
// Read the sensor
Azimuth = adc1_get_raw(ADC1_CHANNEL_6);
Elevation = adc1_get_raw(ADC1_CHANNEL_7);
for ( uint8_t k = 0; k < numSensors; k++)
{
SensorValue[k] = {Azimuth, Elevation};
// record the maximum sensor value
if (SensorValue > SensorMax)
{
SensorMax = SensorValue;
}
// record the minimum sensor value
if (SensorValue < SensorMin)
{
SensorMin = SensorValue;
}
}
}
// apply the calibration to the sensor reading
SensorValue = map(SensorValue, SensorMin, SensorMax, 0, 4000);
// in case the sensor value is outside the range seen during calibration
SensorValue = constrain(SensorValue, 0, 4000);
uint16_t inputPin[ numSensors ] = {Azimuth, Elevation};
uint8_t ai;
for (ai = 0; ai < numSensors ; ai++)
{
// subtract the last reading:
total[ ai ] = total[ ai ] - readings[ai][readIndex];
// read from the sensor:
readings[ai][readIndex] = inputPin[ai];
// add the reading to the total:
total[ai] = total[ai] + readings[ai][readIndex];
// calculate the average:
average = total[ai] / numReadings;
Serial.print( "Average sensor: ");
Serial.print( ai );
Serial.print( " " );
Serial.println(average);
}
// advance to the next position in the array:
readIndex = readIndex + 1;
// if we're at the end of the array...
if (readIndex >= numReadings)
{
// ...wrap around to the beginning:
readIndex = 0;
}
}