1

I've succeeded to get an Arduino to work with a pressure sensor.

What I want to do is

  • when I put pressure on the sensor, after 5 minutes the led lights on.
  • it starts over again if there's no pressure on the sensor.

Here's the program:

// FSR is verbonden met analoog 0 
int fsrAnalogePin = 0; 
// De LED is verbonden met pin 11 (pmw pin) 
int LEDpin = 11; 
// De analoge waarde van de fsr spanningdeler 
int fsrWaarde; 
// De helderheid van de led tussen 0 en 255 
int LEDhelderheid; 

void setup() { 
    // start de serial monitor 
    Serial.begin(9600); 
    pinMode(LEDpin, OUTPUT); 
} 

void loop() { 
    fsrWaarde = analogRead(fsrAnalogePin); 
    // print ‘Analoge waarde’ 
    Serial.print(“Analoge waarde = “); 
    // print de fsrwaarde op de monitor 
    Serial.println(fsrWaarde); 

    // maak van getallen tussen 0 en 1023 getallen tussen 0 en 255 
    LEDhelderheid = map(fsrWaarde, 0, 50, 0, 255); 
    analogWrite(LEDpin, LEDhelderheid); 
    delay(100); 
}

I've got this for the pressure sensor... now I need to combine it with 5 minutes and the led lights on when it's reached that 5 min of pressure.

i managed to do this but it aint enough...

unsigned long currentTime;
unsigned long startTime;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  currentTime = millis();
  startTime = currentTime;
}

    void loop() {
  currentTime = millis();
  // put your main code here, to run repeatedly: 
  Serial.println(currentTime);
  delay(1000); 

  if(currentTime-startTime > 6000) {
Serial.println("1 minuut voorbij");
startTime = currentTime;
  }
}
1

Try this:

unsigned long pressureOnTime; //millis() time when pressure switch is first turned on

const unsigned long ledOnAfterTimeThreshold = 300000; //5 minutes * 60 sec * 1000 mSec
int pressureThreshold = 1; //minimum analog reading from pressure sensor considered to be "on"

const int fsrAnalogePin = 0; 
const int ledPin = 11; 

int ledState = LOW;
int pressureState = LOW;

void setup() {
    Serial.begin(9600);
}

void loop() {
    checkPressureSwitch();  

    //do other stuff here
}


void checkPressureSwitch() {
    if( analogRead(fsrAnalogePin) >=  pressureThreshold ) 
    {
        handlePressureOn();
    }
    else 
    {
        handlePressureOff();
    }

    digitalWrite(ledPin, ledState);
}

void handlePressureOn() {
    if ( pressureState == LOW )
    {
        //pressure switch was off, but is now on
        pressureOnTime = millis();
        pressureState = HIGH;
    }

    else if ( millis() - pressureOnTime >= ledOnAfterTimeThreshold ) 
    {
        ledState = HIGH;
    }
}

void handlePressureOff() {
    ledState = LOW;
    pressureState = LOW;
}
  • thanks but im getting a void error(void handlePressureOn) on the line; else if ( millis() - pressureOnTime >= ledOnAfterTimeTheshold ) and it says that (ledOnAfterTimeTheshold) was not declared – peruwan May 4 '14 at 13:27
  • There was a typo: change ledOnAfterTimeTheshold to ledOnAfterTimeThreshold. I edited the answer as well. – imjosh May 5 '14 at 14:18
  • thank you again but when i do the SerialMonitor i dont see anything doing...maybe its how i put my board check this link is how i put my board; thuisexperimenteren.nl/science/Arduino/Arduino_FSR/… – peruwan May 6 '14 at 10:52
  • Hi, please read and try to understand the code. Notice that I didn't put any Serial.print() statements, so of course there is nothing happening in the serial monitor. Your questions states that you want the LED to be the indicator and this code should do that. If you want something output in the serial console, add some print statements. – imjosh May 6 '14 at 15:51
  • sorry im a rookie...but im going to try to do it on my board see if it works and understand – peruwan May 7 '14 at 18:13
0

The very simplest way - not necessarily the best way - is something like:

Keep re-reading the pressure sensor until it senses pressure;
Read millis(), add 300000 (# of msec in 5 min) to it's value, and store that;
Keep re-read millis() until it returns a number >= to that stored value;
Read the pressure sensor;
If it senses pressure,
  turn on the alarm;
  do nothing, forever;
Otherwise, start over again;

It isn't the best way, because the Arduino does nothing else for the 5 minutes until the final sensor check. But as you're learning, you probably don't need it to do anything else - for now. When that time comes, there are better ways than dedicating your processor to watching the clock. But this will get you started, and you may figure out some of those ways on your own, by then. Good luck!

Update: I realized my answer does not account for the possibility that pressure is released from the sensor and reapplied some time later, if both occur within the 5 minute wait. How you treat that situation will depend on the needs of your application (which I'm guessing is for you to learn to work with time and physical data). I'll leave that as a further exercise for you.

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.