#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
// --- Pin Definitions ---
#define I2C_SDA 21
#define I2C_SCL 22
#define ONEWIRE_BUS 4 // DS18B20 Data Pin
#define POT_PIN 34 // Potentiometer (for simulated GSR/Stress)
#define LED_PIN 13 // LED (for simulated haptic motor)
// --- Global Objects ---
Adafruit_SSD1306 display(128, 64, &Wire, -1);
OneWire oneWire(ONEWIRE_BUS);
DallasTemperature sensors(&oneWire);
Adafruit_MPU6050 mpu;
// --- Global Variables ---
float temperatureC = 0.0;
int gsrValue = 0;
void setup() {
Serial.begin(115200);
// Setup I2C
Wire.begin(I2C_SDA, I2C_SCL);
// 1. Setup OLED Display
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println("System Booting...");
display.display();
delay(1000);
// 2. Setup Temperature Sensor (DS18B20)
sensors.begin();
// 3. Setup Motion Sensor (MPU-6050)
if (!mpu.begin()) {
Serial.println("Failed to find MPU6050 chip");
display.println("MPU-6050 NOT FOUND");
display.display();
while (1);
}
Serial.println("MPU6050 Found!");
// 4. Setup LED
pinMode(LED_PIN, OUTPUT);
// 5. Setup Potentiometer (Analog Input)
pinMode(POT_PIN, INPUT);
}
void loop() {
// --- 1. READ REAL SENSORS ---
// Read Temperature
sensors.requestTemperatures();
temperatureC = sensors.getTempCByIndex(0);
// Read "Stress" from Potentiometer
// Reads 0-4095, map to 0-100%
gsrValue = map(analogRead(POT_PIN), 0, 4095, 0, 100);
// Read Motion
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
float accelX = a.acceleration.x;
// --- 2. GENERATE SIMULATED DATA (FOR WOKWI) ---
// Simulate Heart Rate (65-90 bpm)
int heartRate = random(65, 90);
// Simulate Blood Pressure (110-125 / 70-85)
int bpSystolic = random(110, 125);
int bpDiastolic = random(70, 85);
// Simulate GPS Location (Bonus)
float latitude = 12.3456 + (random(-10, 10) / 10000.0);
float longitude = 78.9101 + (random(-10, 10) / 10000.0);
// --- 3. DISPLAY ALL DATA ---
display.clearDisplay();
display.setCursor(0, 0);
display.setTextSize(1);
// --- Page 1: Vitals ---
display.printf("HR: %d bpm (SIM)\n", heartRate);
display.printf("BP: %d/%d (SIM)\n", bpSystolic, bpDiastolic);
display.printf("Temp: %.1f C\n", temperatureC);
display.printf("Stress: %d%%\n", gsrValue);
display.printf("Acc.X: %.1f\n", accelX);
// --- Page 2: Location (Bonus) ---
display.setCursor(0, 50);
display.printf("Lat: %.4f\n", latitude);
display.printf("Lon: %.4f\n", longitude);
display.display();
// --- 4. Haptic Alert Logic ---
// If "stress" is over 90%, turn on the LED
if (gsrValue > 90) {
digitalWrite(LED_PIN, HIGH);
} else {
digitalWrite(LED_PIN, LOW);
}
delay(1000); // Update every second
}Loading
ds18b20
ds18b20