Hello,
I am currently using some force sensitive resistors to sound an alarm when enough pressure is applied to them to make the input value < the threshold. The trouble is that I need them to do this only after the threshold has been met for 2 seconds. I am very new to coding and arduinos and I am having trouble figuring out how to achieve this. I would greatly appreciate any suggestions. Thank you.
Here is the relevant part of my current code
const int analogPin = A2; // pin that the sensor 1 is attached to
const int analogPin2 = A4; // pin that sensor 2 is attached to
const int threshold = 800; // an arbitrary threshold level that's in the range of the analog input
const int threshold2 = 500;
int SPEAKER = 9; // pin that speaker is attached to
int freq = 1800; // speaker frequency
void setup() {
// initialize the LED pin as an output:
pinMode(SPEAKER, OUTPUT);
// initialize serial communications:
Serial.begin(9600);
}
void loop() {
// read the value of the potentiometer:
int analogValue = analogRead(analogPin);
// if the analog value is low enough, buzz:
if (analogValue < threshold) {
tone(SPEAKER, freq);
delay(400);
noTone(SPEAKER);
delay(100);
}
else {
noTone(SPEAKER);
}