Hey guys i need some help averageing my sensor readings. I would like to average them over 3 readings. Any input on this? I have looked at the smoothing example but there must be an easier way than that. Thanks!
#include <PWM.h>
#include <PID_v1.h>
#include <SPI.h>
#include <LiquidCrystal.h>
const int ebp = A0;
const int tps = A1;
const int vss = A2;
const int ebswitch = 13;
const int pwmIn = 8;
const int pwmOut = 10; //change to 9 for real code
//testing only
const int test = 9; //delete after testing
//
int ebpvalue = 0;
int ebpvalueD= 0;
int tpsvalue = 0;
int vssvalue = 0;
int ebswitchvalue = 0;
//test only
int dc = 114;
//
double totaltime = 5988;
double dutycycleRead = 0.0;
double dutycycleIn= 0.0;
double dutycycleOut = 0.0;
int dutycycleInD = 0;
int dutycycleOutD = 0;
unsigned long durationL;
int32_t frequency = 167; //frequency (in Hz)
//pid info
//Define PID variables
double Setpoint, Input, pidOutput;
//Specify the links and initial tuning parameters
PID myPID(&Input, &pidOutput, &Setpoint,1,40,0, DIRECT);
// initialize lcd with pin numbers
LiquidCrystal lcd (7, 6, 5, 4, 3, 2);
void setup() {
//TCCR2B = TCCR2B & 0b11111000 | 0x06; //set frequency to 122 hz
pinMode(ebp, INPUT);
pinMode(tps, INPUT);
pinMode(vss, INPUT);
pinMode(ebswitch, INPUT);
pinMode(pwmIn, INPUT);
pinMode(pwmOut, OUTPUT);
pinMode(test, OUTPUT);
//initialize all timers except for 0, to save time keeping functions
InitTimersSafe();
//sets the frequency for the specified pin
bool success = SetPinFrequencySafe(test, frequency); //change to pwmOut after testing
//if the pin frequency was set successfully, turn pin 13 on
if(success) {
pinMode(test, OUTPUT); //change to pwmOut after testing
}
//initialize PID variables
Input = ebpvalue;
Setpoint = 360; //test value of 360 = 33.5 psia or 20 psig
myPID.SetOutputLimits(25, 108);
//turn the PID on
myPID.SetMode(AUTOMATIC);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
}
void loop() {
ebpvalue = analogRead(ebp);
ebpvalueD = ((ebpvalue * 0.004888 * 19.01) - 13.5);
//testing only
dc = map(ebpvalue, 0, 1023, 0, 108);
pwmWrite(test, dc);
//
tpsvalue = analogRead(tps);
vssvalue = analogRead(vss);
ebswitchvalue = digitalRead(ebswitch);
durationL = pulseIn(pwmIn, LOW);
dutycycleRead = (durationL / totaltime) * 100;
dutycycleIn = (dutycycleRead * 2.55);
lcd.clear();
if (ebswitchvalue == HIGH && tpsvalue < 86 && vssvalue > 300){
Input = ebpvalue;
myPID.Compute();
pwmWrite(pwmOut, pidOutput);
lcd.setCursor(14, 0);
lcd.print("ON");
lcd.setCursor(0, 1);
lcd.print("VGT%:");
lcd.setCursor(5, 1);
lcd.print((pidOutput * 0.3922));
}
else{
pwmWrite(pwmOut, dutycycleIn);
lcd.setCursor(13, 0);
lcd.print("OFF");
lcd.setCursor(0, 1);
lcd.print("VGT%:");
lcd.setCursor(5, 1);
lcd.print(dutycycleRead);
}
if(ebswitchvalue == HIGH){
lcd.setCursor(0, 0);
lcd.print("ENABLED");
}
else{
lcd.setCursor(0, 0);
lcd.print("DISABLED");
}
lcd.setCursor(11, 1);
lcd.print("EP:");
lcd.setCursor(14, 1);
lcd.print(ebpvalueD);
delay(1);
}