Hi, I am working on code to store counts on a couple analog sensors on the ADC ports.
I got the code to compile with no errors. I am using FlashStorage.h library to avoid stepping on the boot loader part of the memory. BUT.....when I compiled and uploaded the build, as it was finishing up progamming the hardware, it errored and said it couldn't find the Arduino Zero on USB6 (whereas before programming it found it).
I tried the reset switch and it did not help. It would find the device on the USB but when I upload code to it, an error message pops up that it couldn't find the board. I tried the same code on a Adafruit Trinket M0 (SAMD21 same as Zero) and it bricked it too. I am assuming I bricked the boot loader......or something else? I have the following questions:
1). How do I restore these boards to take uploads?
2). What in my code would have bricked it? What is the fix?
#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;
}