I am doing a project using an FSR (Force Sensitive Resistor), I'm currently reading values with no problem every 100milis. I want to output via Serial.print the highest value, I've read.
I am having a hard time doing so. The project is use to "measure" the strength some projectiles hit the sensor. I want to get the highest value.
This is the code I have until now.
int fsrPin = 0; // the FSR and 10K pulldown are connected to a0
int fsrReading; // the analog reading from the FSR resistor divider
void setup(void) {
// We'll send debugging information via the Serial monitor
Serial.begin(9600);
}
void loop(void) {
fsrReading = analogRead(fsrPin);
Serial.print("Analog reading = ");
Serial.println(fsrReading); // the raw analog reading
delay(100);
}
1 Answer 1
You don't want to delay ever. Instead you want to change your thinking to "X miliseconds have passed, time to print and reset":
int maxval = 0;
uint32_t lastSample = 0;
void setup() {
Serial.begin(115200);
}
void loop() {
if (millis() - lastSample > 100) { // Every 100ms:
lastSample = millis();
Serial.println(maxVal);
maxVal = 0;
}
int reading = analogRead(0);
if (reading > maxVal) {
maxVal = reading;
}
}
Every 100ms it will print the maximum analog value read during the past 100ms.
You can replace the 100ms with any other value you choose, or even with a check to see if a button has been pressed, in which case you get the maximum reading since the last time the button was pressed:
// global
bool lastButtonValue = HIGH;
// in loop()
if (digitalRead(3) != lastButtonValue) {
lastButtonValue = digitalRead(3);
if (lastButtonValue == LOW) {
// ... do the print and reset
}
}
-
how short an event (<1ms) and rate of sampling determine chance of missing an impulse force peak. software peak detection by a loop is a poor way for projectile peak force or g readings.Tony Stewart EE since 1975– Tony Stewart EE since 197509/25/2016 21:55:31Commented Sep 25, 2016 at 21:55
-
@TonyStewart What choice do you have though on an Arduino? All you can do is sample as fast as you can - theoretically at a frequency at least twice the reciprocal of the duration of an impact. Using free-running mode and an interrupt to read the values would give access to faster sampling rates, but that is beyond the ability of the OP I feel.Majenko– Majenko09/25/2016 21:57:25Commented Sep 25, 2016 at 21:57
-
@Majenko Thank, man, you approach was what I was looking for, you are right the approach should be delay-less, and should be "X time has passed need to print and reset" I am getting good data now.... THANKS!ALEZA– ALEZA09/25/2016 22:02:04Commented Sep 25, 2016 at 22:02
-
You add a small transistor emitter follower and output to a small plastic cap to peak fast and hold as long as the interval between sampless. driving the input low after a slow decaying reading, resets the cap voltage to zero, momentarily, from a bidirectional port, then waiting for the next projectile. This can be improved greatly but far better than trying to sample a detector.Tony Stewart EE since 1975– Tony Stewart EE since 197509/25/2016 22:02:07Commented Sep 25, 2016 at 22:02
-
@TonyStewart Maybe on the electronics.SE site that would be a good idea, but this is the Arduino site. Here it's done (for better or worse) using an Arduino.Majenko– Majenko09/25/2016 22:03:10Commented Sep 25, 2016 at 22:03
if
and>
(Greater-Than).delay()
in your code at all.