Storing in onboard flash issue

Hello,

Finally getting somewhere with this code. The final part is retrieving the data from memory upon reset condition. I can see when it gets to "saving data" section in serial monitor. When I reset, it is not retrieving the saved data in memory. What am I missing using FlashStorage.h?

#include <FlashStorage.h> // Threshold values for detecting a press (you can calibrate these) const int threshold1 = 500; // Adjust based on sensor 1 calibration const int threshold2 = 500; // Adjust based on sensor 2 calibration // Define a struct to store press counters struct PressData { int pressCounters[200]; // Array to hold press counts (100 entries for each sensor) int currentIndex; // Current index in the array }; // Flash storage for storing activation counts FlashStorage(pressStorage, PressData); // Store a struct in flash memory // Declare an instance of PressData to hold the current session's data PressData pressData = {{0}, 0}; // Activation counters for each sensor int sensor1Count = 0; int sensor2Count = 0; // Pin definitions for sensors const int sensor1Pin = A0; const int sensor2Pin = A1; void setup() { Serial.begin(115200); // Configure sensor pins as input pinMode(sensor1Pin, INPUT); pinMode(sensor2Pin, INPUT); // Load any previously saved data from flash memory (if any) pressData = pressStorage.read(); Serial.println("Restored press counts from flash memory:"); for (int i = 0; i < pressData.currentIndex; i++) { Serial.println(pressData.pressCounters[i]); } } void loop() { // Read the values from the sensors int sensor1Value = analogRead(sensor1Pin); int sensor2Value = analogRead(sensor2Pin); // Check if sensor 1 is pressed if (sensor1Value < threshold1) { sensor1Count++; Serial.println("Sensor 1 pressed!"); delay(100); // Debouncing delay } // Check if sensor 2 is pressed if (sensor2Value < threshold2) { sensor2Count++; Serial.println("Sensor 2 pressed!"); delay(100); // Debouncing delay } // If either sensor was pressed, store the counts in the array if (sensor1Value < threshold1 || sensor2Value < threshold2) { // Store sensor counts in the array pressData.pressCounters[pressData.currentIndex++] = sensor1Count; pressData.pressCounters[pressData.currentIndex++] = sensor2Count; // Reset sensor counts for next activation sensor1Count = 0; sensor2Count = 0; // If the array reaches 100 activations (200 entries), write to flash memory if (pressData.currentIndex >= 200) { saveToFlash(); } } delay(100); // Short delay to avoid rapid polling } // Function to save the press data to internal flash memory void saveToFlash() { Serial.println("Saving data to flash memory..."); pressStorage.write(pressData); // Write the PressData struct to flash memory Serial.println("Data saved."); // Reset index for future data logging pressData.currentIndex = 0; } 

Not familiar with the board nor with FlashStorage library. The storeNameAndSurname example does a test to see if the owner is valid. So I would use that before reading the data from the flash storage.

You can also verify in your saveToFlash() if the data is written correctly by reading it back immediately.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.