variable resistor + edge detection?

Hi
I am currently examining the ButtonStateChange example sketch, to hack/adapt it to suit my needs; using an FSR I wish to detect when the current changes from 0 to >0 - using the FSR as a momentary switch (conventional switch design/shape/housing doesn't suit my needs), such that I can get ONE event from the state change. Is there a simple function out there that does edge detection like this?

psuedo-code
if (sensorVariable>0){
do this once only
}
else{
reset
}

thanks
Brendan

psuedo-code
unsigned char done = 0;
if (sensorVariable>0 && done == 0){
   done =1;
   //do this once only
}
else{
reset
}

To reset, change the variable done to 0

The variable, done, should be static or global, so that it retains its value. If declared that way in a function, it will reset each time the function is called.

Also note that greater than 0 may not be appropriate for the FSR. You may need some (slightly) higher value to prevent false positives.

Hallo gents, and thanks for the (once again) prompt replies.

Whilst working on this today, I thought it best to stick to testing the code with a switch - I can implement the FSR later. Apologies for not being 100% clear on this; what I wish to do is (via code) turn a momentary switch into a latching one, such that an LED lights upon the first press of the switch (switch 0 to 1 transition, or FSR > x), and then the LED is off when the next press occurs (switch 0 to 1, or FSR > x).

I will try to implement the 'done' variable in my current hack, but what is (clearly) apparent is that when the contact is made the digitalRead function is continuously returning a stream of 1's, where I only want one 'hit'.

The code below is a hack of the buttonStateDetection and Debounce examples; it doesn't work as I wish - it flashes the LED when button is pressed, what I want the code to do is toggle the state of the LED:

//use millis() as a timer to check if state change is > wait time
//combines debounce and statechange examples


int newState = 0;//current state, on or off
int oldState = 0;//previous state, off or on
int switchPin = 2;
int ledPin = 12;

long newTime = 0;//time at which state changed
//_________________________________________________
int noiseDelay = 50;//wait time; change to test
//_________________________________________________
int pressCount = 0;

void setup(){
  pinMode(switchPin, INPUT);//switch connection
  pinMode(ledPin, OUTPUT);//LED+ (anode, long)
  Serial.begin(9600);
}

void loop(){
  int readState = digitalRead(switchPin);//read the switch's state and store it
  
  if (readState != oldState){//if state has changed
    newTime = millis();
  }
  if ((millis() - newTime) > noiseDelay){//if wait time has passed
    newState = readState;//true reading
    if (newState==HIGH){//if off --> on
     pressCount++;
    }
    else{
      pressCount = 0;
    }
    pressCount = pressCount%2;
    digitalWrite(ledPin, pressCount);//assign reading to output
  }
  oldState = readState;
  Serial.println(newState);
  delay(20);
}

thanks again Team-Arduino

To implement signal edge detection with a debounce you need four variables that are affected by the button:

lastState
state
lastReading
reading

The first three should be static or global. This works by using the readings to handle the debounce, while the states handle the signal edge. When the reading and lastReading are different, you reset the debounce timer. When the debounce timer is reached, the state variable is set to that reading. When the state and lastState variable are different, you have a signal edge. At the end of each loop the last variables should be updated to be the current ones.

Hi
SUCCESS

I tried rather inexpertly to hack together the idea above with my existing code; due to my limited skillz I couldn't get it to roll.

I did however get the functionality I wanted (turn a momentary switch into a latching one and toggle between two LEDs), without using a timer:

/*
Latching functionality from a momentary switch - toggles between two LEDs;
this sketch combines elements of the examples StateChangeDetection and Debounce;
it reads a switch on pin2, counting the number of HIGH readings (%2).
Brendan McCloskey 2012
*/
int counter=0;     //count # of presses
int newState=0;    //is switch open or closed
int lastState=0;   //what was previous state

void setup(){
  pinMode(2, INPUT);
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
  Serial.begin(9600);
}

void loop(){
  //local var initialized every loop; reads input pin
  int readPin=digitalRead(2);
  delay(20);
  //if state changes AND pin is HIGH...
  if (readPin != lastState){
    if (readPin == HIGH){  
    //...accept as true reading
    newState=readPin;
    counter++;
    }
  }
  //binary 'function'
  counter=counter%2;  
  if (counter==0){
    digitalWrite(11, HIGH);
    digitalWrite(12, LOW);
  }
  else{
    digitalWrite(12, HIGH);
    digitalWrite(11, LOW);
  }
  Serial.write(counter);
  //update previous state
  lastState=readPin;
  delay(20);
}

Thanks for all the helpful input

Brendan

I'll offer one unsolicited piece of advice.

  int readPin=digitalRead(2);
  delay(20);
  //if state changes AND pin is HIGH...
  if (readPin != lastState){

Looking at this, there does not appear to be any relationship between readPin and lastState, so one wonders why you are comparing them.

Contrast that with:

  int currState = digitalRead(2);
  delay(20);
  //if state changes AND pin is HIGH...
  if (currState != lastState){

Here, the similarity of names leads one to expect that they are related.

Good names make all the difference when you come back to look at code 5 years, 5 months, or 5 weeks later. It's amazing how much you will forget, and how difficult it is to understand old code when the names appear to have been chosen at random.