CODE - MakersAsylumIndia/VIBE-SPOT GitHub Wiki

THE ACTUAL CODE: #include <Arduino.h> #include <Preferences.h>

const int piezoPin = A1; // Pin where the piezo sensor is connected

const int numSamples = 100; // Number of samples to take for averaging const int sampleInterval = 10; // Milliseconds delay between samples

// Calibration variable int baselineValue = 0; Preferences preferences;

unsigned long lastPrintTime = 0; const unsigned long printInterval = 5000; // 5 seconds

void setup() { Serial.begin(115200); // Removed the while(!Serial) delay loop per request

// Initialize preferences preferences.begin("piezo", false);

// Calibration step long total = 0; for (int i = 0; i < numSamples; i++) { total += analogRead(piezoPin); delay(sampleInterval); } baselineValue = total / numSamples; safePrint("Calibration complete. Baseline value: " + String(baselineValue)); }

void loop() { int maxSoFar = -100000; for (int i = 0; i < numSamples; i++) { int sample = analogRead(piezoPin); if (sample > maxSoFar) { maxSoFar = sample; } delay(sampleInterval); }

int adjustedValue = maxSoFar - baselineValue;

if (adjustedValue > 850) { // Read current count of stored values int count = preferences.getInt("count", 0);

// Store new adjusted value under a new key like "value0", "value1", ...
String key = "value" + String(count);
preferences.putInt(key.c_str(), adjustedValue);

// Increment and store updated count
count++;
preferences.putInt("count", count);

safePrint("Stored adjusted value " + String(adjustedValue) + " as " + key);

} else { safePrint("Adjusted value below threshold (" + String(adjustedValue) + "), not stored."); }

// Print all stored values every 5 seconds unsigned long currentTime = millis(); if (currentTime - lastPrintTime >= printInterval) { lastPrintTime = currentTime; printAllStoredValues(); } }

void printAllStoredValues() { int count = preferences.getInt("count", 0); safePrint("Total stored values: " + String(count));

for (int i = 0; i < count; i++) { String key = "value" + String(i); int val = preferences.getInt(key.c_str(), 0); safePrint(key + ": " + String(val)); } safePrint(""); // Blank line for readability }

void safePrint(String message) { if (Serial) { Serial.println(message); } } THE DELETION CODE: #include <Arduino.h> #include <Preferences.h>

Preferences preferences;

void setup() { Serial.begin(115200); while (!Serial) delay(10);

preferences.begin("piezo", false);

// Clear all stored keys in the "piezo" namespace if (preferences.clear()) { Serial.println("Preferences cleared successfully."); } else { Serial.println("Failed to clear Preferences."); }

preferences.end(); // Close Preferences

Serial.println("Done. You can now upload your main program."); }

void loop() { // Nothing to do here }

⚠️ **GitHub.com Fallback** ⚠️