1

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);
} 
  • 2
    You need to learn about if and > (Greater-Than). – Majenko Sep 25 '16 at 21:37
  • Majenko, thanks... I dont know how to use it when using the delay, since.... I want to have a fast rate reading, and once I output it, reset it so the next measure will compare again, as to output multiple impacts. – ALEZA Sep 25 '16 at 21:39
  • Ah, so you want the maximum value within the 100ms time frame then? – Majenko Sep 25 '16 at 21:39
  • Majenk, 10ms, can be changed just used it for testing. (For this problem, I know I have to compare "last value" against "current value" and save the greater, but I am having trouble with, how fast should I "sample", and how to reset the "last value" variable, in order to be ready for the next impact. – ALEZA Sep 25 '16 at 21:42
  • You reset the "last value" by setting it to 0. As to when that happens, that is entirely up to you and is only something you can decide. How fast to sample? Well, I'd say as fast as you possibly can. There is no use for delay() in your code at all. – Majenko Sep 25 '16 at 21:43
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 Sunnyskyguy EE75 Sep 25 '16 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 Sep 25 '16 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 Sep 25 '16 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 Sunnyskyguy EE75 Sep 25 '16 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 Sep 25 '16 at 22:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.