// -----------------------------------------
// Predictive Maintenance Example (Wokwi)
// -----------------------------------------
// Pins
const int vibrationPin = A0;
const int tempPin = A1;
const int ledPin = 13;
// Thresholds (tune as needed)
const int vibThreshold = 600; // High vibration alert
const float tempThreshold = 60.0; // High temperature °C
// Moving average window
const int windowSize = 10;
int vibHistory[windowSize];
int indexCount = 0;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
// Initialize vibration history
for (int i = 0; i < windowSize; i++) vibHistory[i] = 0;
Serial.println("Predictive Maintenance System Started...");
}
void loop() {
// ----- Read vibration (using Potentiometer) -----
int vibValue = analogRead(vibrationPin);
// Moving average filter
vibHistory[indexCount] = vibValue;
indexCount = (indexCount + 1) % windowSize;
long total = 0;
for (int i = 0; i < windowSize; i++) {
total += vibHistory[i];
}
int vibFiltered = total / windowSize;
// ----- Read temperature from TMP36 -----
int rawTemp = analogRead(tempPin);
float voltage = rawTemp * (5.0 / 1023.0);
float temperatureC = (voltage - 0.5) * 100.0; // TMP36 equation
// ----- Print sensor data -----
Serial.print("Vibration: ");
Serial.print(vibFiltered);
Serial.print(" | Temp: ");
Serial.print(temperatureC);
Serial.println(" C");
// ----- Predictive Alerts -----
bool warning = false;
if (vibFiltered > vibThreshold) {
Serial.println("⚠️ Warning: Abnormal vibration detected!");
warning = true;
}
if (temperatureC > tempThreshold) {
Serial.println("🔥 Warning: Temperature exceeding safe limit!");
warning = true;
}
// LED alert
digitalWrite(ledPin, warning ? HIGH : LOW);
delay(500);
}