user32415: I'm having some trouble wrapping my head around timers in Arduino. I've got a FSR Pressure Sensor and I want it to turn on my NeoPixel LED after pressing on it for 5 seconds. Any help is much appreciated!
My goal: 
FSRReading > 1022 then wait 5 seconds, turn on NeoPixel
FSRReading < 1022, then reset timer, turn off NeoPixel
This is what I currently have for my code, I can detect and act on sensor readings, but no timer:
#include <Adafruit_NeoPixel.h>
#define PIN 6
#define NUMPIXELS 16
#include "Timer.h"


Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

int fsrPin = 9; // FSR is connected to analog 0
int fsrReading; // the analog reading from the FSR resistor divider int LEDbrightness;
int LEDbrightness;
Timer t;


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

void loop(void) {
fsrReading = analogRead(fsrPin); 
Serial.println(fsrReading);
Serial.print("Analog reading = "); 

// we'll need to change the range from the analog reading (0-1023) down to the range // used by analogWrite (0-255) with map!
LEDbrightness = map(fsrReading, 0, 1023, 0, 255);

if (fsrReading < 10) {
Serial.println(" - No pressure"); } else if (fsrReading < 200) {
Serial.println(" - Light touch"); } else if (fsrReading < 500) {
Serial.println(" - Light squeeze"); } else if (fsrReading < 800) {
Serial.println(" - Medium squeeze"); } else {
Serial.println(" - Big squeeze"); }
delay(50);

for(int i=0;i<NUMPIXELS;i++){

  // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
  if (fsrReading>1022){
  pixels.setPixelColor(i, pixels.Color(0,255,0)); 
  } else if (fsrReading<1022) {
  pixels.setPixelColor(i, pixels.Color(0,0,0));
  }

  pixels.show(); // This sends the updated pixel color to the hardware.
 }
 }


user30005: 
My goal: FSRReading > 1022 then wait 5 seconds, turn on NeoPixel FSRReading < 1022, then reset timer, turn off NeoPixel

something like this will do.
  static uint32_t pressed_time; //last time the pressure sensor is pressed
  static uint8_t pressed_state=0; //the previous state. '1'->pressed, '0'->not pressed

  if (pressed()) {
    if (!pressed_state)) { //first time the pressure sensor is pressed
      pressed_state = 1;  //pressure sensor has been pressed
      pressed_time = millis(); //take time now
    } 
  } else {                //no longer pressed
    pressed_state = 0;  //reset pressed state
    turnOffled();       //turn the led off
  }

  if ((millis() > pressed_time + LED_DLY) && pressed_state) turnOnled();

untested code, use at your own risk
edit:
the above code in action, with a minor modification to return the desired led state (active high):
    //turn on / off the led pins
    if (led_ctrl()) digitalWrite(LED_PIN, HIGH);    //turn on the led, active high
    else digitalWrite(LED_PIN, LOW);        //turn off the led
}


Note:
1. pressed() is active low;
2. the led is programmed to turn on 100ms after the button goes low.
hope it helps.

user31106: The basic concept for tracking timing in the loop() is as follows:
1) when your event occurs, you store the current time in a variable: 
unsigned long lastReading = 0;
uint8_t waiting = false;

if ( !waiting && fsrReading > 1022 ) {
  lastReading = millis();
  waiting = true;
}

2) you compare the lastReading variable against the current time and when enough time has passed, you can perform you timed action. ie:
int timeToWait = 5000; // 5 seconds

// are we waiting? and has enough time passed?
if ( waiting && millis() - lastReading > timeToWait ) {
   waiting = false; // reset

   // Light up lights
   ...
}

Note: this specific implementation only requires the sensor to rise above 1022 once, then wait 5 seconds before executing the timed action. If you want the sensor reading to be maintained above 1022 for the entire duration then you would add a check for a release with something like:
if ( fsrReading < 1022 ) {
  waiting = false; // reset because reading fell below 1022
}


32415: <p>I'm having some trouble wrapping my head around timers in Arduino. I've got a FSR Pressure Sensor and I want it to turn on my NeoPixel LED after pressing on it for 5 seconds. Any help is much appreciated!</p>

<p>My goal: 
FSRReading > 1022 then wait 5 seconds, turn on NeoPixel
FSRReading &lt; 1022, then reset timer, turn off NeoPixel</p>

