Hi there,
This is my first ever Arduino program so bear with me.
I have a pretty simple setup an Arduino UNO and two Force sensetive resistors ( basically a push button which can sense how hard you are pushing. it will give you a value between 0 and 1024).
My goal is to read sensor 1 connected to analog 0 and check if it is beyond 900 value, then read sensor 2 connected to analog 1 and check if that is also pressed beyond 900 value. Then I want to see if the time elapsed between sensor 1 press and sensor 2 press is greater than 1 second; light up the onboard LED (pin13) if not repeat.
Please note that sensor 1 and sensor 2 are not both pressed at the same time. always sensor 1 is pressed first, then released and then sensor 2 is pressed and released.
below you can see the quick code that I put together, But it doesn't quite work. Can you guys shed some light? i think it has to do with micros().
int fsrRead1;
int fsrRead2;
unsigned long time1;
unsigned long time2;
unsigned long dt;
void setup() {
// initialize digital pin 13 as an output.
Serial.begin(9600);
pinMode(13, OUTPUT);
//pinMode(A0,INPUT);
}
void loop() {
fsrRead1=analogRead(0);
if (fsrRead1 >900){
Serial.print("-------------");
Serial.print(fsrRead1);
time1=micros();
fsrRead2=analogRead(1);
}
if (fsrRead2>900){
Serial.print(fsrRead2);
time2=micros();
Serial.print("$$$$$Delta time2$$$$$ ");
Serial.println(time2-time1);
dt=time2-time1;
if (dt>1000000){
digitalWrite(13,HIGH);
Serial.println("<><><><><><><><><><><><><><><><><> ");
Serial.println(dt);
Serial.println("<><><><><><><><><><><><><><><><><> ");
}
}
else{
digitalWrite(13,LOW);
dt=0;
}
}
It doesn't quite work how?
What does it actually do despite what you want it to do?
What does the serial monitor read as you go through your procedure?
When I push the sensor beyond 900 value pressure:
It starts printing to serial port as expected:
-------------960
-------------959
-------------958
-------------955
-------------953
-------------949
-------------947
-------------943
-------------941
-------------937
-------------935
-------------931
when I push the second sensor it starts printing:
959$$$$$$$$$Delta time2$$$$$$$$$ 3120
-------------905
960$$$$$$$$$Delta time2$$$$$$$$$ 3120
-------------905
961$$$$$$$$$Delta time2$$$$$$$$$ 3120
-------------904
959$$$$$$$$$Delta time2$$$$$$$$$ 3120
-------------904
959$$$$$$$$$Delta time2$$$$$$$$$ 3128
-------------906
958$$$$$$$$$Delta time2$$$$$$$$$ 3120
-------------906
942$$$$$$$$$Delta time2$$$$$$$$$ 3120
As you can see dt is always 3120, which is the time difference between the two actions. no matter how much I delay between the two pushes i always get the same dt value!
So it doesn't enter into the third if statement to turn the LED on.
I hope that explains it better.
Thanks
Additionally the above serial outputs are achieved this way:
I press sensor 1 ( it starts printing) while i am still holding sensor 1 down, I push sensor 2, then the second sensor triggers the print statement.
If I press the first sensor and let go( printing from first sensor obviously stops) and then when I press the second sensor I don't get any serial print!! shouldn't i get serial print from second sensor?
because this is how i intend to use it: press the first sensor, let go and then press second sensor.
fsrRead1 = analogRead(0);
if (fsrRead1 > 900)
{
Serial.print("-------------");
Serial.print(fsrRead1);
time1 = micros();
fsrRead2 = analogRead(1);
}
How likely is it that the second reading will be greater than 900 so soon after the first one ?
Read the first sensor and if it is above the threshold save the time and start looking for the second sensor to be above the threshold. When it is, save the time, do the calculations and take the necessary actions.
Ooops, I think I found the problem! My bad guys! I wasn't reading fsrRead2 in the main loop now that I added fsrRead2=analogRead(1), it works.
Thank you guys