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.
avgTemp = (9*avgTemp + 1*reading)/10;
.avg += (reading - avg) >> 6;
, for τ = 64.