<p>This is what I currently have for my code, I can detect and act on sensor readings, but no timer:</p>

<pre><code>#include &lt;Adafruit_NeoPixel.h&gt;
#define PIN 6
#define NUMPIXELS 16
#include "Timer.h"


Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

int fsrPin = 9; // FSR is connected to analog 0
int fsrReading; // the analog reading from the FSR resistor divider int LEDbrightness;
int LEDbrightness;
Timer t;


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

void loop(void) {
fsrReading = analogRead(fsrPin); 
Serial.println(fsrReading);
Serial.print("Analog reading = "); 

// we'll need to change the range from the analog reading (0-1023) down to the range // used by analogWrite (0-255) with map!
LEDbrightness = map(fsrReading, 0, 1023, 0, 255);

if (fsrReading &lt; 10) {
Serial.println(" - No pressure"); } else if (fsrReading &lt; 200) {
Serial.println(" - Light touch"); } else if (fsrReading &lt; 500) {
Serial.println(" - Light squeeze"); } else if (fsrReading &lt; 800) {
Serial.println(" - Medium squeeze"); } else {
Serial.println(" - Big squeeze"); }
delay(50);

for(int i=0;i&lt;NUMPIXELS;i++){

  // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
  if (fsrReading&gt;1022){
  pixels.setPixelColor(i, pixels.Color(0,255,0)); 
  } else if (fsrReading&lt;1022) {
  pixels.setPixelColor(i, pixels.Color(0,0,0));
  }

  pixels.show(); // This sends the updated pixel color to the hardware.
 }
 }
</code></pre>
 30005: <blockquote>
  <p>My goal: FSRReading > 1022 then wait 5 seconds, turn on NeoPixel FSRReading &lt; 1022, then reset timer, turn off NeoPixel</p>
</blockquote>

<p>something like this will do.</p>

<pre><code>  static uint32_t pressed_time; //last time the pressure sensor is pressed
  static uint8_t pressed_state=0; //the previous state. '1'-&gt;pressed, '0'-&gt;not pressed

  if (pressed()) {
    if (!pressed_state)) { //first time the pressure sensor is pressed
      pressed_state = 1;  //pressure sensor has been pressed
      pressed_time = millis(); //take time now
    } 
  } else {                //no longer pressed
    pressed_state = 0;  //reset pressed state
    turnOffled();       //turn the led off
  }

  if ((millis() &gt; pressed_time + LED_DLY) &amp;&amp; pressed_state) turnOnled();
</code></pre>

<p>untested code, use at your own risk</p>

<p>edit:</p>

<p>the above code in action, with a minor modification to return the desired led state (active high):</p>

<pre><code>    //turn on / off the led pins
    if (led_ctrl()) digitalWrite(LED_PIN, HIGH);    //turn on the led, active high
    else digitalWrite(LED_PIN, LOW);        //turn off the led
}
</code></pre>

<p><a href="https://i.stack.imgur.com/2Wv3m.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2Wv3m.png" alt="enter image description here"></a></p>

<p>Note:
1. pressed() is active low;
2. the led is programmed to turn on 100ms after the button goes low.</p>

<p>hope it helps.</p>
 31106: <p>The basic concept for tracking timing in the loop() is as follows:</p>

<p>1) when your event occurs, you store the current time in a variable: </p>

<pre><code>unsigned long lastReading = 0;
uint8_t waiting = false;

if ( !waiting &amp;&amp; fsrReading &gt; 1022 ) {
  lastReading = millis();
  waiting = true;
}
</code></pre>

<p>2) you compare the lastReading variable against the current time and when enough time has passed, you can perform you timed action. ie:</p>

<pre><code>int timeToWait = 5000; // 5 seconds

// are we waiting? and has enough time passed?
if ( waiting &amp;&amp; millis() - lastReading &gt; timeToWait ) {
   waiting = false; // reset

   // Light up lights
   ...
}
</code></pre>

<p>Note: this specific implementation only requires the sensor to rise above 1022 once, then wait 5 seconds before executing the timed action. If you want the sensor reading to be maintained above 1022 for the entire duration then you would add a check for a release with something like:</p>

<pre><code>if ( fsrReading &lt; 1022 ) {
  waiting = false; // reset because reading fell below 1022
}
</code></pre>
