Next steps using IF statements to trigger actions

Hi all, please bare with me as I'm just starting to learn how to program Arduino, and this is my first post here.

I am testing out a very simple capacitive sensor switch, and I've got my head round how to trigger an LED to come on when you move your hand close to a piece of metal foil, using the following code:

#include <CapacitiveSensor.h>

CapacitiveSensor cSensor(4,2);

int sensorPin = 4;
int ledPin = 11;
int sensorValue = 0;

void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}

void loop() {
long sensor = cSensor.capacitiveSensor(30);
sensorValue = digitalRead(sensorPin);
Serial.print("Digital reading = ");
Serial.println(sensor);
// turn the ledPin on
if (sensor < 300){
digitalWrite(ledPin, LOW);
}else{
digitalWrite(ledPin,HIGH);
}
// stop the program for milliseconds:
delay(100);

}

This has got me to the point where the LED is only activated when the sensor reading rises above 300.

What I want to do next is to add a time condition so it only comes on when the reading rises above 300 for say 2 seconds. I've had a search online and through this forum, and I can find some references to using time as a condition, but I could really do with some pointers on how to modify my IF statement so it only applies when a second condition of the reading staying above 300 for a set number of milliseconds occurs.

Many thanks in advance,

David

DavidLund:
Hi all, please bare with me

Perhaps I can be permitted to skip that bit :slight_smile:

What I want to do next is to add a time condition so it only comes on when the reading rises above 300 for say 2 seconds.

If you look at this piece of code you will see that it continuously "resets the clock" when the condition is not met. Consequently it can only reach the timeout if the condition is constant. Obviously you would substitute your sensor reading for the button.

btnVal = digitalRead(btnPin);
if (btnVal == HIGH) {    // assumes btn is LOW when pressed
   lastBtnPressMillis = millis();   // btn not pressed so reset clock
}
if (millis() - lastBtnPressMillis > = interval) {
   // button has been pressed for longer than interval
}

...R

Hi Robin,

I'm afraid you've lost me already, baby steps needed!

My IF statement at the moment is:

if (sensor < 300){
digitalWrite(ledPin, LOW);
}else{
digitalWrite(ledPin,HIGH);
}

Which I can follow is setting an output if a condition is met. In your code example, I can follow a timer is re-setting, but how do I combine these into a joint condition?

David

I think, where I have if (btnVal == HIGH) you need to have if (sensor >= 300) And note that I have changed your test to >= so that it resets when the condition is NOT met.

...R

Hi Robin,

I'm still not getting my head round this I'm afraid - being new to the language I know what I want to say, and can kind of read the code, but can't yet figure out how to link different parts into new sketches, if that makes sense.

Could you walk me through your code snippet so I understand what its saying, and how it would work in my code, so I can learn what is going on? Parts such as lastBtnPressMillis are confusing me as I don't know what this translates to in my own code.

Many thanks,

David

Does this help ?

void loop() {

  // other code

  // your code to read get a number into the variable sensor 

  if (sensor  >= 300) {   // value is outside the acceptable range
     lastSensorCheckMillis = millis();   // out of range so reset clock
  }
  if (millis() - lastSensorCheckMillis > = interval) {
     // value has been within range longer than interval
     // code to do something
  }
}

I hope I understand what you want to achieve - which is that "something" should not happen unless the value of sensor remains within the acceptable range (below 300) througout an interval.

With this code if the value ever goes outside the range the timing is restarted.

...R

Thanks, the penny has finally dropped and I can follow that!

David

Glad to help.

When I first figured it out it did seem back-to-front.

...R