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.
2 Answers 2
total[i] = total[i] - readings[readIndex];
total
is of type int
. That is, it is a scalar integer. You cannot index into it, total[i]
doesn't make sense. That is why the compiler is complaining.
-
I see. So, how can I make subdivisions of the var total for each sensor/relay set?Dante Nahuel Ciai– Dante Nahuel Ciai2016年06月08日 17:35:58 +00:00Commented Jun 8, 2016 at 17:35
-
You could make total an array (if I understand what you're asking).uint128_t– uint128_t2016年06月08日 18:16:12 +00:00Commented Jun 8, 2016 at 18:16
Using the exponential moving average filter suggested by Gerben, things become quite simpler:
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;
const int numReadings = 64;
const word printInterval = 1000; // 1 second
float tempC[4];
unsigned long lastPrintTime = 0;
void setup()
{
analogReference(INTERNAL);
Serial.begin(115200);
for (int i = 0; i < 4; i++) {
digitalWrite(relayPin[i], HIGH);
pinMode(relayPin[i], OUTPUT);
}
}
void loop()
{
// readings and control
for (int i = 0; i < 4; i++) {
float raw_temp = analogRead(tempPin[i]) / 9.31;
tempC[i] += (raw_temp - tempC[i]) / numReadings; // filter
if (tempC[i] < lowerLimit) {
digitalWrite(relayPin[i], LOW); //relay OFF
}
else if (tempC[i] > upperLimit) {
digitalWrite(relayPin[i], HIGH); // relay ON
}
}
if (millis() - lastPrintTime >= 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
}
}
-
I wonder whether this code could be (very slightly) improved by initializing
tempC[i]
with the firstraw_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 ifnumReadings
and/orprintInterval
is high. So if you agree, what would be the most elegant way to implement this?Christoph– Christoph2020年02月25日 07:43:43 +00:00Commented 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 insetup()
.Edgar Bonet– Edgar Bonet2020年02月25日 09:05:34 +00:00Commented 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!Christoph– Christoph2020年02月25日 13:35:37 +00:00Commented Feb 25, 2020 at 13:35
avgTemp = (9*avgTemp + 1*reading)/10;
.avg += (reading - avg) >> 6;
, for τ = 64.