Skip to main content
Arduino

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Help with getting Sensor Average using Arrays

First of all, let me be clear: I'm a total noob, I have close to zero programming experience and I know i'm out of my league, but I want to learn and understand. Here's the issue. The following code has been made to control heat pads using 4 relays that turn off/on depending on the temperature that 4 lm35 Temp sensors pick up. Because these sensors can be jumpy sometimes, I want to get the average temperature from, let's say, 50 readings, and make the code check THAT data instead of the raw temp reading. I have the code for the arduino (I wrote some code and guys over other forums took it and modified it to make it shorter (and that kind of lost me, but the code works) and then I searched smoothing data and got a piece of code that does exactly what I want: make an array, calculate the average temp, and keep deleting and adding new data.

I tried to join both codes together and I got all different kind of errors, lol So here I am.

The Code that works without average data is this one:

const byte tempPin[] = {A1, A2, A3, A4};
const byte relayPin[] = {6, 7, 8, 9};
// hysteresis = upperLimit - lowerLimit
const byte lowerLimit = 24;
const byte upperLimit = 31;
float tempC[4];
word reading[4];
word printInterval = 1000; // 1 second
unsigned long printCheck = 0, lastPrintTime = 0;
void setup()
{
 analogReference(INTERNAL);
 Serial.begin(115200);
 for (int i = 0; i < 4; i++) {
 pinMode(relayPin[i], INPUT_PULLUP);
 pinMode(relayPin[i], OUTPUT); // defaults HIGH, relay OFF
 }
}
void loop()
{
 // readings and control
 for (int i = 0; i < 4; i++) {
 reading[i] = analogRead(tempPin[i]);
 tempC[i] = reading[i] / 9.31;
 if (tempC[i] < lowerLimit) {
 digitalWrite(relayPin[i], LOW); //relay OFF
 }
 else if (tempC[i] > upperLimit) {
 digitalWrite(relayPin[i], HIGH); // relay ON
 }
 }
 printCheck = millis() - lastPrintTime;
 if (printCheck >= printInterval) {
 for (int i = 0; i < 4; i++) {
 Serial.print("tempC");
 Serial.print(i + 1);
 Serial.print(" ");
 Serial.println(tempC[i]);
 }
 Serial.println();
 lastPrintTime = millis(); // reset print timer
 }
}

The average code I got from Arduino Learning center is this one:

/*
 Smoothing
 Reads repeatedly from an analog input, calculating a running average
 and printing it to the computer. Keeps ten readings in an array and
 continually averages them.
 The circuit:
 * Analog sensor (potentiometer will do) attached to analog input 0
 Created 22 April 2007
 By David A. Mellis <[email protected]>
 modified 9 Apr 2012
 by Tom Igoe
 http://www.arduino.cc/en/Tutorial/Smoothing
 This example code is in the public domain.
*/
// 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
// use this value to determine the size of the readings array.
const int numReadings = 10;
int readings[numReadings]; // the readings from the analog input
int readIndex = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
int inputPin = A0;
void setup() {
 // initialize serial communication with computer:
 Serial.begin(9600);
 // initialize all the readings to 0:
 for (int thisReading = 0; thisReading < numReadings; thisReading++) {
 readings[thisReading] = 0;
 }
}
void loop() {
 // subtract the last reading:
 total = total - readings[readIndex];
 // read from the sensor:
 readings[readIndex] = analogRead(inputPin);
 // add the reading to the total:
 total = total + readings[readIndex];
 // 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;
 }
 // calculate the average:
 average = total / numReadings;
 // send it to the computer as ASCII digits
 Serial.println(average);
 delay(1); // delay in between reads for stability
}

The monster I created joining both codes is this one:

const byte tempPin[] = {A1, A2, A3, A4};
const byte relayPin[] = {6, 7, 8, 9};
// hysteresis = upperLimit - lowerLimit
const byte lowerLimit = 24;
const byte upperLimit = 31;
float tempC[4];
word reading[4];
const int numReadings = 50;
int readings[numReadings];
int readIndex = 0;
int total = 0;
float average[4];
word printInterval = 1000; // 1 second
unsigned long printCheck = 0, lastPrintTime = 0;
void setup()
{
 analogReference(INTERNAL);
 Serial.begin(115200);
 for (int i = 0; i < 4; i++) {
 pinMode(relayPin[i], INPUT_PULLUP);
 pinMode(relayPin[i], OUTPUT); // defaults HIGH, relay OFF
 }
 for (int thisReading = 0; thisReading < numReadings; thisReading++){
 readings[thisReading] = 0;
 }
}
void loop()
{
 // readings and control
 for (int i = 0; i < 4; i++) {
 reading[i] = analogRead(tempPin[i]);
 tempC[i] = reading[i] / 9.31;
 total[i] = total[i] - readings[readIndex];
 readings[readIndex] = tempC[i]
 total[i] = total[i] + readings[readIndex]
 readIndex[i] = readIndex[i] + 1 ;
 if (readIntex[i] >= numReadings) {
 readIndex = 0;
 }
 average[i] = total[i] / numReadings;
 if (average[i] < lowerLimit) {
 digitalWrite(relayPin[i], LOW); //relay OFF
 }
 else if (average[i] > upperLimit) {
 digitalWrite(relayPin[i], HIGH); // relay ON
 }
 }
 printCheck = millis() - lastPrintTime;
 if (printCheck >= printInterval) {
 for (int i = 0; i < 4; i++) {
 Serial.print("tempC");
 Serial.print(i + 1);
 Serial.print(" ");
 Serial.println(tempC[i]);
 }
 Serial.println();
 lastPrintTime = millis(); // reset print timer
 }
}

These are the errors that I got:

Arduino: 1.6.8 (Windows 7), Board: "Arduino/Genuino Uno"
\temprelayfinal2.ino: In function 'void loop()':
temprelayfinal2:40: error: invalid types 'int[int]' for array subscript
 total[i] = total[i] - readings[readIndex];
 ^
temprelayfinal2:40: error: invalid types 'int[int]' for array subscript
 total[i] = total[i] - readings[readIndex];
 ^
temprelayfinal2:42: error: expected ';' before 'total'
 total[i] = total[i] + readings[readIndex]
 ^
temprelayfinal2:44: error: 'readIntex' was not declared in this scope
 if (readIntex[i] >= numReadings) {
 ^
temprelayfinal2:47: error: invalid types 'int[int]' for array subscript
 average[i] = total[i] / numReadings;
 ^
exit status 1
invalid types 'int[int]' for array subscript
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Answer*

Draft saved
Draft discarded
Cancel
3
  • I wonder whether this code could be (very slightly) improved by initializing tempC[i] with the first raw_temp reading? Wouldn't this significantly increase the accuracy of the first readings (i.e. tempC[i])? In many use cases, this will not matter practically but in some it might, especially if numReadings and/or printInterval is high. So if you agree, what would be the most elegant way to implement this? Commented Feb 25, 2020 at 7:43
  • @Christoph: Initializing tempC[i] the way you suggest would indeed improve the first readings, at the cost of slightly increased complexity. The most elegant implementation would probably be a "filter" class that handles the initialization itself when it gets the first reading. The simplest would be to explicitly initialize the array in setup(). Commented Feb 25, 2020 at 9:05
  • Cool! I was wondering exactly that: is it "allowed" to already take a first sensor reading in setup()? So if it is, it sounds like it's the simplest solution (instead of adding a flag-variable or even a class). I realize that "simplest" is not always the same as "most elegant". Thanks! Commented Feb 25, 2020 at 13:35

lang-cpp

AltStyle によって変換されたページ (->オリジナル) /