#include <Keypad.h>
// Keypad setup
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Analog input pin
const int sensorPin = A0;
// Flags for method selection
bool useMedian = false;
bool useAverage = false;
bool useMovingAverage = false;
bool useExponentialAverage = false;
// Moving average variables
const int numReadings = 10;
int readings[numReadings];
int readIndex = 0;
long int total = 0;
// Function to calculate median
int readMedian(int pin, int samples) {
int raw[samples];
for (int i = 0; i < samples; i++) {
raw[i] = analogRead(pin);
}
// Sort the copy of raw values
for (int i = 0; i < samples - 1; i++) {
for (int j = i + 1; j < samples; j++) {
if (raw[i] > raw[j]) {
int temp = raw[i];
raw[i] = raw[j];
raw[j] = temp;
}
}
}
return raw[samples / 2];
}
// Function to calculate average
float calculateAverage(int pin, int readings) {
long int sum = 0;
for (int i = 0; i < readings; i++) {
sum += analogRead(pin);
delay(1);
}
return static_cast<float>(sum) / readings;
}
// Function to calculate moving average
float calculateMovingAverage(int pin, int numReadings) {
total = total - readings[readIndex];
readings[readIndex] = analogRead(pin);
total = total + readings[readIndex];
readIndex = (readIndex + 1) % numReadings;
return static_cast<float>(total) / numReadings;
}
// Function to calculate exponential moving average
float calculateExponentialAverage(int pin, float weight) {
static float smoothedValue = 0.0;
float rawValue = analogRead(pin);
smoothedValue = weight * rawValue + (1.0 - weight) * smoothedValue;
return smoothedValue;
}
void setup() {
Serial.begin(9600);
for (int i = 0; i < numReadings; i++) {
readings[i] = 0;
}
}
void loop() {
char key = keypad.getKey();
if (key) {
switch (key) {
case '1':
useMedian = true;
useAverage = false;
useMovingAverage = false;
useExponentialAverage = false;
Serial.println("1- Median ");
break;
case '2':
useMedian = false;
useAverage = true;
useMovingAverage = false;
useExponentialAverage = false;
Serial.println("2- Average ");
break;
case '3':
useMedian = false;
useAverage = false;
useMovingAverage = true;
useExponentialAverage = false;
Serial.println("3- Moving Average ");
break;
case '4':
useMedian = false;
useAverage = false;
useMovingAverage = false;
useExponentialAverage = true;
Serial.println("4- Exponential Moving Average ");
break;
default:
Serial.println("Invalid key pressed.");
break;
}
}
int rawValue = analogRead(sensorPin);
float smoothedValue;
if (useMedian) {
smoothedValue = readMedian(sensorPin, 100);
} else if (useAverage) {
smoothedValue = calculateAverage(sensorPin, 15);
} else if (useMovingAverage) {
smoothedValue = calculateMovingAverage(sensorPin, 10);
} else if (useExponentialAverage) {
smoothedValue = calculateExponentialAverage(sensorPin, 0.5);
}
Serial.print("Raw value: ");
Serial.print(rawValue);
Serial.print(" Smoothed value: ");
Serial.println(smoothedValue);
delay(100);
}