Hello, the issue I'm having when counting elapsed time is that the program uses the time at which I reference the variable. For instance, here is some sample code that shows my issue:
The println in the second if statement above always outputs 8 or 12, something like that, when it should be in the thousands.
If I print the t1 in both if statements, it's different, when it should be the same. It gets the time when the variable is referenced. It doesn't actually save the value of the time, it just saves the "method" attached to the variable.
@AWOL, other code literally has no bearing on the time so it should not matter. We're using this with a range finder sensor, but I can put everything if you really think it would help.
I edited my post and at the bottom have my full code.
@Robin2, I updated my post with full code at the bottom, t1 is set when one sensor senses an object and the other when another senses an object. I'm the one setting the sensors off and so I'm in control of the time.
Like I said, if I print t1 right after setting the variable and print t1 in another part of the code, it's different, which should not be the case. t1 should be the same throughout.
if I print t1 right after setting the variable and print t1 in another part of the code, it's different, which should not be the case. t1 should be the same throughout.
Can you please post a complete program that shows this problem ?
One of the limitations of SoftwareSerial is that only one SoftwareSerial port can be looking for input at a time. When you call rf1Serial.available() then it will watch for a start bit on rf1Serial. If you then call rf2Serial.available() it will STOP looking for a start bit on rf1Serial and start looking for one on rf2Serial.
One of the limitations of SoftwareSerial is that only one SoftwareSerial port can be looking for input at a time. When you call rf1Serial.available() then it will watch for a start bit on rf1Serial. If you then call rf2Serial.available() it will STOP looking for a start bit on rf1Serial and start looking for one on rf2Serial.
Is there any way to fix it? Any alternative? Thanks.
nishilshah17:
Is there any way to fix it? Any alternative? Thanks.
You could use an Arduino MEGA and use hardware serial (Serial1, Serial2, or Serial3) for one or both of the devices.
You could use an Arduino Leonardo and use hardware serial (Serial1) for one of the devices.
nishilshah17: @Robin2, I updated my post with full code at the bottom, t1 is set when one sensor senses an object and the other when another senses an object. I'm the one setting the sensors off and so I'm in control of the time.
Like I said, if I print t1 right after setting the variable and print t1 in another part of the code, it's different, which should not be the case. t1 should be the same throughout.
It makes it very difficult to make sense of a Thread if you update the code in an earlier Post. Put updated code in a new Post to maintain the flow of the discussion.
As said in Reply #9 your code provides no way to know what value is being printed.
You MIGHT be able to run yet another software serial alongside SoftwareSerial to have two functioning software ports. That's not why I wrote it and I never tested it like that.